Topic Link: Click to open the link
See the code for details:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include < cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue># Define MAX (a) > (b)? ( A):(B) #define MIN (a) < (b) ( A):(B)) using namespace Std;typedef long long ll;typedef long double ld;const ld EPS = 1e-9, PI = 3.14159265358979323846264 33832795;const int mod = 1000000000 + 7;const int INF = 0x3f3f3f3f;const int seed = 131;const ll INF64 = ll (1e18); const in T MAXN = 1e5 + 10;int n,m,cnt,root[maxn],a[maxn],x,y,k;struct Node {int L, r, sum;} T[MAXN * 40];vector<int> v;int getid (int x) {return lower_bound (V.begin (), V.end (), X)-V.begin () + 1;} void Update (int l, int r, int& x, int y, int pos) {t[++cnt] = T[y]; t[cnt].sum++; x = CNT; if (L = = r) return; int mid = (L + R) ≫> 1; If (Mid >= pos) update (L, Mid, T[x].l, T[Y].L, POS); else Update (mid+1, R, T[X].R, T[Y].R, POS);} int query (int l, int r, int x, int y, int k) {if (L = = r) return l; int mid = (L + r) >> 1; int sum = t[t[y].l].sum-t[t[x].l].sum; if (sum >= k) return query (L, Mid, t[x].l, T[Y].L, K); else return query (mid+1, R, T[X].R, T[Y].R, k-sum);} int main () {while (~scanf ("%d%d", &n,&m)) {for (int i = 1; I <= n; i++) scanf ("%d", &a[i]), V.push_ Back (A[i]); Sort (V.begin (), V.end ()); V.erase (Unique (V.begin (), V.end ()), V.end ()); for (int i = 1; I <= n; i++) update (1, N, Root[i], root[i-1], GetID (A[i])); while (m--) {scanf ("%d%d%d", &x,&y,&k); printf ("%d\n", V[query (1, N, Root[x-1],root[y], K)-1]); }} return 0;}
POJ 2104 k-th Number (Chairman tree)