Title Link: BZOJ-1901
Problem analysis
This problem can be solved by tree array or line segment tree.
The first layer is the interval and the second layer is the weight value.
Both spatial complexity and time complexity are O (n log n).
Code
Tree array nested segment tree
#include <iostream> #include <cstdlib> #include <cstdio> #include <cmath> #include < Algorithm> #include <cstring>using namespace std;const int MAXN = 10000 + 5, MN = 1000000015, Maxnode = 10000 * 30 * + 15;int N, M, Index, Used_index;int A[MAXN], ROOT[MAXN], son[maxnode][2], T[maxnode], U[MAXN], c[maxn];void Add (int &x, int s, int t, int Pos, int Num) {if (x = = 0) x = ++index; T[X] + = num;if (s = = t) return;int m = (s + t) >> 1;if (POS <= m) Add (Son[x][0], S, M, Pos, Num); else Add (son[x][ 1], M + 1, T, Pos, Num); }void Change (int x, int. Pos, int Num) {for (int i = x; i <= n; i + = i & i) Add (Root[i], 0, MN, Pos, Num);} int get_sum (int x) {int ret = 0;for (int i = x; i; i-= i & i) ret + = T[son[u[i]][0]];return ret;} void Init_u (int x) {for (int i = x; i; i-= i & i) u[i] = Root[i];} void Turn (int x, int f) {for (int i = x; i; i-= i & i) {if (c[i] = = Used_index) break; C[i] = Used_index; U[i] = son[u[i]][f];}} int main () {scanf ("%d%d ", &n, &m); for (int i = 1; I <= n; ++i) {scanf ("%d ", &a[i]); Change (i, A[i], 1);} Char F;int Pos, Num, L, R, K, temp;for (int i = 1; I <= m; ++i) {f = '-'; while (F < ' A ' | | f > ' Z ') F = GetChar () if (f = = ' C ') {scanf ("%d%d", &pos, &num); Change (Pos, A[pos],-1); A[pos] = Num; Change (Pos, Num, 1);} else {scanf ("%d%d%d", &l, &r, &k); int L, r, mid;l = 0; r = Mn;init_u (L-1); Init_u (R); Used_index = 0;while (L < r) {mid = (L + r) >> 1; temp = Get_sum (r)-Get_sum (L-1); ++used_index;if (temp >= k) {R = mid; Turn (L-1, 0); Turn (R, 0);} else {L = mid + 1;k-= Temp; Turn (L-1, 1); Turn (R, 1);}} printf ("%d\n", L);}} return 0;}
[Bzoj 1901] Dynamic rankings "Tree array set segment Tree | | Line segment Tree Set segment tree "