ZOJ2112--Dynamic Rankings (动态区间第k大)

时间:2021-10-12 06:45:10

Dynamic Rankings


Time Limit: 10 Seconds      Memory Limit: 32768 KB

The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query like to simply find the k-th smallest number of the given N numbers. They have developed a more powerful system such that for N numbers a[1], a[2], ..., a[N], you can ask it like: what is the k-th smallest number of a[i], a[i+1], ..., a[j]? (For some i<=j, 0<k<=j+1-i that you have given to it). More powerful, you can even change the value of some a[i], and continue to query, all the same.

Your task is to write a program for this computer, which

- Reads N numbers from the input (1 <= N <= 50,000)

- Processes M instructions of the input (1 <= M <= 10,000). These instructions include querying the k-th smallest number of a[i], a[i+1], ..., a[j] and change some a[i] to t.

Input

The first line of the input is a single number X (0 < X <= 4), the number of the test cases of the input. Then X blocks each represent a single test case.

The first line of each block contains two integers N and M, representing N numbers and M instruction. It is followed by N lines. The (i+1)-th line represents the number a[i]. Then M lines that is in the following format

Q i j k or
C i t

It represents to query the k-th number of a[i], a[i+1], ..., a[j] and change some a[i] to t, respectively. It is guaranteed that at any time of the operation. Any number a[i] is a non-negative integer that is less than 1,000,000,000.

There're NO breakline between two continuous test cases.

Output

For each querying operation, output one integer to represent the result. (i.e. the k-th smallest number of a[i], a[i+1],..., a[j])

There're NO breakline between two continuous test cases.

Sample Input

2
5 3
3 2 1 4 7
Q 1 4 3
C 2 6
Q 2 5 3
5 3
3 2 1 4 7
Q 1 4 3
C 2 6
Q 2 5 3

Sample Output

3
6
3
6

主席树动态第k大基本可以手写,,但是感觉理解还不是很深。另外定义两个数组,一个用来新建一颗主席树,所有修改的结果都在这个上面进行,而另一个是记录中间值,相当于temp的效果,不改变原始数组。。(挖个坑)

附主席树专题链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=63941#overview

 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 5e4+;
int n,m,tot,idx;
ll a[maxn],vec[maxn*];
struct
{
int x,y,k,flag,idx;
} Q[maxn]; // 主席树
int lson[maxn*],rson[maxn*],c[maxn*],root[maxn]; //依次为左儿子 右儿子 线段树 根节点
int build (int l,int r)
{
int root = tot++;
c[root] = ;
if (l != r)
{
int mid = (l + r) >> ;
lson[root] = build(l,mid);
rson[root] = build(mid+,r);
}
return root;
}
int update(int root,int pos,int val)
{
int new_root = tot++;
int tmp = new_root;
int l = ,r = idx;
c[new_root] = c[root] + val;
while (l < r)
{
int mid = (l + r) >> ;
if (pos <= mid)
{
rson[new_root] = rson[root];
root = lson[root];
lson[new_root] = tot++;
new_root = lson[new_root];
r = mid;
}
else
{
lson[new_root] = lson[root];
root = rson[root];
rson[new_root] = tot++;
new_root = rson[new_root];
l = mid + ;
}
c[new_root] = c[root] + val;
}
return tmp;
}
// 树状数组维护
int s[maxn],use[maxn];
inline int lowbit (int x)
{
return x & -x;
}
void add(int k,int pos,int d)
{
while (k <= n)
{
s[k] = update(s[k],pos,d);
k += lowbit(k);
}
}
int sum(int pos)
{
int res = ;
while (pos)
{
res += c[lson[use[pos]]];
pos -= lowbit(pos);
}
return res;
}
int query(int left,int right,int k)
{
int l_root = root[left-];
int r_root = root[right];
for (int i = left-; i > ; i -= lowbit(i))
use[i] = s[i];
for (int i = right; i > ; i -= lowbit(i))
use[i] =s[i];
int l = ,r = idx;
while (l < r)
{
int t = sum(right) - sum(left-) + c[lson[r_root]] - c[lson[l_root]];
int mid = (l + r) >> ;
if (t >= k)
{
for (int i = left-; i > ; i -= lowbit(i))
use[i] = lson[use[i]];
for (int i = right; i > ; i -= lowbit(i))
use[i] = lson[use[i]];
l_root = lson[l_root];
r_root = lson[r_root];
r = mid;
}
else
{
for (int i = left-; i > ; i -= lowbit(i))
use[i] = rson[use[i]];
for (int i = right; i > ; i -= lowbit(i))
use[i] = rson[use[i]];
l_root = rson[l_root];
r_root = rson[r_root];
k -= t;
l = mid + ;
}
}
return l;
} void read()
{
scanf ("%d%d",&n,&m);
for (int i = ; i <= n; i++)
{
scanf ("%lld",a+i);
vec[idx++] = a[i];
}
for (int i = ; i < m; i++)
{
char op[];
scanf ("%s",op);
if (op[] == 'C')
{
scanf ("%d%d",&Q[i].x,&Q[i].y);
Q[i].flag = ;
vec[idx++] = Q[i].y;
}
if (op[] == 'Q')
{
scanf ("%d%d%d",&Q[i].x,&Q[i].y,&Q[i].k);
Q[i].flag = ;
}
}
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int T;
scanf ("%d",&T);
while (T--)
{
idx = tot = ;
read();
sort(vec,vec+idx); //离散化
idx = unique(vec,vec+idx) - vec;
root[] = build(,idx);
for (int i = ; i <= n; i++)
{
int tmp = lower_bound(vec,vec+idx,a[i]) - vec ;
root[i] = update(root[i-],tmp,);
}
for (int i = ; i <= n; i++)
s[i] = root[];
for (int i = ; i < m; i++)
{
if (Q[i].flag == )
{
int tmp1 = lower_bound(vec,vec+idx,a[Q[i].x]) - vec ;
int tmp2 = lower_bound(vec,vec+idx,Q[i].y) - vec ;
add(Q[i].x,tmp1,-);
add(Q[i].x,tmp2,);
a[Q[i].x] = Q[i].y;
}
if (Q[i].flag == )
{
printf("%lld\n",vec[query(Q[i].x,Q[i].y,Q[i].k)]);
}
}
}
return ;
}