日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

P1903-[国家集训队]数颜色/维护队列【带修莫队】

發布時間:2023/12/3 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 P1903-[国家集训队]数颜色/维护队列【带修莫队】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

正題

題目鏈接:https://www.luogu.com.cn/problem/P1903


題目大意

要求支持兩個操作

  • QLR:Q\ \ L\ \ R:Q??L??R:詢問L,RL,RL,R之間有多少個不同的數
  • RPCol:R\ \ P\ \ Col:R??P??Col:PPP修改為ColColCol

  • 解題思路

    莫隊中多加一個時間維度ttt表示前面有多少個修改操作,然后莫隊即可。


    codecodecode

    #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int N=143333; struct que_node{int id,t,l,r; }que[N]; struct cha_node{int pos,val,pre; }cha[N]; int n,m,c[N],ans[N],answer,cnt,tot,T; int v[1000010]; bool operator<(que_node x,que_node y){if(x.l/T!=y.l/T)return x.l/T<y.l/T;if(x.r/T!=y.r/T)return x.r/T<y.r/T;return x.t<y.t; } int main() {scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)scanf("%d",&c[i]);for(int i=1;i<=m;i++){char op[3];int x,y;scanf("%s%d%d",op,&x,&y);if(op[0]=='Q'){if(x>y)swap(x,y);que[++cnt]=(que_node){cnt,tot,x,y};}else{cha[++tot]=(cha_node){x,y,c[x]};c[x]=y;}}T=ceil(exp((log(n)+log(tot))/3));for(int i=tot;i>=1;i--)c[cha[i].pos]=cha[i].pre;sort(que+1,que+1+cnt);int l=1,r=0,t=0;for(int i=1;i<=cnt;i++){while(que[i].l<l)answer+=!v[c[--l]]++;while(que[i].l>l)answer-=!--v[c[l++]];while(que[i].r>r)answer+=!v[c[++r]]++;while(que[i].r<r)answer-=!--v[c[r--]];while(que[i].t<t){int pos=cha[t].pos;if(l<=pos&&pos<=r)answer-=!--v[c[pos]];c[pos]=cha[t--].pre;if(l<=pos&&pos<=r)answer+=!v[c[pos]]++;}while(que[i].t>t){int pos=cha[++t].pos;if(l<=pos&&pos<=r)answer-=!--v[c[pos]];c[pos]=cha[t].val;if(l<=pos&&pos<=r)answer+=!v[c[pos]]++;}ans[que[i].id]=answer;}for(int i=1;i<=cnt;i++)printf("%d\n",ans[i]);return 0; }

    總結

    以上是生活随笔為你收集整理的P1903-[国家集训队]数颜色/维护队列【带修莫队】的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。