Title Description
Coffee in the world is also a lollipop sold, coffee bought N (1≤n≤50000) only attached. The n lollipop is wrapped in a small plastic bag, lined up in a row, and the adjacent two lollipop plastic bags are connected. For convenience, we numbered the lollipop from left to right at 1. N. Each lollipop has a taste. The taste of the first I is CI (1≤ci≤50000). Two lollipop i,j tastes the same, when and only if CI=CJ. Coffee's evaluation of the overall taste of M-Lollipop is rather strange. If there is a taste of c0 in this m lollipop that is more than half m/2 of the total, then coffee thinks the M lollipop is mainly c0 flavored. Coffee know that the c0 here must be unique if it exists. And when C0 does not exist, coffee thinks the M-Lollipop is a mixed flavor. Coffee temporarily reluctant to eat lollipop, it is thinking of some fun problems. If you consider a continuous subsequence of the lollipop sequence S. T (1≤s≤t≤n), including Lollipop s and T. So what's the overall taste of this t-s+1 lollipop? Coffee has a bunch of such questions, altogether M (1≤m≤50000). The first problem is the lollipop sub-sequence Si. TI's overall taste. Please help to solve.
Input
Line 1th: Two integers separated by a space, representing N,m. 2nd.. N+1 line: One integer per line, and line i+1 represents CI. Line n+2..n+m+1: Each line of two integers separated by a space of i+n+1 line, Si,ti.
Output
1th.. M-line: An integer i-integer for each line represents your answer to question I, which is SI. TI's overall taste. If the overall taste is c0, then the answer is expressed in C0. If the overall taste is a mixed taste, then the answer is expressed in 0
Sample input
5 3
1
2
2
1
1
1 5
2 5
2 4
Sample output
1
0
2
Exercises
Chairman Tree
Same as "bzoj2223" [Coci 2009]patuljci practice.
Time Complexity $O (n\log N) $.
It's a pleasure to take a blood ^_^
#include <cstdio> #define N 300001int root[n], lp[n << 5], Rp[n << 5], Si[n << 5], tot;void pus HUP (int x) {si[x] = Si[lp[x]] + si[rp[x]];} void ins (int x, int &y, int l, int r, int p) {y = ++tot; if (L = = r) {Si[y] = si[x] + 1; Return } int mid = (L + r) >> 1; if (P <= mid) rp[y] = Rp[x], ins (lp[x], lp[y], L, Mid, p); else lp[y] = lp[x], ins (rp[x], rp[y], mid + 1, r, p); Pushup (y);} int query (int x, int y, int l, int r, int p) {if (L = = r) return l; int mid = (L + r) >> 1; if (Si[lp[y]]-si[lp[x]] > P) return query (Lp[x], lp[y], L, Mid, p); if (Si[rp[y]]-si[rp[x]] > P) return query (Rp[x], rp[y], mid + 1, r, p); return 0;} int main () {int n, Lim = 50000, M, I, X, Y, t; scanf ("%d%d", &n, &m); for (i = 1; I <= n; i + +) {scanf ("%d", &x); Ins (root[i-1], root[i], 1, Lim, X); } while (M--) { scanf ("%d%d", &x, &y); t = query (Root[x-1], root[y], 1, Lim, (Y-x + 1) >> 1); if (t) printf ("%d\n", t); else printf ("0\n"); } return 0;}
"bzoj5178" [Jsoi2011] Lollipop chairman Tree