Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 31790 | Accepted: 9838 | |
Case Time Limit: 2000MS |
Description
Input
Output
Sample Input
7 3 1 5 2 6 3 7 4 2 5 3 4 4 1 1 7 3
Sample Output
5 6 3
Hint
Source
#include <iostream> #include <cstdio> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #include <vector> using namespace std; #define rep(i, l, r) for (int i = l; i <= r; i++) #define REP(i, l, r) for (int i = l; i >= r; i--) #define X first #define Y second #define MAXN 100010 int n, T_T, num[MAXN], head, tail, root[MAXN], m = 0, hash[MAXN], maxvalue; struct Tree{int l, r, lc, rc, s;} a[MAXN*25]; pair<int, int> f[MAXN]; bool cmp1(pair<int, int> a, pair<int, int> b) {return a.X < b.X;} bool cmp2(pair<int, int> a, pair<int, int> b) {return a.Y < b.Y;} inline void discretization() { sort(f+1, f+1+n, cmp1); int pre = f[1].X; hash[1] = f[1].X; f[1].X = 1; rep(i, 2, n) if (f[i].X != pre) pre = f[i].X, hash[f[i-1].X+1] = f[i].X, f[i].X = f[i-1].X + 1; else f[i].X = f[i-1].X; maxvalue = f[n].X; sort(f+1, f+1+n, cmp2); rep(i, 1, n) num[i] = f[i].X; } inline void build_tree(int i, int L, int R, int x) { int mid = (L + R) >> 1; a[i].l = L, a[i].r = R; if (L == R) {a[i].s = (L == x ? 1 : 0); return;} build_tree(a[i].lc = ++m, L, mid, x); build_tree(a[i].rc = ++m, mid+1, R, x); a[i].s = a[a[i].lc].s + a[a[i].rc].s; } inline void add_tree(int i, int j, int L, int R, int x) { int mid = (L + R) >> 1; a[i].l = L, a[i].r = R; if (L == R) {a[i].s = a[j].s + 1; return;} if (x <= mid) a[i].rc = a[j].rc, add_tree(a[i].lc = ++m, a[j].lc, L, mid, x); else a[i].lc = a[j].lc, add_tree(a[i].rc = ++m, a[j].rc, mid+1, R, x); a[i].s = a[a[i].lc].s + a[a[i].rc].s; } inline int query(int i, int j, int ql, int qr, int k) { int L = a[i].l, R = a[i].r; int mid = (L + R) >> 1; if (L == R) return L; if ((a[a[i].lc].s - a[a[j].lc].s) >= k) query(a[i].lc, a[j].lc, ql, qr, k); else query(a[i].rc, a[j].rc, ql, qr, k-(a[a[i].lc].s - a[a[j].lc].s)); } inline void write(int i) { printf("%d : %d %d %d %d %d\n", i, a[i].l, a[i].r, a[i].lc, a[i].rc, a[i].s); if (a[i].lc) write(a[i].lc); if (a[i].rc) write(a[i].rc); } int main() { cin >> n >> T_T; rep(i, 1, n) scanf("%d", &f[i].X), f[i].Y = i; discretization(); head = 1, tail = maxvalue; build_tree(m = root[1] = 1, head, tail, num[1]); rep(i, 2, n) add_tree(root[i] = ++m, root[i-1], head, tail, num[i]); while (T_T--) { int tl, tr, tk; scanf("%d%d%d", &tl, &tr, &tk); cout << hash[query(root[tr], root[tl-1], head, tail, tk)] << endl; } return 0; }
kyeremal-poj2104-K-th Number-主席树
原文地址:http://blog.csdn.net/kyeremal/article/details/45873291