Bzoj2453: Maintenance queue

Source: Internet
Author: User
2453: Maintenance queue time limit: 10 sec memory limit: 128 MB
Submit: 183 solved: 89
[Submit] [Status] Description: Have you ever played marbles when you were a child? Child A has some marbles. A prefers to queue them, numbered from left to right as 1 to n. In order to make the entire queue colorful and beautiful, the children want to know the number of marbles of different colors in a row. Of course, a sometimes replaces the color of a bullet ball in the queue based on personal preferences. However, a hasn't learned programming yet, and he thinks brainstorming is a waste of mental power. So he asks you for help. The first line of the input file contains two integers, N and M. N integers in the second line, indicating the color of the marbles in the initial queue. Next m rows, each row is in the form of "q l r" or "r x C ", "q l r" indicates how many different colors of marbles A wants to know from the first marbles in the queue to the r marbles, "R x C" indicates that a changes the marbles at position X to the C color. For each Q operation, output a line indicates the query result. Sample Input
2 3
1 2
Q 1 2
R 1 2
Q 1 2
Sample output2
1 hint

 

For 100% of the data, there are 1 ≤ n ≤ 10000, 1 ≤ m ≤ 10000, Child A will not be modified more than 1000 times, all colors are represented by an integer ranging from 1 to 10 ^ 6.

 

Source

2011 Fujian Training

Question:

Use pre [I] to record the position of the previous ball of the same color as I.

When you ask about l to R, if pre [I] <r indicates that the ball from L to I is useless, then ans ++

We can use this idea... Multipart

Each part is sorted by Pre [I], and the question is the same as that of the tutorial.

----- Hzwer

Code:

 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: Maintenance queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.