Title Description
Shang Son is an ancient country princess, usually a big hobby is picking flowers.
It was sunny and sunny today, and the princess went to the new garden in the palace to pick up the flowers in the morning.
The garden is large enough to accommodate n flowers, with a C color (denoted by an integer 1-c), and the flowers lined up so that the princess can pick flowers. Every time the princess collected flowers will be counted the color of the flowers, the more the number of colors she will be happier! At the same time, she has a hobby, she does not allow the last to pick up the flowers, a color of only one flower. To this end, the princess each picked a flower, either had already picked up the color of the flower, or have quite the right instinct to tell her that she will be able to pick up the color of the flowers again.
As a result of time, the princess can only walk through the garden for a continuous period of mining flowers, then let the Maid Fu Han to arrange the trip. Fu Han Jie comprehensive a variety of factors to develop a m-itinerary, and then to you ask the princess how many flowers can be picked (she knows you are a master programmer, will be able to quickly give the answer!) ), and finally choose the itinerary that makes the Princess happy (in order to get more bonuses!). )。
Input/output format
Input format:
The first line is four spaces separated by an integer n, c, and M. The next line of n spaces separated by the integer, each number in [1, c], the number of I represents the color of the first flower. The next M-line of two spaces separated by the integer L and R (L≤r), indicating that the maid arranged the trip for the princess through the L to the r flower to be picked flowers.
Output format:
A total of M-lines, one integer per line, and the number of numbers representing the color of the flower the Princess can pick up in the maid's first trip.
Input and Output sample input sample #: Copy
5 3 51 2 2 3 11 51 22 22 33 5
Output Example # #: Replication
20010
Description
For 100% of data, 1≤n≤2*10^62∗1 0 6 ,c≤n,m≤ 2*10^62∗1 0 6 .
There are two subtask.
Subtask1 guaranteed n,m,c \leq 3*10^5n,m,c≤3∗105, accounting for 100 points
Subtask2 guaranteed n,m,c \leq 2*10^6n,m,c≤2∗106, accounting for 100 points
Exercises
Tag Mo team Kaka ...
It's like an HH necklace with a tree-like array ...
However, we do not consider maintaining the last position of a certain number, but consider maintaining the first one in the second position where a number appears.
All in all, it's enumerating $l$, and then considering where each number appears in the interval from the beginning to the end.
Then in the tree array for all of these locations $+1$ and then the interval query can be
And then we can record the next position where every number appears. Next
If a number is on the left side of the $l$, then its next is not the second one, and it becomes the first one, so it's going to lose 1 of its next, then the number +1 on the next position of it.
May be a little abstract, perceptual understanding
1 //Minamoto2#include <iostream>3#include <cstdio>4#include <algorithm>5 using namespacestd;6 #defineGetc () (p1==p2&& (p2= (p1=buf) +fread (Buf,1,1<<21,stdin), P1==P2)? eof:*p1++)7 Charbuf[1<< +],*p1=buf,*p2=buf;8InlineintRead () {9 #defineNum ch-' 0 'Ten CharChBOOLflag=0;intRes; One while(!isdigit (ch=getc ())) A(ch=='-') && (flag=true); - for(Res=num;isdigit (Ch=getc ()); res=res*Ten+num); -(flag) && (res=-res); the #undefNum - returnRes; - } - Charsr[1<< +],z[ -];intc=-1, Z; +InlinevoidOt () {fwrite (SR),1, c+1, stdout), c=-1;} -InlinevoidPrintintx) { + if(c>1<< -) Ot ();if(x<0) sr[++c]= $, x=-x; A while(z[++z]=x%Ten+ -, x/=Ten); at while(Sr[++c]=z[z],--z); sr[++c]='\ n'; - } - Const intn=2e6+5; - structnode{ - intL,r,id; -InlineBOOL operator< (ConstNode &b)Const{returnl<B.L;} in }q[n]; - inta[n],ans[n],c[n],cnt[n],next[n],last[n],b[n],n,k,m; toInlinevoidAddintXinty) { + for(; x<=n;x+=x&-x) c[x]+=y; - } theInlineintQueryintx) { * intres=0; $ for(; x;x-=x&-x) res+=C[x];Panax Notoginseng returnRes; - } the intMain () { +N=read (), K=read (), m=read (); A for(intI=1; i<=n;++i) a[i]=read (); the for(intI=n;i;--i) next[i]=last[a[i]],last[a[i]]=i; + for(intI=1; i<=n;++i) - if(++b[a[i]]==2) Add (I,1); $ for(intI=1; i<=m;++i) $Q[i].l=read (), Q[i].r=read (), q[i].id=i; -Sort (q+1, q+1+m); - intnow=1; the for(intI=1; i<=m;++i) { - while(now<Q[I].L) {Wuyi if(Next[now]) Add (next[now],-1); the if(Next[next[now]]) Add (Next[next[now]],1); -++Now ; Wu } -Ans[q[i].id]=query (Q[I].R)-query (q[i].l-1); About } $ for(intI=1; i<=m;++i) print (ans[i]); - Ot (); - return 0; -}
[bzoj2743] [HEOI2012] Picking flowers (tree array)