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.
Source
Day2
I heard the jump wrote a tree-like array, so I'm going to have a team Shang. The water's going to be over 0.0.
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstdlib>5#include <algorithm>6#include <vector>7#include <cmath>8 #defineYyj (s) freopen (S ".", "R", stdin), Freopen (S ". Out", "w", stdout);9 #defineLLG Long LongTen #defineMAXN 1000010 One structnode{ A LLG l,r,wz; - }C[MAXN]; - Llg I,J,K[MAXN],KL,X,N,M,A[MAXN],L,R,ANS[MAXN],BJ[MAXN]; the using namespacestd; - BOOLcmpConstNode & A,ConstNode &b) - { - if(k[a.l]+1<k[b.l]+1)return true; + if(k[a.l]+1>k[b.l]+1)return false; - if(A.R<B.R)return true;Else return false; + } A intMain () at { -Yyj"a"); -Cin>>N; -KL=SQRT (n) +1; - while(i<=N) - { inJ=KL; m++; - while(j--) to { +i++; -k[i]=m; the } * } $ for(i=1; i<=n;i++) scanf ("%lld",&a[i]);Panax NotoginsengCin>>m; - for(i=1; i<=m;i++) {scanf ("%lld%lld", &C[I].L,&C[I].R); c[i].wz=i;} theSort (c+1, c+m+1, CMP); +l=c[1].L; r=c[1].R; A for(i=l;i<=r;i++) the { + if(!Bj[a[i]]) - { $ans[c[1].wz]++; $ } -bj[a[i]]++; - } the for(i=2; i<=m;i++) - {Wuyians[c[i].wz]=ans[c[i-1].wz]; the while(c[i].l>l) - { Wubj[a[l]]--; - if(!Bj[a[l]]) About { $ans[c[i].wz]--; - } -l++; - } A while(c[i].l<l) + { thel--; -bj[a[l]]++; $ if(bj[a[l]]==1) the { theans[c[i].wz]++; the } the } - while(c[i].r<R) in { thebj[a[r]]--; the if(!Bj[a[r]]) About { theans[c[i].wz]--; the } ther--; + } - while(c[i].r>R) the {Bayir++; thebj[a[r]]+=1; the if(bj[a[r]]==1) - { -ans[c[i].wz]+=1; the } the } the } the for(i=1; i<=m;i++) printf ("%lld\n", Ans[i]); - return 0; the}
Want to see the tree array solution: http://www.cnblogs.com/ljh2000-jump/p/5607110.html
BZOJ1878 [Sdoi2009]hh's Necklace