[POI2014]KUR-Couriers BZOJ3524 主席树
给一个长度为n的序列a。1≤a[i]≤n。
m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2。如果存在,输出这个数,否则输出0。
第一行两个数n,m。
第二行n个数,a[i]。
接下来m行,每行两个数l,r,表示询问[l,r]这个区间。
m行,每行对应一个答案。
7 5
1 1 3 2 3 4 3
1 3
1 4
3 7
1 7
6 6
1
0
3
0
4
仍然是基础的主席树;
不离散化也可以;
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> //#pragma GCC optimize(2) //#include<cctype> //#pragma GCC optimize("O3") using namespace std; #define maxn 700005 #define inf 0x3f3f3f3f #define INF 9999999999 #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) #define rdult(x) scanf("%lu",&x) #define rdlf(x) scanf("%lf",&x) #define rdstr(x) scanf("%s",x) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const long long int mod = 1e9 + 7; #define Mod 1000000000 #define sq(x) (x)*(x) #define eps 1e-3 typedef pair<int, int> pii; #define pi acos(-1.0) //const int N = 1005; #define REP(i,n) for(int i=0;i<(n);i++) inline ll rd() { ll x = 0; char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == '-') f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); } ll sqr(ll x) { return x * x; } /*ll ans; ll exgcd(ll a, ll b, ll &x, ll &y) { if (!b) { x = 1; y = 0; return a; } ans = exgcd(b, a%b, x, y); ll t = x; x = y; y = t - a / b * y; return ans; } */ ll qpow(ll a, ll b, ll c) { ll ans = 1; a = a % c; while (b) { if (b % 2)ans = ans * a%c; b /= 2; a = a * a%c; } return ans; } int n, m, q; int cnt = 0; int a[maxn], b[maxn], T[maxn]; int sum[maxn << 5], lson[maxn << 5], rson[maxn << 5]; int build(int l, int r) { int rt = ++cnt; sum[rt] = 0; if (l < r) { int mid = (l + r) >> 1; lson[rt] = build(l, mid); rson[rt] = build(mid + 1, r); } return rt; } int upd(int pre, int l, int r, int x) { int rt = ++cnt; lson[rt] = lson[pre]; rson[rt] = rson[pre]; sum[rt] = sum[pre] + 1; if (l < r) { int mid = (l + r) >> 1; if (x <= mid)lson[rt] = upd(lson[pre], l, mid, x); else rson[rt] = upd(rson[pre], mid + 1, r, x); } return rt; } int query(int u, int v, int l, int r, int k) { if (l >= r)return l; int x = sum[lson[v]] - sum[lson[u]]; int y = sum[rson[v]] - sum[rson[u]]; int mid = (l + r) >> 1; if (x > k)return query(lson[u], lson[v], l, mid, k); else if (y > k) return query(rson[u], rson[v], mid + 1, r, k); else return 0; } int main(){ //ios::sync_with_stdio(0); rdint(n); rdint(q); for (int i = 1; i <= n; i++)rdint(a[i]), b[i] = a[i]; sort(b + 1, b + 1 + n); m = unique(b + 1, b + 1 + n) - b - 1; T[0] = build(1, m); for (int i = 1; i <= n; i++) { int t = lower_bound(b + 1, b + 1 + m, a[i]) - b; T[i] = upd(T[i - 1], 1, m, t); } while (q--) { int x, y, z; rdint(x); rdint(y); z = (y - x + 1) >> 1; int t = query(T[x - 1], T[y], 1, m, z); if (t == 0)printf("0\n"); else printf("%d\n", b[t]); } return 0; }
EPFL - Fighting