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
主席树太神了。
这题是动态第k大。
如果是不修改,直接主席树就可以了。
要修改要套如树状数组求和。
参考链接:
http://blog.csdn.net/acm_cxlove/article/details/8565309
http://www.cnblogs.com/Rlemon/archive/2013/05/24/3096264.html
http://seter.is-programmer.com/posts/31907.html
http://blog.csdn.net/metalseed/article/details/8045038
/* ***********************************************
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 ;
}
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+ ...
- 【BZOJ 1901】【Zju 2112】 Dynamic Rankings 动态K值 树状数组套主席树模板题
达神题解传送门:http://blog.csdn.net/dad3zz/article/details/50638360 说一下我对这个模板的理解: 看到这个方法很容易不知所措,因为动态K值需要套树状 ...
随机推荐
- React 学习二 组件
React的一个最大的特点就是组件化的开发模式.今天就来试一下: <!DOCTYPE html> <html> <head> <meta charset=&q ...
- windows 下的一些常用命令提示符
windows下dos命令窗口输入 netstat -ano即可查看端口使用情况, 如果要查看指定端口是否被占用 使用命令netstat -ano|findstr 端口号, 例如要查看8080端口号是 ...
- **PHP转义Json里的特殊字符的函数
http://www.banghui.org/11332.html 在给一个 App 做 API,从服务器端的 MySQL 取出数据,然后生成 JSON.数据中有个字段叫 content,里面保存了文 ...
- python处理汉字转拼音pypinyin
主要是pypinyin 包,官网: http://pypinyin.readthedocs.io/zh_CN/master/index.html jieba包,主要是用来分词的,我之前的博文有介绍:h ...
- Vue.js—快速入门及实现用户信息的增删
Vue.js是什么 Vue.js 是一套构建用户界面的渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计.Vue 的核心库只关注视图层,它不仅易于上手,还便于与第三方库或既有项目 ...
- LoadRunner中log的使用总结
LoadRunner中log的使用总结 1.log的设置方式. 在 runtime setting中可以设置log的生成方式: 默认的log方式: Enable logging选中,log optio ...
- Mendeley文献管理软件使用介绍
<!DOCTYPE html> New Document /* GitHub stylesheet for MarkdownPad (http://markdownpad.com) / / ...
- react篇章-React State(状态)-组件都是真正隔离的
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- Eclipse daemon not running. starting it now on port ***的
daemon not running. starting it now on port ***的 1) 运行 cmd,进入命令行2) 输入 netstat -ano ,找出占用端口***(port * ...
- 【原创】获取MySQL crash 时的core file
最近有台服务器的MySQL经常crash,为了进一步定位问题,开启了mysql core file功能,开启步骤如下,供参考 [开启步骤] 1. my.cnf文件中增加2个配置选项 [mysqld] ...