ZOJ 2112 Dynamic Rankings(主席树の动态kth)
题目链接: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)的更多相关文章
- zoj 2112 Dynamic Rankings(主席树&动态第k大)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- ZOJ -2112 Dynamic Rankings 主席树 待修改的区间第K大
Dynamic Rankings 带修改的区间第K大其实就是先和静态区间第K大的操作一样.先建立一颗主席树, 然后再在树状数组的每一个节点开线段树(其实也是主席树,共用节点), 每次修改的时候都按照树 ...
- ZOJ 2112 Dynamic Rankings(树状数组+主席树)
题意 \(n\) 个数,\(m\) 个操作,每次操作修改某个数,或者询问某个区间的第 \(K\) 小值. \(1 \leq n \leq 50000\) \(1 \leq m \leq 10000\) ...
- ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解
题意:求区间第k小,节点可修改 思路:如果直接用静态第k小去做,显然我更改一个节点后,后面的树都要改,这个复杂度太高.那么我们想到树状数组思路,树状数组是求前缀和,那么我们可以用树状数组套主席树,求出 ...
- 主席树[可持久化线段树](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 ...
- ZOJ 2112 Dynamic Rankings(动态区间第 k 大+块状链表)
题目大意 给定一个数列,编号从 1 到 n,现在有 m 个操作,操作分两类: 1. 修改数列中某个位置的数的值为 val 2. 询问 [L, R] 这个区间中第 k 大的是多少 n<=50,00 ...
- 整体二分(SP3946 K-th Number ZOJ 2112 Dynamic Rankings)
SP3946 K-th Number (/2和>>1不一样!!) #include <algorithm> #include <bitset> #include & ...
- 整体二分&cdq分治 ZOJ 2112 Dynamic Rankings
题目:单点更新查询区间第k大 按照主席树的思想,要主席树套树状数组.即按照每个节点建立主席树,然后利用树状数组的方法来更新维护前缀和.然而,这样的做法在实际中并不能AC,原因即卡空间. 因此我们采用一 ...
- ZOJ 2112 Dynamic Rankings (动态第 K 大)(树状数组套主席树)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
随机推荐
- Cloudera CDH 、Impala本地通过Parcel安装配置详解及什么是Parcel
本文引用自:Cloudera CDH .Impala本地通过Parcel安装配置详解及什么是Parcelhttp://www.aboutyun.com/forum.php?mod=viewthread ...
- 关于HIVE的配置
一:安装配置hive 1.检测hadoop 2.解压hive 3.修改环境 sudo vi /etc/profile 4.source以下 5.复制hive-env.sh 6.编辑hive-env.s ...
- 【android学习4】Eclipse中Clean作用
今天修改Servlet中代码,重启服务端程序之后发现没有启作用,于是Clean了一把,果然生效. 查阅资料得知,Eclipse中是根据时间戳去编译代码,如果某个类对应的时间戳没有发生改变就不会重新编译 ...
- Nagios Looking Glass 本地文件包含漏洞
漏洞名称: Nagios Looking Glass 本地文件包含漏洞 CNNVD编号: CNNVD-201310-682 发布时间: 2013-10-31 更新时间: 2013-10-31 危害等级 ...
- 10 Golden Rules of Project Risk Management
The benefits of risk management in projects are huge. You can gain a lot of money if you deal with u ...
- OProfile 性能分析工具
OProfile 性能分析工具 官方网站:http://oprofile.sourceforge.net/news/ oprofile.ko模块本文主要介绍Oprofile工具,适用系统的CPU性能分 ...
- Kafka+Storm+HDFS整合实践
在基于Hadoop平台的很多应用场景中,我们需要对数据进行离线和实时分析,离线分析可以很容易地借助于Hive来实现统计分析,但是对于实时的需求Hive就不合适了.实时应用场景可以使用Storm,它是一 ...
- saltstack故障解决
[root@saltstack_s ~]# salt '*' test.pingSalt request timed out. The master is not responding. If thi ...
- Power-BI 报表常用功能自适应设置
Power-BI 报表可以跨平台浏览,并自适应多种屏幕大小.在Power-BI 的开发界面下,就有多个属性用于设定在不同屏幕报表的展现方式,以达到更优的用户体验. 1.PC布局:设定报表在PC机上的布 ...
- MongoDB操作
创建.删除数据库 格式 use DATABASE_NAME 如果不存在,则创建,否则直接切换到该数据库 显示当前所在的数据库 db 显示所有数据库 show dbs 删除数据库 db.dropData ...