嘟嘟嘟
这题和主席树板子差不多啊。
正常建树,然后查询的时候看哪一个区间的和大于(R - L + 1),就到哪一个区间去找,否则返回(0)。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 5e5 + 5;
const int maxt = 1e7 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, m;
struct Tree
{
int ls, rs, sum;
}t[maxt];
int root[maxn], cnt = 0;
void insert(int old, int& now, int l, int r, int id)
{
t[now = ++cnt] = t[old];
t[now].sum++;
if(l == r) return;
int mid = (l + r) >> 1;
if(id <= mid) insert(t[old].ls, t[now].ls, l, mid, id);
else insert(t[old].rs, t[now].rs, mid + 1, r, id);
}
int len;
int query(int old, int now, int l, int r)
{
if(l == r) return (t[now].sum << 1) > len ? l : 0;
int mid = (l + r) >> 1;
if(((t[t[now].ls].sum - t[t[old].ls].sum) << 1) > len)
return query(t[old].ls, t[now].ls, l, mid);
else if(((t[t[now].rs].sum - t[t[old].rs].sum) << 1) > len)
return query(t[old].rs, t[now].rs, mid + 1, r);
else return 0;
}
int main()
{
n = read(); m = read();
for(int i = 1, x; i <= n; ++i) x = read(), insert(root[i - 1], root[i], 1, n, x);
for(int i = 1, L, R; i <= m; ++i)
{
L = read(); R = read(); len = R - L + 1;
write(query(root[L - 1], root[R], 1, n)), enter;
}
return 0;
}