The same is to insert and look for the K-large, here because the interval does not exist in the case, so you can now sort the interval and then directly to the line. If there is a case of inclusion, you can only go to the chairman tree or the MO team algorithm.
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < Cstdlib>using namespace std;struct Node {node *ch[2];int rkey, size, Val; Node (int val): Val (val), size (1) {rkey = rand (); ch[0] = ch[1] = NULL;} void maintain () {size = 1;if (ch[0]! = null) size + = Ch[0]->size;if (ch[1]! = null) size + = ch[1]->size;}}; void Rotate (node *&x, int D) {node *k = x->ch[d ^ 1];x->ch[d ^ 1] = K->ch[d];k->ch[d] = X;x->maintain () ; K->maintain (); x = k;} void Insert (Node *&o, int x) {if (o = = NULL) o = new Node (x); else {int d = x > O->val;insert (o->ch[d], x); if (O->ch[d]->rkey > O->rkey) {Rotate (o, D ^ 1);}} O->maintain ();} void Remove (node *&o, int x) {if (O->val = = x) {if (o->ch[0] = = NULL | | o->ch[1] = = null) {node *k = o;if (o >ch[0] = = NULL) o = o->ch[1];else o = o->ch[0];d elete K;} else {int d = o->ch[0]->rkey > O->ch[1]->rkey;rotate (o, D); remove (O->ch[d], X);}} else {int d = x > O->val;remove (o->ch[d], x);}} int findkth (Node *o, int k) {if (o = = NULL | | k > O->size | | k <= 0) return 0;int lsize = o->ch[0] = = null? 0 : O->ch[0]->size;if (k <= lsize) return findkth (O->ch[0], k); else if (k = = lsize + 1) return O->val;else R Eturn findkth (o->ch[1], k-lsize-1);} void Remove_tree (Node *&o) {if (o = = null) return;if (o->ch[0]! = null) Remove_tree (o->ch[0]); if (o->ch[1]! = NULL) Remove_tree (o->ch[1]);d elete (o); o = NULL;} struct Seg {int L, r, K, id; Seg (int l, int r, int k, int id): L (L), R (R), K (k), ID (ID) {}seg () {}bool operator < (const SEG &x) Const {if (r = = X.R) return L < X.l;return R < X.R;}}; const int MAXN = 1e6 + 10; Seg QUERY[MAXN]; Node *treap;int ANS[MAXN], N, M, A[maxn];int main () {while (scanf ("%d%d", &n, &m)! = EOF) {for (int i = 1; I <= N i++) {scanf ("%d", &a[i]);} for (int i = 1; I <= m; i++) {int L, r, K; scanf ("%d%d%d", &l, &r, &k); Query[i] = Seg (L, R, K, I);} Sort (query + 1, query + 1 + m), int nowl = 1, Nowr = 1;remove_tree (Treap), for (int i = 1; I <= m; i++) {while (Nowr < = QUERY[I].R) {insert (treap, a[nowr++]);} while (Nowl < QUERY[I].L) {remove (treap, a[nowl++]);} Ans[query[i].id] = findkth (treap, QUERY[I].K);} for (int i = 1; I <= m; i++) printf ("%d\n", Ans[i]);} return 0;}
POJ 2761 Feed The Dogs Foundation Treap