原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define lc root<<1
#define rc root<<1|1
#define INF 0x3f3f3f3f
const int Max_N = ;
struct SBT{
int key, size, cnt;
SBT *ch[];
inline void push_up(){
size = ch[]->size + ch[]->size + cnt;
}
int cmp(int v) const{
if (v == key) return -;
return v > key;
}
}*null, stack[Max_N * ], *ptr[Max_N << ];
int sz = , sum = , arr[Max_N];
void init(){
null = &stack[sz++];
null->key = null->size = null->cnt = ;
}
void rotate(SBT* &x, int d){
SBT *k = x->ch[!d];
x->ch[!d] = k->ch[d];
k->ch[d] = x;
k->size = x->size;
x->push_up();
x = k;
}
void Maintain(SBT* &x, int d){
if (x->ch[d] == null) return;
if (x->ch[d]->ch[d]->size > x->ch[!d]->size){
rotate(x, !d);
} else if (x->ch[d]->ch[!d]->size > x->ch[!d]->size){
rotate(x->ch[d], d), rotate(x, !d);
} else {
return;
}
Maintain(x, ), Maintain(x, );
}
void insert(SBT* &x, int key){
if (x == null){
x = &stack[sz++];
x->ch[] = x->ch[] = null;
x->key = key, x->size = x->cnt = ;
} else {
int d = x->cmp(key);
x->size++;
if (- == d){
x->cnt++;
return;
}
insert(x->ch[d], key);
x->push_up();
Maintain(x, d);
}
}
void del(SBT* &x, int key){
if (x == null) return;
int d = x->cmp(key);
x->size--;
if (- == d){
if (x->cnt > ){
x->cnt--;
} else if (x->ch[] == null || x->ch[] == null){
x = x->ch[] != null ? x->ch[] : x->ch[];
} else {
SBT *ret = x->ch[];
for (; ret->ch[] != null; ret = ret->ch[]);
del(x->ch[], x->key = ret->key);
}
} else {
del(x->ch[d], key);
}
if (x != null) x->push_up();
}
int sbt_rank(SBT *x, int key){
int t, cur;
for (t = cur = ; x != null;){
t = x->ch[]->size;
if (key < x->key) x = x->ch[];
else if (key >= x->key) cur += x->cnt + t, x = x->ch[];
}
return cur;
}
void seg_built(int root, int l, int r){
for (int i = l; i <= r; i++) insert(ptr[root], arr[i]);
if (l == r) return;
int mid = (l + r) >> ;
seg_built(lc, l, mid);
seg_built(rc, mid + , r);
}
void seg_query(int root, int l, int r, int x, int y, int val){
if (x > r || y < l) return;
if (x <= l && y >= r){
sum += sbt_rank(ptr[root], val);
return;
}
int mid = (l + r) >> ;
seg_query(lc, l, mid, x, y, val);
seg_query(rc, mid + , r, x, y, val);
}
void seg_del(int root, int l, int r, int pos, int val){
if (pos > r || pos < l) return;
del(ptr[root], val);
if (l == r) return;
int mid = (l + r) >> ;
seg_del(lc, l, mid, pos, val);
seg_del(rc, mid + , r, pos, val);
}
void seg_insert(int root, int l, int r, int pos, int val) {
if (pos > r || pos < l) return;
insert(ptr[root], val);
if (l == r) return;
int mid = (l + r) >> ;
seg_insert(lc, l, mid, pos, val);
seg_insert(rc, mid + , r, pos, val);
}
void gogo(int n, int a, int b, int k){
int l = , r = INF;
while (l < r){
sum = ;
int mid = (l + r) >> ;
seg_query(, , n, a, b, mid);
if (sum < k) l = mid + ;
else r = mid;
}
printf("%d\n", l);
}
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
char ch;
int t, n, m, a, b, k;
scanf("%d", &t);
while (t--){
sz = , init();
scanf("%d %d", &n, &m);
for (int i = ; i <= n; i++) scanf("%d", &arr[i]);
std::fill(ptr, ptr + (n << ), null);
seg_built(, , n);
while (m--){
getchar();
scanf("%c", &ch);
if (ch == 'Q'){
scanf("%d %d %d", &a, &b, &k);
gogo(n, a, b, k);
} else {
scanf("%d %d", &a, &b);
seg_del(, , n, a, arr[a]);
seg_insert(, , n, a, arr[a] = b);
}
}
}
return ;
}

zoj 2112 Dynamic Rankings的更多相关文章

  1. ZOJ 2112 Dynamic Rankings(动态区间第 k 大+块状链表)

    题目大意 给定一个数列,编号从 1 到 n,现在有 m 个操作,操作分两类: 1. 修改数列中某个位置的数的值为 val 2. 询问 [L, R] 这个区间中第 k 大的是多少 n<=50,00 ...

  2. 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )

    在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...

  3. 整体二分(SP3946 K-th Number ZOJ 2112 Dynamic Rankings)

    SP3946 K-th Number (/2和>>1不一样!!) #include <algorithm> #include <bitset> #include & ...

  4. 整体二分&cdq分治 ZOJ 2112 Dynamic Rankings

    题目:单点更新查询区间第k大 按照主席树的思想,要主席树套树状数组.即按照每个节点建立主席树,然后利用树状数组的方法来更新维护前缀和.然而,这样的做法在实际中并不能AC,原因即卡空间. 因此我们采用一 ...

  5. ZOJ 2112 Dynamic Rankings(主席树の动态kth)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2112 The Company Dynamic Rankings ...

  6. ZOJ 2112 Dynamic Rankings(带修改的区间第K大,分块+二分搜索+二分答案)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  7. ZOJ -2112 Dynamic Rankings 主席树 待修改的区间第K大

    Dynamic Rankings 带修改的区间第K大其实就是先和静态区间第K大的操作一样.先建立一颗主席树, 然后再在树状数组的每一个节点开线段树(其实也是主席树,共用节点), 每次修改的时候都按照树 ...

  8. zoj 2112 Dynamic Rankings 动态第k大 线段树套Treap

    Dynamic Rankings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...

  9. 高级数据结构(树状数组套主席树):ZOJ 2112 Dynamic Rankings

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  10. ZOJ 2112 Dynamic Rankings (动态第 K 大)(树状数组套主席树)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

随机推荐

  1. iOS 7中使用UINavigationController进行pop崩溃

    最近在一个项目中遇到一种情况,push到一个界面,如果那个界面未请求到数据,则直接pop回去,然而使用 [self.navigationController popViewControllerAnim ...

  2. NSString用法

    1 创造字符串 NSString *str1 = @"hello"; NSString *str2 = [NSString string]; NSString *str3 = [N ...

  3. MSP430F149学习之路——LED

    #include <msp430x14x.h> void int_clk(); void delay(int i); void main() { WDTCTL = WDTPW + WDTH ...

  4. note name

    谦谦君子:借用<周易·谦>:“初六,谦谦君子,用涉大川,吉.” 温润如玉:化用<诗经·卫风·淇奥>“有匪君子,如切如磋,如琢如磨”之义.

  5. 洛谷P1113 杂物

    P1113 杂务 251通过 441提交 题目提供者该用户不存在 标签图论递推 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 为什么会只有10分? 题目描述 John的农场在给奶牛挤奶前有很 ...

  6. 用命令实现Win7远程桌面关机和重启

    关机 shutdown -s -t 0 重启 shutdown -r -t 0 打开运行框(Win+R键),输入上述命令即可,后面的数字表示关机/重启延迟的时间 at 12:00 shutdown - ...

  7. 【MySQL】MySQL事务回滚脚本

    MySQL自己的 mysqlbinlog | mysql 回滚不好用,自己写个简单脚本试试: 想法是用mysqlbinlog把需要回滚的事务区域从mysql-bin.file中找到,然后通过脚本再插入 ...

  8. 在CentOS 7上安装Python3.5源码包

    最近开始系统学习Python 3.5,发现CentOS 7系统自带的python版本是Python 2.7.现在要使用Python 3.5该怎么办?方法大体跟安装其他程序一样.以下为详细经过: 1.事 ...

  9. repeater标签双重循环的使用

    在网站开发中,.NET中的repeater标签几乎是笔者首选,也是唯一一个不会生成多余元素的标签,所有样式都是自定义的,这点类似 struts中的<s:iterator/>标签. 在日常编 ...

  10. CentOS学习笔记--JDK安装

    JDK安装 大部分的服务都离不开JAVA环境,CentOS里都是OpenJDK,显然我们还是使用JAVA的JDK好.(注:minimal版没有安装OpenJDK,其它版本需要删除这个.) JDK下载 ...