ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

带修莫队

2022-02-24 12:01:47  阅读:176  来源: 互联网

标签:ch 修莫队 int long freopen 例题 define


随便提几笔吧,时间不太充裕就不写太多了。

就是在区间的基础上再加上一个时间。

来个例题 数颜色|维护队列

直接就是考虑按照 \(l\) 所在块, \(r\) 所在块, \(t\) 的优先级排序,然后可以证明在块长取 \(n^{\frac{2}{3}}\) 时最优,可用最劣情况证明其复杂度。

然后就是稍微注意点细节就好,比如离线化问题的时候记得修改原数组之类的。

那么结合例题看下代码就大概能理解了吧。

code:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define file(a) freopen(#a".in","r",stdin),freopen(#a".out","w",stdout)
inline int read(){
	int s=0,f=1;char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') f=-1;ch=getchar();}
	while('0'<=ch&&ch<='9') {s=s*10+(ch^48);ch=getchar();}
	return s*f;
}
const int N=133333+3;
const int MAXN=1000000+3;
int n,Q,Siz;
int s[N],p[N],tot1,tot2;
struct ques{
	int l,r,t,opt;
}q[N];
struct opat{
	int p,s,f;
}c[N];
bool cmp(ques x,ques y){
	if(x.l/Siz==y.l/Siz){
		if(x.r/Siz==y.r/Siz) return x.t<y.t;
		return x.r/Siz<y.r/Siz;
	}
	return x.l/Siz<y.l/Siz;
}
bool cmp2(ques x,ques y){
	return x.t<y.t;
}
int Num,cnt[MAXN];
inline void add(int x){
	if(!cnt[x]) ++Num;
	++cnt[x];
}
inline void sub(int x){
	--cnt[x];
	if(!cnt[x]) --Num;
}
char opt[5];
int ans[N];
int main(){
	n=read();Q=read();Siz=pow(n,(double)2/(double)3);
	for(int i=1;i<=n;++i){
		s[i]=p[i]=read();
	}
	for(int t=1;t<=Q;++t){
		int a,b;
		scanf("%s",opt+1);a=read();b=read();
		if(opt[1]=='Q'){
			++tot1;q[tot1]={a,b,tot1,tot2};
		}else{
			++tot2;c[tot2]={a,s[a],b};
			s[a]=b;
		}
	}
	sort(q+1,q+1+tot1,cmp);
	int l=1,r=0,opt=0;
	for(int i=1;i<=tot1;++i){
		while(opt<q[i].opt){
			++opt;
			if(l<=c[opt].p&&c[opt].p<=r){
				add(c[opt].f);sub(c[opt].s);
			}
			p[c[opt].p]=c[opt].f;
		}
		while(q[i].opt<opt){
			if(l<=c[opt].p&&c[opt].p<=r){
				add(c[opt].s);sub(c[opt].f);
			}
			p[c[opt].p]=c[opt].s;
			--opt;
		}
		while(l<q[i].l){
			sub(p[l]);
			++l;
		}
		while(q[i].l<l){
			--l;
			add(p[l]);
		}
		while(r<q[i].r){
			++r;
			add(p[r]);
		}
		while(q[i].r<r){
			sub(p[r]);
			--r;
		}
		ans[q[i].t]=Num;
	}
	for(int i=1;i<=tot1;++i){
		printf("%d\n",ans[i]);
	}
	return 0;
}

标签:ch,修莫队,int,long,freopen,例题,define
来源: https://www.cnblogs.com/cbdsopa/p/15931107.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有