Test Instructions: Give a sequence of n numbers, M queries, each asking for the number of different numbers within an interval.
Method: Offline + tree-like array
parsing: After reading the question did not have a clue, think of the line tree to engage, but it seems very troublesome, and then listen to their offline down to engage. It was almost clear that 1 more lessons were pushed. The offline and online gap is really big. If offline, all the intervals are linear. What is the general idea? Is that every number, we can preprocess where he was last seen. Then for an interval of inquiry [L,r], we can think of this to ask: the number of the last occurrence of the l~r in the number of the left of L. This will be good, first of all the interval by the right end of the order. Each time the element is added, the value of the position +1 where it was last appeared is added 1, then the value in its own position +1 is hit the 1 mark, which is a linear evaluation, that is, every time Getsum (A[I].L).
#include <stdio.h>#include <algorithm>using namespace STD;structnode{intLintR;intID;}; Node a[200100] ;intpre[200100] ;intcol[200100] ;intc[200100] ;intans[200100] ;intlast[1000100] ;intNintLowbit (intx) {returnx& (-X);}voidUpdateintXintP) { while(x <= N) {C[x] + = p; x + = Lowbit (x); }}intGetsum (intx) {intsum =0; while(x) {sum + = c[x]; X-= Lowbit (x); }returnsum;}intCMP (node asdf, Node B) {returnASDF.R < B.R;}intMain () {scanf("%d", &n); for(inti =1; I <= N; i++) {scanf("%d", &col[i]); Pre[i] = last[col[i]]; Last[col[i]] = i; }intm;scanf("%d", &m); for(inti =1; I <= m; i++) {scanf("%d%d", &A[I].L, &A[I].R); A[i].id = i; } sort (A +1, A +1+ M, CMP);intnow =0; for(inti =1; I <= m; i++) { while(Now < A[I].R) {now + +; Update (Pre[now] +1,1) ;if(now! = N) {Update (now +1,-1) ;} } Ans[a[i].id] = Getsum (A[I].L); } for(inti =1; I <= m; i++) {printf("%d\n", Ans[i]); }}
Bzoj 1878 [Sdoi2009]hh's Necklace offline + tree-shaped array