http://www.lydsy.com/JudgeOnline/problem.php?id=1878
Test instructions: ...
Idea: It's a lot easier than the previous question. Number is small, open an array hash record occurrence number (remember the array to open 1e6), and then directly counted on the line.
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 #defineN 500107 structNode {8 intL, R, ans, id;9} p[n*4];Ten intmp[n* -], Num[n], ans, kuai; One A BOOLcmpConstNode &a,ConstNode &b) { - if(A.l/kuai = = B.l/kuai)returnA.R <B.R; - returnA.l/kuai < B.L/Kuai; the } - - BOOLCmpid (ConstNode &a,ConstNode &b) { - returna.ID <b.id; + } - + voidUpdateintIdintW) { A if(Mp[num[id]]) ans--; Mp[num[id]] + = W;if(Mp[num[id]]) ans++; at } - - intMain () { - intN, M; - while(~SCANF ("%d", &N)) { - for(inti =1; I <= N; i++) scanf ("%d", Num +i); inscanf"%d", &m); - for(inti =1; I <= m; i++) scanf ("%d%d", &P[I].L, &P[I].R), p[i].id =i; toKuai = sqrt (n); Ans =0; +Memset (MP,0,sizeof(MP)); -Sort (p +1, p +1+m, CMP); the for(intL =1, R =0, i =1; I <= m; i++) { * intL = p[i].l, r =P[I].R; $ for( ; L < L; l++) Update (L,-1);Panax Notoginseng for( ; L > L; l--) Update (L-1,1); - for( ; R < R; r++) Update (R +1,1); the for( ; R > R; r--) Update (R,-1); +P[i].ans =ans; A } theSort (p +1, p +1+m, cmpid); + for(inti =1; I <= m; i++) printf ("%d\n", P[i].ans); - } $ return 0; $ } - /* - 6 the 1 2 3 4 3 5 - 4Wuyi 1 2 the 3 5 - 2 6 Wu 1 1 - */
Bzoj 1878:[sdoi2009]hh Necklace (Mo team algorithm)