Title Description
Give a sequence of length n. 1≤a[i]≤n.
The M-group asks, each time an interval [l,r] is asked, if there is a number that appears in [L,r] greater than (r-l+1)/2. If present, output this number, otherwise output 0.
Input
The first line is two numbers n,m.
Second row n number, A[i].
The next M-line, two numbers per line, l,r to ask [L,r] this interval.
Output
M lines, one answer per line.
Sample input
9 {
1 1 3 2 3 4 3
1 3
1 4
3 7
1 7
6 6
Sample output
1
0
3
0
4
Exercises
Chairman Tree
With bzoj2223, there is no need for discretization.
bzoj2223: http://www.cnblogs.com/GXZlegend/p/6292609.html
1#include <cstdio>2 intsi[10000010], lp[10000010], rp[10000010], root[500010], tot;3 voidPushup (intx)4 {5SI[X] = si[lp[x]] +Si[rp[x]];6 }7 voidInsintXint&y,intLintRintp)8 {9y = + +tot;Ten if(L = =R) One { ASi[y] = Si[x] +1; - return; - } the intMid = (L + r) >>1; - if(P <= mid) rp[y] =Rp[x], INS (lp[x], lp[y], L, Mid, p); - ElseLp[y] = lp[x], ins (rp[x], rp[y], Mid +1, R, p); - pushup (y); + } - intQueryintXintYintLintRintp) + { A if(L = = r)returnl; at intMid = (L + r) >>1; - if(Si[lp[y]]-si[lp[x]] > P)returnquery (Lp[x], lp[y], L, Mid, p); - if(Si[rp[y]]-si[rp[x]] > P)returnQuery (Rp[x], rp[y], Mid +1, R, p); - return 0; - } - intMain () in { - intN, M, I, A, b; toscanf"%d%d", &n, &m); + for(i =1; I <= N; i + + ) - { thescanf"%d", &a); *INS (Root[i-1], Root[i],1, N, a); $ }Panax Notoginseng while(M-- ) - { thescanf"%d%d", &a, &b); +printf"%d\n", Query (Root[a-1], Root[b],1, N, (b-a +1) >>1)); A } the return 0; +}
"bzoj3524" [poi2014]couriers