[ZJU 2112] Dynamic Rankings
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=1901
[算法]
首先 , 考虑没有修改操作
不妨建立可持久化线段树 , 第i棵树维护区间[1 , i]中的数的出现个数 , 则可以通过在线段树上二分的方式求出答案
那么 , 若有修改操作 , 我们不妨使用树状数组套可持久化线段树
树状数组中的第i个元素为一棵可持久化线段树 , 代表区间[i , i - lowbit(i) + 1]每个数的出现次数
询问时可以同样二分 , 只需用最多log(N)棵线段树作差即可
时间复杂度 : O(NlogN ^ 2)
[代码]
#include<bits/stdc++.h> using namespace std; #define MAXN 200010 #define MAXP 5000010 #define MAXLOG 20 typedef long long ll; typedef unsigned long long ull; typedef long double ld; int n , m , len , L , R; int a[MAXN] , root[MAXN] , val[MAXN] , x[MAXN] , y[MAXN] , l[MAXN] , r[MAXN] , k[MAXN] , ql[MAXLOG] , qr[MAXLOG]; char type[MAXN][5]; struct Presitent_Segment_Tree { int sz; int sum[MAXP] , lson[MAXP] , rson[MAXP]; Presitent_Segment_Tree() { sz = 0; memset(root , 0 , sizeof(root)); memset(lson , 0 , sizeof(lson)); memset(rson , 0 , sizeof(rson)); } inline void modify(int &k , int old , int l , int r , int pos , int value) { k = ++sz; lson[k] = lson[old] , rson[k] = rson[old]; sum[k] = sum[old] + value; if (l == r) return; int mid = (l + r) >> 1; if (mid >= pos) modify(lson[k] , lson[k] , l , mid , pos , value); else modify(rson[k] , rson[k] , mid + 1 , r , pos , value); } inline int query(int l , int r , int k) { int cnt = 0; if (l == r) return l; for (int i = 1; i <= L; i++) cnt -= sum[lson[ql[i]]]; for (int i = 1; i <= R; i++) cnt += sum[lson[qr[i]]]; int mid = (l + r) >> 1; if (cnt >= k) { for (int i = 1; i <= L; i++) ql[i] = lson[ql[i]]; for (int i = 1; i <= R; i++) qr[i] = lson[qr[i]]; return query(l , mid , k); } else { for (int i = 1; i <= L; i++) ql[i] = rson[ql[i]]; for (int i = 1; i <= R; i++) qr[i] = rson[qr[i]]; return query(mid + 1 , r , k - cnt); } } } PST; struct Binary_Indexed_Tree { inline int lowbit(int x) { return x & (-x); } inline void modify(int pos , int x , int val) { for (int i = pos; i <= n; i += lowbit(i)) PST.modify(root[i] , root[i] , 1 , len , x , val); } inline int query(int l , int r , int k) { L = 0 , R = 0; for (int i = l - 1; i; i -= lowbit(i)) ql[++L] = root[i]; for (int i = r; i; i -= lowbit(i)) qr[++R] = root[i]; return PST.query(1 , len , k); } } BIT; template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); } template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); } template <typename T> inline void read(T &x) { T f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } int main() { scanf("%d%d" , &n , &m); len = n; for (int i = 1; i <= n; i++) { scanf("%d" , &a[i]); val[i] = a[i]; } for (int i = 1; i <= m; i++) { scanf("%s" , type[i]); if (type[i][0] == 'C') { scanf("%d%d" , &x[i] , &y[i]); val[++len] = y[i]; } else scanf("%d%d%d" , &l[i] , &r[i] , &k[i]); } sort(val + 1 , val + len + 1); len = unique(val + 1 , val + len + 1) - val - 1; for (int i = 1; i <= n; i++) a[i] = lower_bound(val + 1 , val + len + 1 , a[i]) - val; for (int i = 1; i <= m; i++) if (type[i][0] == 'C') y[i] = lower_bound(val + 1 , val + len + 1 , y[i]) - val; for (int i = 1; i <= n; i++) BIT.modify(i , a[i] , 1); for (int i = 1; i <= m; i++) { if (type[i][0] == 'C') { BIT.modify(x[i] , a[x[i]] , -1); BIT.modify(x[i] , y[i] , 1); a[x[i]] = y[i]; } else printf("%d\n" , val[BIT.query(l[i] , r[i] , k[i])]); } return 0; }