BZOJ2453: 維護隊列

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   ar   for   

2453: 維護隊列Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 183  Solved: 89
[Submit][Status]Description你小時候玩過彈珠嗎?小朋友A有一些彈珠,A喜歡把它們排成隊列,從左至右編號為1到N。為了整個隊列鮮豔美觀,小朋友想知道某一段連續彈珠中,不同顏色的彈珠有多少。當然,A有時候會依據個人喜好,替換隊列中某個彈珠的顏色。但是A還沒有學過編程,且覺得頭腦風暴太浪費腦力了,所以向你來尋求協助。Input輸入檔案第一行包含兩個整數N和M。第二行N個整數,表示初始隊列中彈珠的顏色。接下來M行,每行的形式為“Q L R”或“R x c”,“Q L R”表示A想知道從隊列第L個彈珠到第R個彈珠中,一共有多少不同顏色的彈珠,“R x c”表示A把x位置上的彈珠換成了c顏色。Output對於每個Q操作,輸出一行表示詢問結果。Sample Input
2 3
1 2
Q 1 2
R 1 2
Q 1 2
Sample Output2
1HINT

 

對於100%的資料,有1 ≤ N ≤ 10000, 1 ≤ M ≤ 10000,小朋友A不會修改超過1000次,所有顏色均用1到10^6的整數表示。

 

Source

2011福建集訓

題解:

用pre[i]記錄前一個和i相同顏色的球的所在位置

詢問l到r時,如果pre[i]<r說明在l到i這一段沒用和i顏色相同的球,則ans++

利用這種思路我們可以。。。分塊

每一塊內按pre[i]排序,然後和教主的魔法那題都一樣了

-----hzwer

代碼:

 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 10000+100014 #define maxm 1000000+100015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 using namespace std;19 inline int read()20 {21     int x=0,f=1;char ch=getchar();22     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}23     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}24     return x*f;25 }26 int n,m,block,b[maxn],c[maxn],pre[maxn],pos[maxn],last[maxm];27 void reset(int x)28 {29     int l=(x-1)*block+1,r=min(x*block,n);30     for(int i=l;i<=r;i++)pre[i]=b[i];31     sort(pre+l,pre+r+1);32 }33 int find(int x,int y)34 {35     int l=(x-1)*block+1,r=x*block,mid;36     while(l<=r)37     {38         mid=(l+r)>>1;39         if(pre[mid]>=y)r=mid-1;else l=mid+1;40     }41     return l-(x-1)*block-1;42 }43 int query(int x,int y)44 {45     int sum=0,bx=pos[x],by=pos[y];46     if(by-bx<=1)47     {48         for(int i=x;i<=y;i++)if(b[i]<x)sum++;49     }50     else51     {52         for(int i=x;i<=bx*block;i++)if(b[i]<x)sum++;53         for(int i=(by-1)*block+1;i<=y;i++)if(b[i]<x)sum++;54     }55     for(int i=bx+1;i<by;i++)sum+=find(i,x);56     return sum;57 }58 void change(int x,int y)59 {60     for(int i=1;i<=n;i++)last[c[i]]=0;61     c[x]=y;62     for(int i=1;i<=n;i++)63     {64         int t=b[i];65         b[i]=last[c[i]];66         last[c[i]]=i;67         if(t!=b[i])reset(pos[i]);68     }69 }70 int main()71 {72     freopen("input.txt","r",stdin);73     freopen("output.txt","w",stdout);74     n=read();m=read();75     block=floor(sqrt(n));76     for(int i=1;i<=n;i++)77      {78          c[i]=read();79          pos[i]=(i-1)/block+1;80          b[i]=last[c[i]];81          last[c[i]]=i;82      }83     for(int i=1;i<=pos[n];i++)reset(i);84     char ch;int x,y;85     while(m--)86     {87         ch=‘ ‘;88         while(ch!=‘Q‘&&ch!=‘R‘)ch=getchar();x=read();y=read();89         if(ch==‘R‘)change(x,y);else printf("%d\n",query(x,y));90     } 91     return 0;92 }
View Code

 

BZOJ2453: 維護隊列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.