ZOJ 2112 Dynamic Rankings (动态第 K 大)(树状数组套主席树)
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
表示不是很懂,先插个眼。。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define LL long long
#define pii (pair<int, int>)
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; //zoj 2104 动态主席树修改+静态主席树
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 d) {
while(x <= n) {
S[x] = Insert(S[x], pos, d);
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-], 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;
} int main() {
//freopen("input.txt", "r", stdin);
int Tcase; cin >> 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 {
add(query[i].l, Hash(a[query[i].l]), -);
add(query[i].l, Hash(query[i].r), );
a[query[i].l] = query[i].r;
}
}
}
return ;
}
ZOJ 2112 Dynamic Rankings (动态第 K 大)(树状数组套主席树)的更多相关文章
- zoj 2112 Dynamic Rankings 动态第k大 线段树套Treap
Dynamic Rankings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...
- ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解
题意:求区间第k小,节点可修改 思路:如果直接用静态第k小去做,显然我更改一个节点后,后面的树都要改,这个复杂度太高.那么我们想到树状数组思路,树状数组是求前缀和,那么我们可以用树状数组套主席树,求出 ...
- P2617 Dynamic Rankings(树状数组套主席树)
P2617 Dynamic Rankings 单点修改,区间查询第k大 当然是无脑树套树了~ 树状数组套主席树就好辣 #include<iostream> #include<cstd ...
- LUOGU P2617 Dynamic Rankings(树状数组套主席树)
传送门 解题思路 动态区间第\(k\)大,树状数组套主席树模板.树状数组的每个位置的意思的是每棵主席树的根,维护的是一个前缀和.然后询问的时候\(log\)个点一起做前缀和,一起移动.时空复杂度\(O ...
- [COGS257]动态排名系统 树状数组套主席树
257. 动态排名系统 时间限制:5 s 内存限制:512 MB [问题描述]给定一个长度为N的已知序列A[i](1<=i<=N),要求维护这个序列,能够支持以下两种操作:1.查询A[ ...
- BZOJ1901 - Dynamic Rankings(树状数组套主席树)
题目大意 给定一个有N个数字的序列,然后又m个指令,指令种类只有两种,形式如下: Q l r k 要求你查询区间[l,r]第k小的数是哪个 C i t 要求你把第i个数修改为t 题解 动态的区间第k ...
- 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树状数组套主席树)
http://www.lydsy.com/JudgeOnline/problem.php?id=1901 首先还是吐槽时间,我在zoj交无限tle啊!!!!!!!!我一直以为是程序错了啊啊啊啊啊啊. ...
- 【树状数组套主席树】带修改区间K大数
P2617 Dynamic Rankings 题目描述给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+ ...
- ZOJ 2112 Dynamic Rankings (动态第k大,树状数组套主席树)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- 【BZOJ 1901】【Zju 2112】 Dynamic Rankings 动态K值 树状数组套主席树模板题
达神题解传送门:http://blog.csdn.net/dad3zz/article/details/50638360 说一下我对这个模板的理解: 看到这个方法很容易不知所措,因为动态K值需要套树状 ...
随机推荐
- php: Can't use function return value in write context
关于empty()函数, php手册中提到,php5.5之前empty()函数只支持检查变量,传入任何其他的表达式或函数都会产生语法错误. Note: Prior to PHP 5.5, empty( ...
- ext大法好啊
http://www.cnblogs.com/keshuqi/p/6257895.html
- 安卓下拉刷新空间SwipeRefreshLayout的基本使用
1.先写布局文件 <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/refresh" androi ...
- git上传本地项目
1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点 ...
- im4java学习----查看文档和test用例
im4java下载地址:http://sourceforge.net/projects/im4java/files/(谷歌搜索出来的第一个官方地址打不开) 我们需要下载bin和src 这2个压缩包. ...
- RPC-整体概念
RPC概述 RPC(Remote Procedure Call),即远程过程调用,是一种通过网络从远程计算机程序上请求服务而不需要了解底层网络技术的协议,实现调用远程主机上的方法就像调用本地方法一样. ...
- bzoj1855: [Scoi2010]股票交易 单调队列优化dp ||HDU 3401
这道题就是典型的单调队列优化dp了 很明显状态转移的方式有三种 1.前一天不买不卖: dp[i][j]=max(dp[i-1][j],dp[i][j]) 2.前i-W-1天买进一些股: dp[i][j ...
- 分布式缓存Memcache
Memcached是分布式的,也就是说它不是本地的.它基于网络连接(当然它也可以使用localhost)方式完成服务,本身它是一个独立于应用的程序或守护进程(Daemon方式). Memcached使 ...
- HDU 1162 Eddy's picture (最小生成树 普里姆 )
题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...
- [FZU2254]英语考试
在过三个礼拜,YellowStar有一场专业英语考试,因此它必须着手开始复习. 这天,YellowStar准备了n个需要背的单词,每个单词的长度均为m. YellowStar准备采用联想记忆法来背诵这 ...