跳到正文

题解

题解:AT_arc164_e [ARC164E] Segment-Tree Optimization

Purslane约 3 分钟
原文
文章信息与版本

文章信息

发布
2024年9月26日
收录
2026年9月9日
更新
2026年11月5日
篇幅
约 3 分钟
获赞
2

版本

  1. v1当前

Solution

唐龙题,没啥难度。但是有点细节。

  1. 考虑 dd 的最小值。假设本质不同的段有 kk 个,那么必定有 2d≥k2^d \ge k,因此 dmin⁡=⌈log⁡2(k)⌉d_{\min} = \lceil \log_2(k) \rceil。这是容易构造的。
  2. 考虑求 cc 的最小值。显然在第 d−1d-1 层以内的本质不同的线段越多越好。因此我们要在第 dd 层放 2(k−2d−1)2(k - 2^{d - 1}) 条线段,他们两两相邻配对,被经过的次数是两倍他们被经过的总次数减去四倍它们被同时经过的总次数。

复杂度 O(n2)O(n^2)。

C++
#include<bits/stdc++.h>
#define int long long
#define ffor(i,a,b) for(int i=(a);i<=(b);i++)
#define roff(i,a,b) for(int i=(a);i>=(b);i--)
using namespace std;
const int MAXN=4000+10,MAXM=2e5+10;
int n,q,dp[MAXN][MAXN],l[MAXM],r[MAXM],lsh[MAXM],tot,cnt1[MAXN],cnt2[MAXN];
signed main() {
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>q;
	ffor(i,1,q) cin>>l[i]>>r[i],lsh[++tot]=l[i],lsh[++tot]=r[i]+1;
	lsh[++tot]=1,lsh[++tot]=n+1;
	sort(lsh+1,lsh+tot+1),tot=unique(lsh+1,lsh+tot+1)-lsh-1;
	ffor(i,1,q) l[i]=lower_bound(lsh+1,lsh+tot+1,l[i])-lsh,r[i]=lower_bound(lsh+1,lsh+tot+1,r[i]+1)-lsh-1;
	int cnt=tot-1;
	cout<<ceil(log2(cnt))<<' ';
	if(cnt==1) return cout<<q,0;
	int rst=cnt-pow(2,(int)ceil(log2(cnt))-1);
	ffor(i,1,q) {
		cnt1[l[i]]++,cnt1[r[i]+1]--;
		cnt2[l[i]]++,cnt2[r[i]]--;	
	}
	ffor(i,1,tot) cnt1[i]+=cnt1[i-1],cnt2[i]+=cnt2[i-1];
	memset(dp,0x3f,sizeof(dp));
	dp[0][rst]=0;
	ffor(i,1,tot-1) {
		ffor(j,0,rst) dp[i][j]=dp[i-1][j];
		if(i>=2) ffor(j,0,rst-1) dp[i][j]=min(dp[i][j],dp[i-2][j+1]+cnt1[i]+cnt1[i-1]-2*cnt2[i-1]);
	}
	cout<<dp[tot-1][0]*2;
	return 0;
}
原文
存档评论1 条抓取于