It is necessary to summarize the data structures that can be persisted for the first time today.
First, we need to find out how to solve the k-th largest problem by using N line segment trees. If you want to calculate the global K, you can insert the value of the number into the line segment tree as the position, and then locate the k position through the interval and +. Because the k-th largest value is obtained through range and, obviously, the prefix and nature are satisfied. Therefore, the k-th number of the l and r-th intervals is queried, create two line segments based on the 1-l-1, 1-r intervals, and then subtract the nodes to obtain the k-th percentile. Therefore, the memory space required for this solution is n * logn (the range of the number of tubes is not required, and the range can be reduced to n through discretization ).
However, it is noted that each tree has only one node modified relative to the previous one, and at most only the logn nodes have changed. Therefore, only the modified nodes are stored, then point the pointer to the original unchanged node.
The first time I wrote this article, it felt a little cool to implement it. But not long =. =
# Include <cstdio> # include <cstring> # include <algorithm> # include <vector> using namespace std; const int maxn = 100000 + 10; const int maxm = maxn * 30; int lc [maxm], rc [maxm], sumv [maxm], cnt; int root [maxn], n, q, a [maxn]; vector <int> vnum; void init () {cnt = 1; root [0] = 0;} void build (int rt, int l, int r) {sumv [rt] = 0; if (l = r) return; int mid = (l + r)> 1; lc [rt] = cnt ++; rc [rt] = cnt ++; build (lc [rt] , L, mid); build (rc [rt], mid + 1, r);} void update1 (int rt, int prt, int l, int r, int pos, int val) {int mid = (l + r)> 1; if (l = r) return; if (pos <= mid) {int newrt = cnt ++; lc [rt] = newrt; sumv [newrt] = sumv [lc [prt] + val; rc [rt] = rc [prt]; update1 (newrt, lc [prt], l, mid, pos, val);} else {int newrt = cnt ++; rc [rt] = newrt; sumv [newrt] = sumv [rc [prt] + val; lc [rt] = lc [prt]; update1 (newrt, rc [prt], Mid + 1, r, pos, val) ;}} void update (int rt, int pos, int val) {root [rt] = cnt ++; sumv [root [rt] = sumv [root [rt-1] + val; update1 (root [rt], root [rt-1], 1, n, pos, val);} int query (int lroot, int rroot, int l, int r, int k) {int mid = (l + r)> 1, nowsum = sumv [lc [rroot]-sumv [lc [lroot]; if (l = r) return vnum [l-1]; if (nowsum> = k) return query (lc [lroot], lc [rroot], l, mid, k); else return qu Ery (rc [lroot], rc [rroot], mid + 1, r, k-nowsum);} int getid (int val) {return lower_bound (vnum. begin (), vnum. end (), val)-vnum. begin () + 1;} int main () {while (scanf ("% d", & n, & q )! = EOF) {init (); vnum. clear (); build (root [0], 1, n); for (int I = 1; I <= n; I ++) {scanf ("% d ", & a [I]); vnum. push_back (a [I]);} sort (vnum. begin (), vnum. end (); vnum. erase (unique (vnum. begin (), vnum. end (), vnum. end (); for (int I = 1; I <= n; I ++) {update (I, getid (a [I]), 1 );} while (q --) {int l, r, k; scanf ("% d", & l, & r, & k ); int ret = query (root [l-1], root [r], 1, n, k); printf ("% d \ n", ret );}} return 0 ;}
POJ 2104 K-th Number, chair tree, maximum K