Test instructions
A sequence with a length of N and a M query;
The number of different numbers in the interval to ask;
n<=50000,m<=200000, Digital <=1000000
Exercises
The online algorithm is too advanced and will not, so this problem will be done offline;
Analysis data range, m query can be stored completely, 1000000 of the number can also be hash barely discrete;
Then consider the number of a range of numbers, probably 1-r minus 1-(L-1) ;
But because there are duplicate numbers, for the number of repetitions, we should only calculate between l-r;
That's the way to log the array pre[x] When you traverse the query, indicating where the first digit of the x is located (the initial value is 0);
Using a tree-like array to maintain the prefix of the number of categories and the number of recorded numbers to delete the previous one, add new;
Then the right end of the sorting query, each encounter the right endpoint can solve the query;
There is no hole, to understand the 1 A;
Code:
#include <stdio.h> #include <string.h> #include <algorithm> #define N 50005#define M 200002#define V 1000001using namespace Std;int a[n],pre[v],tree[n];struct node{int l,r,ans,num;} Q[m];int CMP (node A,node b) {return A.R<B.R;} int CMP2 (node A,node b) {return a.num<b.num;} int lowbit (int k) {return k& (-K);} void Add (int k,int val) {while (k<=n) {tree[k]+=val;k+=lowbit (k);}} int query (int k) {int ret=0;while (k) {ret+=tree[k];k-=lowbit (k);} return ret;} int main () {int n,m,i,j,k,l,r;scanf ("%d", &n), for (i=1;i<=n;i++) scanf ("%d", A+i), scanf ("%d", &m); for (i=1;i <=m;i++) scanf ("%d%d", &Q[I].L,&Q[I].R), Q[i].num=i;sort (Q+1,Q+1+M,CMP); for (i=1,j=1;i<=n;i++) {if ( Pre[a[i]]) Add (pre[a[i]],-1), add (i,1);p re[a[i]]=i;if (I==Q[J].R) k=query (i), while (I==Q[J].R) {q[j++].ans=k-query (q [J].l-1);}} Sort (Q+1,Q+1+M,CMP2); for (i=1;i<=m;i++) {printf ("%d\n", Q[i].ans);} return 0;}
bzoj-1878 hh Necklace