DESCRIPTIONHH has a chain of beautiful shells. HH believes that different shells will bring good luck, so after each walk, he will take out a shell and think about what they mean. HH keeps collecting new shells, so his necklace is getting longer. One day, he suddenly raised a question: how many different shells are included in a certain piece of shell? The question is difficult to answer ... Because the necklace is too long. So he had to ask the wise you to solve the problem. Input first line: An integer n that represents the length of the necklace. Second line: n integers representing the number of shells in the necklace (integers numbered 0 to 1000000, in turn). Line three: An integer m that indicates the number of HH queries. Next m line: two integers per line, L and R (1≤l≤r≤n), indicating the interval of the inquiry. OUTPUTM lines, one integer per line, in turn, to ask the corresponding answer. Sample Input6
1 2 3 4 3 5
3
1 2
3 5
2 6
Sample Output2
2
4
HINT
For 20% of data, n≤100,m≤1000;
For 40% of data, n≤3000,m≤200000;
For 100% of data, n≤50000,m≤200000.
Exercises
Tree-like arrays & offline Operations Classic Questions
Offline Dafa Good
Record where each position last occurred (like this "only appears once" often to do so)
Sort by one end, activate next when crossing
Tree-like array implementation
Code
#include <cstdio>#include<algorithm>using namespacestd;Const intmaxn=5e4+5, maxm=2e5+5, maxc=1e6+5;structedge{intL,r,num;} E[MAXM];BOOLcmpConstEdge &a,ConstEdge &b) { returnA.r>B.R;}intANS[MAXM],NXT[MAXN],LAST[MAXC];intc[maxn],n,m;intAddintXintW) { for(intI=x;i>0; i-= (i&-i)) c[i]+=W;}intSumintx) { intret=0; for(inti=x;i<=n;i+= (i&-i)) ret+=C[i]; returnret;}intMain () {intcc; scanf ("%d",&N); for(intI=1; i<=n;i++) {scanf ("%d",&cc); Nxt[i]=LAST[CC]; LAST[CC]=i; } for(intI=0; i<=1e6;i++) if(Last[i]) Add (Last[i],1); scanf ("%d",&m); for(intI=1; i<=m;i++) {scanf ("%d%d",&e[i].l,&E[I].R); E[i].num=i; } sort (E+1, e+m+1, CMP); e[0].r=N; for(intI=1; i<=m;i++){ if(e[i].r!=e[i-1].r) { for(intj=e[i-1].r;j>e[i].r;j--) Add (J,-1), add (Nxt[j],1); } Ans[e[i].num]=sum (E[I].L); } for(intI=1; i<=m;i++) printf ("%d\n", Ans[i]); return 0;}
"Tree-shaped Array" bzoj1878[sdoi2009] hh necklace