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大。

如果是不修改,直接主席树就可以了。

要修改要套如树状数组求和。

 /* ***********************************************
Author :kuangbin
Created Time :2013-9-8 8:53:54
File Name :F:\2013ACM练习\专题学习\主席树\ZOJ2112.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; const int MAXN = ;
const int M = ;
int n,q,m,tot;
int a[MAXN], t[MAXN];
int T[MAXN], lson[M], rson[M],c[M];
int S[MAXN]; struct Query
{
int kind;
int l,r,k;
}query[]; void Init_hash(int k)
{
sort(t,t+k);
m = unique(t,t+k) - t;
}
int hash(int x)
{
return lower_bound(t,t+m,x)-t;
}
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 Insert(int root,int pos,int val)
{
int newroot = tot++, tmp = newroot;
int l = , r = m-;
c[newroot] = c[root] + val;
while(l < r)
{
int mid = (l+r)>>;
if(pos <= mid)
{
lson[newroot] = tot++; rson[newroot] = rson[root];
newroot = lson[newroot]; root = lson[root];
r = mid;
}
else
{
rson[newroot] = tot++; lson[newroot] = lson[root];
newroot = rson[newroot]; root = rson[root];
l = mid+;
}
c[newroot] = c[root] + val;
}
return tmp;
} int lowbit(int x)
{
return x&(-x);
}
int use[MAXN];
void add(int x,int pos,int val)
{
while(x <= n)
{
S[x] = Insert(S[x],pos,val);
x += lowbit(x);
}
}
int sum(int x)
{
int ret = ;
while(x > )
{
ret += c[lson[use[x]]];
x -= lowbit(x);
}
return ret;
}
int Query(int left,int right,int k)
{
int left_root = T[left-];
int right_root = T[right];
int l = , r = m-;
for(int i = left-;i;i -= lowbit(i)) use[i] = S[i];
for(int i = right;i ;i -= lowbit(i)) use[i] = S[i];
while(l < r)
{
int mid = (l+r)/;
int tmp = sum(right) - sum(left-) + c[lson[right_root]] - c[lson[left_root]];
if(tmp >= k)
{
r = mid;
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]];
left_root = lson[left_root];
right_root = lson[right_root];
}
else
{
l = mid+;
k -= tmp;
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]];
left_root = rson[left_root];
right_root = rson[right_root];
}
}
return l;
}
void Modify(int x,int p,int d)
{
while(x <= n)
{
S[x] = Insert(S[x],p,d);
x += lowbit(x);
}
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int Tcase;
scanf("%d",&Tcase);
while(Tcase--)
{
scanf("%d%d",&n,&q);
tot = ;
m = ;
for(int i = ;i <= n;i++)
{
scanf("%d",&a[i]);
t[m++] = a[i];
}
char op[];
for(int i = ;i < q;i++)
{
scanf("%s",op);
if(op[] == 'Q')
{
query[i].kind = ;
scanf("%d%d%d",&query[i].l,&query[i].r,&query[i].k);
}
else
{
query[i].kind = ;
scanf("%d%d",&query[i].l,&query[i].r);
t[m++] = query[i].r;
}
}
Init_hash(m);
T[] = build(,m-);
for(int i = ;i <= n;i++)
T[i] = Insert(T[i-],hash(a[i]),);
for(int i = ;i <= n;i++)
S[i] = T[];
for(int i = ;i < q;i++)
{
if(query[i].kind == )
printf("%d\n",t[Query(query[i].l,query[i].r,query[i].k)]);
else
{
Modify(query[i].l,hash(a[query[i].l]),-);
Modify(query[i].l,hash(query[i].r),);
a[query[i].l] = query[i].r;
}
}
}
return ;
}

最新文章

  1. linux命令-文件命令
  2. Security6:授予权限的思路和一般步骤
  3. PowerDesigner设计时表显示注释选项
  4. C#事件快捷设置
  5. SpringMVC学习--springmvc和mybatis整合
  6. PHP 数组的遍历的几种方式(以及foreach与for/while+each效率的比较)
  7. HDU5709 : Claris Loves Painting
  8. Oulipo (kmp)
  9. poj 1985 Cow Marathon
  10. Arduino &quot;Card failed, or not present&quot;(即找不到SD卡)错误解决方案
  11. JavaScript经典代码总结
  12. C++设计模式之建造模式
  13. Linux系统中常见的目录名称以及相应内容
  14. C# 两个datatable中的数据快速比较返回交集或差集[z]
  15. bind,apply,call,caller,callee还傻傻分不清楚?
  16. BitmapImage处理网络图片,例如阿里云获取的图片。异步加载到需要显示的控件上。提升速度非常明显。
  17. Python open 对应的参数
  18. 输入两棵二叉树A,B,判断B是不是A的子结构(c++实现)
  19. Java Collection 学习
  20. windows7下搭建python环境并用pip安装networkx

热门文章

  1. 【转】阐述Handler的实现原理
  2. cf580E. Kefa and Watch(线段树维护字符串hash)
  3. (转)RAM、ROM、SRAM、DRAM、SSRAM、SDRAM、FLASH、EEPROM的区别
  4. apache安装报错
  5. jmeter中通过beanshell访问eclipse中导出jar中的java类的方法
  6. codevs 3026 恶心的扑克
  7. swift 接水果游戏ios源码
  8. vs和github同步开发步骤
  9. javascript基本类型和引用类型,作用域和内存问题
  10. mac上使用命令行显示隐藏文件