zoj2112 主席树动态第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大。
如果是不修改,直接主席树就可以了。
要修改要套如树状数组求和。
/* ***********************************************
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 ;
}
zoj2112 主席树动态第k大 (主席树&&树状数组)的更多相关文章
- zoj2112 主席树动态第k大 ( 参考资料链接)
参考链接: http://blog.csdn.net/acm_cxlove/article/details/8565309 http://www.cnblogs.com/Rlemon/archive/ ...
- 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 Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- zoj 2112 Dynamic Rankings 动态第k大 线段树套Treap
Dynamic Rankings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...
- 静态区间第k大(归并树)
POJ 2104为例 思想: 利用归并排序的思想: 建树过程和归并排序类似,每个数列都是子树序列的合并与排序. 查询过程,如果所查询区间完全包含在当前区间中,则直接返回当前区间内小于所求数的元素个数, ...
- zoj 2112 Dynamic Rankings(主席树&动态第k大)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- PAT天梯赛练习题 L3-002. 堆栈(线段树查询第K大值或主席树)
L3-002. 堆栈 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家都知道“堆栈”是一种“先进后出”的线性结构,基本操作有 ...
- POJ 2104 K-th Number 主席树(区间第k大)
题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...
随机推荐
- svn merge当主干修改后合并分支
例如版本r1的主干创建分支r2,在r2上修改后得到r3,r1之后也修改得到r4,现在合并分支到主干上: 如果r3的修改和r4有冲突会提示出现冲突,因此不用担心主干合并后会被分支操作覆盖,因为这并不是简 ...
- 迅为iMX6Q/PLUS开发板烧写设备树内核 Qt 系统
迅为iMX6Q 和 iMX6PLUS 两个硬件版本,设备树镜像的烧写方法以及镜像所在目录,镜像名称全部一致. 如果用的是 iMX6Q 版本,想要烧写设备树版本镜像,请使用 iMX6Q 设备树版本的光盘 ...
- Objective-C中关于NSArray, NSDictionary, NSNumber等写法的进化
从xcode4.4开始,LLVM4.0编译器为Objective-C添加一些新的特性.创建数组NSArray,哈希表NSDictionary, 数值对象NSNumber时,可以像NSString的初始 ...
- Linux 标准 I/O 库
为什么要设计标准 I/O 库? 直接使用 API 进行文件访问时,需要考虑许多细节问题,例如:read . write 时,缓冲区的大小该如何确定,才能使效率最优 read 和 write 等底层系统 ...
- C#在透明窗体WinForm上面画图(电子尺小工具的实现)
前几天要做一个微信调一调的外挂,里面用到了尺子测量距离,然后就自己下载了一个电子尺,最近要升级我的跳一跳外挂,然后就准备自己做一个电子尺,嵌入到我的外挂里面,在嵌入到我的外挂之前,我自己做了一个完整版 ...
- Python使用三种方法实现PCA算法[转]
主成分分析(PCA) vs 多元判别式分析(MDA) PCA和MDA都是线性变换的方法,二者关系密切.在PCA中,我们寻找数据集中最大化方差的成分,在MDA中,我们对类间最大散布的方向更感兴趣. 一句 ...
- 【dp 状态压缩 单调栈】bzoj3591: 最长上升子序列
奇妙的单调栈状压dp Description 给出1~n的一个排列的一个最长上升子序列,求原排列可能的种类数. Input 第一行一个整数n. 第二行一个整数k,表示最长上升子序列的长度. 第三行k个 ...
- Linux基础学习-数据备份工具Rsync
数据备份工具rsync 作为一个系统管理员,数据备份是非常重要的,如果没有做好备份策略,磁盘损坏了,那么你的数据将全部丢失,所以在日常的维护工作中,一定要时刻牢记给数据做备份. rsync不仅可以可以 ...
- 【STL】栈+队列+优先队列(详)+ 拯救行动题解
一.栈 栈(stack)又名堆栈,它是一种运算受限的线性表.其限制是仅允许在表的一端进行插入和删除运算.这一端被称为栈顶,相对地,把另一端称为栈底.向一个栈插入新元素又称作进栈.入栈或压栈,它是把新元 ...
- 数据结构( Pyhon 语言描述 ) — — 第3章:搜索、排序和复杂度分析
评估算法的性能 评价标准 正确性 可读性和易维护性 运行时间性能 空间性能(内存) 度量算法的运行时间 示例 """ Print the running times fo ...