标签:== chmod eve eof mit otto typedef long color
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 1778 Accepted Submission(s): 793
#include <bits/stdc++.h> typedef long long ll; using namespace std; const int maxn = 1e5 + 10; int n, q, c[maxn]; vector<int> e; int root[maxn * 40], sum[maxn * 40], tot; int ls[maxn * 40], rs[maxn * 40]; inline void init() { e.clear(); memset(root, 0, sizeof(root)); memset(ls, 0, sizeof(ls)); memset(rs, 0, sizeof(rs)); tot = 0; } inline void build(int &rt, int l, int r) { rt = ++tot; sum[rt] = 0; if (l == r)return; int mid = l + r >> 1; build(ls[rt], l, mid); build(rs[rt], mid + 1, r); } inline void update(int l, int r, int pre, int &rt, int pos) { rt = ++tot; ls[rt] = ls[pre]; rs[rt] = rs[pre]; sum[rt] = sum[pre] + 1; if (l == r)return; int mid = l + r >> 1; if (pos <= mid) { update(l, mid, ls[pre], ls[rt], pos); } else { update(mid + 1, r, rs[pre], rs[rt], pos); } } inline int query(int l, int r, int pre, int &rt, int pos) { if (l == r)return l; int mid = l + r >> 1; int cur = sum[ls[rt]] - sum[ls[pre]]; if (cur >= pos) { return query(l, mid, ls[pre], ls[rt], pos); } else { return query(mid + 1, r, rs[pre], rs[rt], pos - cur); } } int main() { #ifndef ONLINE_JUDGE freopen("1.txt", "r", stdin); #endif while (scanf("%d%d", &n, &q) != EOF) { init(); for (register int i = 1; i <= n; ++i) { scanf("%d", &c[i]); e.emplace_back(c[i]); } sort(e.begin(), e.end()); e.erase(unique(e.begin(), e.end()), e.end()); build(root[0], 1, n); for (register int i = 1; i <= n; ++i) { c[i] = lower_bound(e.begin(), e.end(), c[i]) - e.begin() + 1; } for (register int i = 1; i <= n; ++i) { update(1, n, root[i - 1], root[i], c[i]); } //cerr<<"***"<<endl; int l, r; while (q--) { scanf("%d%d", &l, &r); int mx = r - l + 1; int L = 1, R = mx, res = 0; while (L <= R) { int mid = L + R >> 1; int rk = mx - mid + 1; int id = query(1, n, root[l - 1], root[r], rk); if (e[id - 1] >= mid) { res = mid; L = mid + 1; } else { R = mid - 1; } } printf("%d\n", res); } } return 0; }
标签:== chmod eve eof mit otto typedef long color
原文地址:https://www.cnblogs.com/czy-power/p/11450429.html