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

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.

题目大意:给n个数,有m个操作。修改某个数,或者询问一段区间的第k小值。

思路:首先要知道没有修改操作的区间第k大怎么用出席树写:POJ 2104 K-th Number(主席树——附讲解)

至于动态kth的解法可以去看CLJ的《可持久化数据结构研究》(反正我是看这个才懂的),然后在网上查一些资料,YY一下就可以了。

这里写的是树状数组套线段树+可持久化线段树的做法(因为内存不够用)。

简单的讲就是通过树状数组求和,维护前k个线段树的和。时间复杂度为O(nlogn+m(logn)^2)

另参考资料(里面有好几个link :)):http://www.cnblogs.com/kuangbin/p/3308118.html

PS:ZOJ的指针似乎不是4个字节的。这里用指针就要MLE了(代码本来不是这么丑的啊T_T)。

代码(130MS):

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL; const int MAXN = ;
const int MAXM = ;
const int MAXT = MAXM * * ; struct Query {
char op;
int i, j, k;
void read() {
scanf(" %c", &op);
if(op == 'Q') scanf("%d%d%d", &i, &j, &k);
else scanf("%d%d", &i, &k);
}
} query[MAXM];
int a[MAXN];
int n, m, T;
//hashmap
int arr[MAXN + MAXM], total; void buildHash() {
total = ;
for(int i = ; i <= n; ++i) arr[total++] = a[i];
for(int i = ; i <= m; ++i)
if(query[i].op == 'C') arr[total++] = query[i].k;
sort(arr, arr + total);
total = unique(arr, arr + total) - arr;
} int hash(int x) {
return lower_bound(arr, arr + total, x) - arr;
}
//Chairman tree
struct Node {
int lson, rson, sum;
void clear() {
lson = rson = sum = ;
}
} tree[MAXT];
int root[MAXN];
int tcnt; void insert(int& rt, int l, int r, int pos) {
tree[tcnt] = tree[rt]; rt = tcnt++;
tree[rt].sum++;
if(l < r) {
int mid = (l + r) >> ;
if(pos <= mid) insert(tree[rt].lson, l, mid, pos);
else insert(tree[rt].rson, mid + , r, pos);
}
} void buildTree() {
tcnt = ;
for(int i = ; i <= n; ++i) {
root[i] = root[i - ];
insert(root[i], , total - , hash(a[i]));
}
}
//Binary Indexed Trees
int root2[MAXN];
int roota[], rootb[];
int cnta, cntb; void initBIT() {
for(int i = ; i <= n; ++i) root2[i] = ;
} inline int lowbit(int x) {
return x & -x;
} void insert(int& rt, int l, int r, int pos, int val) {
if(rt == ) tree[rt = tcnt++].clear();
if(l == r) {
tree[rt].sum += val;
} else {
int mid = (l + r) >> ;
if(pos <= mid) insert(tree[rt].lson, l, mid, pos, val);
else insert(tree[rt].rson, mid + , r, pos, val);
tree[rt].sum = tree[tree[rt].lson].sum + tree[tree[rt].rson].sum;
}
} int kth(int ra, int rb, int l, int r, int k) {
if(l == r) {
return l;
} else {
int sum = tree[tree[rb].lson].sum - tree[tree[ra].lson].sum, mid = (l + r) >> ;
for(int i = ; i < cntb; ++i) sum += tree[tree[rootb[i]].lson].sum;
for(int i = ; i < cnta; ++i) sum -= tree[tree[roota[i]].lson].sum;
if(sum >= k) {
for(int i = ; i < cntb; ++i) rootb[i] = tree[rootb[i]].lson;
for(int i = ; i < cnta; ++i) roota[i] = tree[roota[i]].lson;
return kth(tree[ra].lson, tree[rb].lson, l, mid, k);
} else {
for(int i = ; i < cntb; ++i) rootb[i] = tree[rootb[i]].rson;
for(int i = ; i < cnta; ++i) roota[i] = tree[roota[i]].rson;
return kth(tree[ra].rson, tree[rb].rson, mid + , r, k - sum);
}
}
}
//Main operation
void modify(int k, int val) {
int x = hash(a[k]), y = hash(val);
a[k] = val;
while(k <= n) {
insert(root2[k], , total - , x, -);
insert(root2[k], , total - , y, );
k += lowbit(k);
}
} int kth(int l, int r, int k) {
cnta = cntb = ;
for(int i = l - ; i; i -= lowbit(i)) roota[cnta++] = root2[i];
for(int i = r; i; i -= lowbit(i)) rootb[cntb++] = root2[i];
return kth(root[l - ], root[r], , total - , k);
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
for(int i = ; i <= m; ++i) query[i].read();
buildHash();
buildTree();
initBIT();
for(int i = ; i <= m; ++i) {
if(query[i].op == 'Q') printf("%d\n", arr[kth(query[i].i, query[i].j, query[i].k)]);
else modify(query[i].i, query[i].k);
}
}
}

ZOJ 2112 Dynamic Rankings(主席树の动态kth)的更多相关文章

  1. zoj 2112 Dynamic Rankings(主席树&amp;动态第k大)

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

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

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

  3. ZOJ 2112 Dynamic Rankings(树状数组+主席树)

    题意 \(n\) 个数,\(m\) 个操作,每次操作修改某个数,或者询问某个区间的第 \(K\) 小值. \(1 \leq n \leq 50000\) \(1 \leq m \leq 10000\) ...

  4. ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解

    题意:求区间第k小,节点可修改 思路:如果直接用静态第k小去做,显然我更改一个节点后,后面的树都要改,这个复杂度太高.那么我们想到树状数组思路,树状数组是求前缀和,那么我们可以用树状数组套主席树,求出 ...

  5. 主席树[可持久化线段树](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 ...

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

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

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

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

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

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

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

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

随机推荐

  1. 深入了解Windows句柄到底是什么

    深入了解Windows句柄到底是什么 http://blog.csdn.net/wenzhou1219/article/details/17659485 总是有新入门的Windows程序员问我Wind ...

  2. HD 1003 Max Sum 的递归解法

    #include <STDIO.H> typedef struct SU_tag{ SU_tag(){} SU_tag(int a,int b,int c):max_sum(a),left ...

  3. Linux Socket过程详细解释(包括三次握手建立连接,四次握手断开连接)

    我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览器浏览网页时,浏览器的进程怎么与web 服务器通信的?当你用QQ聊天时,QQ进程怎么与服务器或你好友所在的QQ进程通信?这些都得靠s ...

  4. SQL Server xtype

    sysobjects 表 在数据库内创建的每个对象(约束.默认值.日志.规则.存储过程等)在表中占一行.只有在 tempdb 内,每个临时对象才在该表中占一行. 列名 数据类型 描述 name sys ...

  5. iOS 增加UIButton按钮的可点击区域

    在很多时候,按钮可能看起来那么大,但是在它周围进行点击时,都能够触发事件,是因为它的可点击区域比我们看到的button要大. 在使用AutoLayout的时候,我们处理的是按钮的image属性,所以这 ...

  6. Java学习-008-判断文件类型实例

    此文源码主要为应用 Java 如何判断文件类型的源码及其测试源码.若有不足之处,敬请大神指正,不胜感激!源代码测试通过日期为:2015-2-2 23:02:00,请知悉. Java 判断文件类型源码如 ...

  7. bootstrap响应式布局简单实例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  8. Linux就这个范儿 第18章 这里也是鼓乐笙箫 Linux读写内存数据的三种方式

    Linux就这个范儿 第18章  这里也是鼓乐笙箫  Linux读写内存数据的三种方式 P703 Linux读写内存数据的三种方式 1.read  ,write方式会在用户空间和内核空间不断拷贝数据, ...

  9. Android Service和Thread的关系

    不少Android初学者都可能会有这样的疑惑,Service和Thread到底有什么关系呢?什么时候应该用Service,什么时候又应该用Thread?答案可能会有点让你吃惊,因为Service和Th ...

  10. Android --账户注销

    参考博客:android如何实现注销功能 Intent logoutIntent=new Intent(SettingActivity.this,LoginActivity.class); //在执行 ...