一、题目描述:
给你一个长度为
数据范围:
二、解题思路:
带修莫队:(思路当然也不是我想出来的
加一维时间轴,感觉有点抽象,但还是可以理解。
当块长取
时间复杂度
三、完整代码:
1 #include<cmath> 2 #include<iostream> 3 #include<algorithm> 4 #define N 150000 5 #define K 1000010 6 using namespace std; 7 char opt; 8 int cnt1,cnt2; 9 int n,m,l=1,r,M,sum; 10 int c[N],ans[N],cnt[K]; 11 struct Query{ 12 int L,R,T,id; 13 bool operator < (const Query &t)const{ 14 if(L/M!=t.L/M) return L<t.L; 15 if(R/M!=t.R/M) return R<t.R; 16 return T<t.T; 17 } 18 }a[N]; 19 struct Modify{ 20 int p,val; 21 }b[N]; 22 void upt(int v,int f) 23 { 24 cnt[v]+=f; 25 if(f>0&&cnt[v]==1) sum++; 26 if(f<0&&cnt[v]==0) sum--; 27 } 28 void Change(int t) 29 { 30 if(l<=b[t].p&&b[t].p<=r) 31 upt(b[t].val,1),upt(c[b[t].p],-1); 32 swap(b[t].val,c[b[t].p]); 33 } 34 int main() 35 { 36 ios::sync_with_stdio(false); 37 cin.tie(0);cout.tie(0); 38 cin>>n>>m; 39 M=pow(n,2.0/3.0); 40 for(int i=1;i<=n;i++) 41 cin>>c[i]; 42 for(int i=1;i<=m;i++) 43 { 44 cin>>opt; 45 if(opt=='Q') 46 { 47 cnt1++; 48 cin>>a[cnt1].L>>a[cnt1].R; 49 a[cnt1].id=cnt1,a[cnt1].T=cnt2; 50 } 51 if(opt=='R') 52 cnt2++,cin>>b[cnt2].p>>b[cnt2].val; 53 } 54 sort(a+1,a+1+cnt1); 55 for(int i=1,t=0;i<=cnt1;i++) 56 { 57 while(t<a[i].T) Change(++t); 58 while(t>a[i].T) Change(t--); 59 while(l>a[i].L) upt(c[--l],1); 60 while(r<a[i].R) upt(c[++r],1); 61 while(l<a[i].L) upt(c[l++],-1); 62 while(r>a[i].R) upt(c[r--],-1); 63 ans[a[i].id]=sum; 64 } 65 for(int i=1;i<=cnt1;i++) 66 cout<<ans[i]<<'\n'; 67 return 0; 68 }
四、写题心得:
第一道带修莫队,纪念一下。收获经验如下:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】