Pick flower (1s 128MB) flower
"Title description"
Shing is the princess of Z country, 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 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 Sample"
5 3 5
1 2 2) 3 1
1 5
1 2
2 2
2 3
3 5
"Output Example"
2
0
0
1
0
"Sample description"
Inquiry [1, 5]: The princess picked the color for 1 and 2 flowers, because the color 3 flower only one flower, the princess does not pick;
Inquiry [1, 2]: color 1 and color 2 of the flowers are only one flower, the princess does not pick;
Inquiry [2, 2]: color 2 flower Only one flower, the princess does not pick;
Inquiry [2, 3]: Because the color 2 flower has two flower, the princess picked the color 2 flower;
Inquiry [3, 5]: Color 1, 2, 3 flowers each, the princess does not pick.
"Data Range"
For 20% of data, n≤100,c≤100,m≤100;
For 50% of data, n≤100000,c≤100,m≤100000;
For 100% of data, 1≤n≤100000,c≤n,m≤100000.
Exercises
Main algorithm: tree-like array; quick sort
According to test instructions, it is easy to think that the answer is the number of the second flower per flower in the interval.
So we:
1, to deal with each flower and it the same color of the next flower position
2, will ask offline, with the left end of the keyword from small to large sort
3, Order Processing inquiry, for each moment, using a pointer to the left end of the previous interval to sweep back to the left end of the current interval, in the process of scanning can take advantage of the processed array, the tree array of tags updated, that is, to make the current interval of the left end of each of the second flower is marked
1#include <algorithm>2#include <iostream>3#include <cstring>4#include <cstdlib>5#include <cstdio>6#include <cmath>7 using namespacestd;8InlineintGet ()9 {Ten intx =0; One Charc =GetChar (); A while('0'> C | | C >'9') C =GetChar (); - while('0'<= C && C <='9') - { thex = (x <<3) + (x <<1) + C-'0'; -c =GetChar (); - } - returnx; + } - Const intme =1000233; + structShape A { at intL, R, I; - }; - shape C[me]; - intl; - intN, M, CO; - intA[me]; in intS[me]; - intTr[me]; to intNex[me]; + intAns[me]; -InlineBOOLrule (Shape A, shape B) the { * if(A.L! = B.L)returnA.L <B.L; $ returnA.R <B.R;Panax Notoginseng } -InlinevoidADD (intXinty) the { + while(x <=N) A { theTR[X] + =y; +x + = x & (-x); - } $ } $InlineintSum (intx) - { - intsum =0; the while(x) - {WuyiSum + =Tr[x]; theX-= x & (-x); - } Wu returnsum; - } About intMain () $ { -n = Get (), CO = get (), M =Get (); - for(inti =1; I <= N; ++i) A[i] =Get (); - for(inti = n; I >=1; --i) A { + if(S[a[i]]) nex[i] =S[a[i]]; the ElseNex[i] = n +1; -S[a[i]] =i; $ } the for(inti =1; I <= m; ++i) the { theC[I].I =i; theC[I].L = Get (), C[I].R =Get (); - } inSort (c +1, C +1+m, rule); the for(inti =1; I <= Co; ++i) the if(Nex[s[i]]) AboutADD (Nex[s[i]],1); theL =1; the for(inti =1; I <= m; ++i) the { + while(L <c[i].l) - { the if(Nex[l]) ADD (Nex[l],-1);Bayi if(Nex[nex[l]]) ADD (Nex[nex[l]],1); the++l; the } -ANS[C[I].I] = SUM (C[I].R)-SUM (C[I].L-1); - } the for(inti =1; I <= m; ++i) printf ("%d\n", Ans[i]); the}
Flower Bzoj 2743