DescriptionShing 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, 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
The first line four spaces separated by an integerN、CAndM。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 Princess through the first l To the R flower to pick flowers. Outputa 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. Sample Input5 3 5
1 2 2) 3 1
1 5
1 2
2 2
2 3
3 5
Sample Output2
0010
"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 flowers are only one, 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.
HINT
"Data Range"
For 100% of data, 1≤n≤10^6,c≤n,m≤10^6.
Ideas
Say he is where.
At first, I was going to do it with Mo team, and the result Nsqrt (n) Decisive tttttttttt
Then know that this problem online is not to be done. Decisive offline processing.
Will all inquiries [st,ed] According to St for the First keyword, ed for the second keyword sort.
Then the preprocessing records where the next flower of the same color in each position is located, the next of the flowers on the I-bit is next[i]
First for all the colors of the flower next[next[pos[i]]++; (Pos[i] is the position of the first I-color flower)
Then because the inquiry of St is ordered, is incremented, then we maintain a variable l, each time the new query to the right sweep, for those who were originally in the query scope but updated the scope of the query is not the corresponding update tree array of flowers.
1#include <iostream>2#include <cstring>3#include <string>4#include <cstdio>5#include <cstdlib>6#include <cmath>7#include <algorithm>8#include <queue>9#include <stack>Ten#include <map> One#include <Set> A#include <list> -#include <vector> -#include <ctime> the#include <functional> - #definePRITNF printf - #defineScafn scanf - #defineSACNF scanf + #definefor (i,j,k) for (int i= (j); i<= (k);(i) + +) - #defineClear (a) memset (A,0,sizeof (a)) + using namespacestd; Atypedef unsignedintUint; at Const intinf=0x3fffffff; - Const Doubleeps=1e-Ten; - ///==============struct declaration============== - structquerys{ - intSt,ed,no; - BOOL operator< (ConstQuerys &RHS)Const{ in returnst==rhs.st?ed<rhs.ed:st<Rhs.st; - } to }; + ///==============var declaration================= - Const intmaxn=1000100; the intn,c,m; * intCOLOR[MAXN],POS[MAXN],NEXT[MAXN]; $ intA[MAXN],ANS[MAXN];Panax Notoginseng Querys QUERY[MAXN]; - ///==============function declaration============ the voidAdd_bit (intXintv); + intQuery_bit (intx); A intLowbit (intx) {returnx&-x;} the ///==============main code======================= + intMain () - { $ #definefile__ $ #ifdef file__ -Freopen ("Input.txt","R", stdin); -Freopen ("output.txt","W", stdout); the #endif -scanf"%d%d%d",&n,&c,&m);Wuyi for(intI=1; i<=n;i++) scanf ("%d", color+i); the for(inti=n;i>=1; i--){ -next[i]=Pos[color[i]]; Wupos[color[i]]=i; - } About for(intI=1; i<=c;i++) $ if(Next[pos[i]]) -Add_bit (Next[pos[i]),1); - for(intI=1; i<=m;i++){ -scanf"%d%d",&query[i].st,&Query[i].ed); AQuery[i]. no=i; + } theSort (query+1, query+1+m); - intL=1; $ for(intI=1; i<=m;i++){ the while(l<query[i].st) { the if(Next[next[l]]) theAdd_bit (Next[next[l]),1); the if(Next[l]) -Add_bit (next[l],-1); inl++; the } theAns[query[i]. No]=query_bit (Query[i].ed)-query_bit (query[i].st-1); About } the for(intI=1; i<=m;i++) theprintf"%d\n", Ans[i]); the return 0; + } - ///================fuction code==================== the voidAdd_bit (intXintv) {Bayi while(x<=N) { thea[x]+=v; thex+=lowbit (x); - } - } the intQuery_bit (intx) { the intres=0; the while(x>0){ theres+=A[x]; -x-=lowbit (x); the } the returnRes; the}BZOJ1878
"HEOI2012" Picking flower BZOJ2743