CF1764H Doremy's Paint 2
题意
给定长为 \(n\) 的序列 \(\{a_i\}\),初始 \(a_i=i\)。有 \(m\) 次操作,每次操作表示将 \((l,r]\) 所有数赋值为 \(a_l\)。现在你需要对每个 \(i\in[1,m]\) 求出若按顺序执行 \([i,i+k)\) 内的操作,最后序列中还剩多少种数字?第 \(i+m\) 次操作就是第 \(i\) 次操作。
\(n,m,k\le 2\times10^5\)
分析
将 \(m\) 次操作复制一份,变成执行一个区间内的操作。
注意到序列最开始互不相同,我们可以考虑记录 \(t_i\) 表示 \(i\) 颜色什么时候消失。考虑时间倒流,在 \(k\) 时刻若要做 \([l,r]\) 操作,则 \(\forall l<i\le r,t_i=k\),因为这个时刻 \((l,r]\) 全部被赋值为 \(a_l\),而 \(a_l\) 消失的条件就需要 \([l,r]\) 内的所有数全被覆盖了,所以 \(t_l=\max_{i=l}^r t_i\)。
考虑如何维护 \(t_i\)。发现我们要做的是形如若干次区间查最大值后推平、单点修改,可以考虑颜色段均摊,用 set 维护推平,推平的同时也可以方便地查最大值。同时维护一棵树状数组表示 \(t_i=j\) 的颜色数量。查询只需要查 \(t_i\le m+k-1\) 的颜色数量即可。复杂度 \(O(n\log n)\)。
点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define un using namespace
#define il inline
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof x)
#define popc __builtin_popcountll
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) ((x)&-(x))
#define lson(x) ((x)<<1)
#define rson(x) ((x)<<1|1)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
using i64=long long;
using u64=unsigned long long;
using pii=pair<int,int>;
template<typename T1,typename T2>inline void ckmx(T1 &x,T2 y){x=x>y?x:y;}
template<typename T1,typename T2>inline void ckmn(T1 &x,T2 y){x=x<y?x:y;}
inline auto rd(){
int qwqx=0,qwqf=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')qwqf=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){qwqx=(qwqx<<1)+(qwqx<<3)+ch-48;ch=getchar();}return qwqx*qwqf;
}
template<typename T>inline void write(T qwqx,char ch='\n'){
if(qwqx<0){qwqx=-qwqx;putchar('-');}
int qwqy=0;static char qwqz[40];
while(qwqx||!qwqy){qwqz[qwqy++]=qwqx%10+48;qwqx/=10;}
while(qwqy--)putchar(qwqz[qwqy]);if(ch)putchar(ch);
}
bool Mbg;
const int maxn=4e5+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,m,k;
struct o{
int l,r;
}a[maxn];
int ans[maxn];
struct BIT{
int c[maxn];
void add(int x,int y){while(x<=m+k)c[x]+=y,x+=lowbit(x);}
int qry(int x){int res=0;while(x)res+=c[x],x-=lowbit(x);return res;}
} T;
//存下标
set<int>s;
//存下标对应的死亡日期a[i]
int t[maxn];
int replace(int l,int r,int x){
auto p=s.upper_bound(l);
int oed=*p-1;
int st,ed,res=-1;
while(r>=*p){
st=*p,ed=*next(p,1)-1,ckmx(res,t[st]);
s.erase(p),T.add(t[st],-(ed-st+1));
if(r<ed)s.insert(r+1),t[r+1]=t[st],T.add(t[st],ed-r);
t[st]=-1;
p=s.upper_bound(l);
}
p=--s.upper_bound(l);
st=*p,ed=oed,ckmx(res,t[st]);
T.add(t[st],-(min(r,ed)-l+1));
if(r<ed)s.insert(r+1),t[r+1]=t[st];
if(l!=st)s.insert(l);
t[l]=x,T.add(x,r-l+1);
return res;
}
int getmax(int x){
return t[*(--s.upper_bound(x))];
}
inline void solve_the_problem(){
n=rd(),m=rd(),k=rd();
rep(i,1,m)a[i].l=rd(),a[i].r=rd(),a[i+m]=a[i];
rep(i,1,n)s.insert(i),t[i]=m+k,T.add(m+k,1);
s.insert(n+1);
per(i,m+k-1,1){
if(a[i].l!=a[i].r){
int x=replace(a[i].l+1,a[i].r,i);
if(x>getmax(a[i].l))replace(a[i].l,a[i].l,x);
// for(auto it=s.begin();it!=s.end();it++)write(*it,32),write(t[*it]);
}
// write(i);
// rep(j,1,m+k)write(T.qry(j)-T.qry(j-1),32);P__;
if(i<=m){
ans[i]=T.qry(i+k-1);
}
}
rep(i,1,m)write(n-ans[i],32);
}
bool Med;
signed main(){
// freopen(".in","r",stdin);freopen(".out","w",stdout);
fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
int _=1;
while(_--)solve_the_problem();
}
/*
*/

浙公网安备 33010602011771号