ZOJ2112--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大基本可以手写,,但是感觉理解还不是很深。另外定义两个数组,一个用来新建一颗主席树,所有修改的结果都在这个上面进行,而另一个是记录中间值,相当于temp的效果,不改变原始数组。。(挖个坑)
附主席树专题链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=63941#overview
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 5e4+;
int n,m,tot,idx;
ll a[maxn],vec[maxn*];
struct
{
int x,y,k,flag,idx;
} Q[maxn]; // 主席树
int lson[maxn*],rson[maxn*],c[maxn*],root[maxn]; //依次为左儿子 右儿子 线段树 根节点
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 update(int root,int pos,int val)
{
int new_root = tot++;
int tmp = new_root;
int l = ,r = idx;
c[new_root] = c[root] + val;
while (l < r)
{
int mid = (l + r) >> ;
if (pos <= mid)
{
rson[new_root] = rson[root];
root = lson[root];
lson[new_root] = tot++;
new_root = lson[new_root];
r = mid;
}
else
{
lson[new_root] = lson[root];
root = rson[root];
rson[new_root] = tot++;
new_root = rson[new_root];
l = mid + ;
}
c[new_root] = c[root] + val;
}
return tmp;
}
// 树状数组维护
int s[maxn],use[maxn];
inline int lowbit (int x)
{
return x & -x;
}
void add(int k,int pos,int d)
{
while (k <= n)
{
s[k] = update(s[k],pos,d);
k += lowbit(k);
}
}
int sum(int pos)
{
int res = ;
while (pos)
{
res += c[lson[use[pos]]];
pos -= lowbit(pos);
}
return res;
}
int query(int left,int right,int k)
{
int l_root = root[left-];
int r_root = root[right];
for (int i = left-; i > ; i -= lowbit(i))
use[i] = s[i];
for (int i = right; i > ; i -= lowbit(i))
use[i] =s[i];
int l = ,r = idx;
while (l < r)
{
int t = sum(right) - sum(left-) + c[lson[r_root]] - c[lson[l_root]];
int mid = (l + r) >> ;
if (t >= k)
{
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]];
l_root = lson[l_root];
r_root = lson[r_root];
r = mid;
}
else
{
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]];
l_root = rson[l_root];
r_root = rson[r_root];
k -= t;
l = mid + ;
}
}
return l;
} void read()
{
scanf ("%d%d",&n,&m);
for (int i = ; i <= n; i++)
{
scanf ("%lld",a+i);
vec[idx++] = a[i];
}
for (int i = ; i < m; i++)
{
char op[];
scanf ("%s",op);
if (op[] == 'C')
{
scanf ("%d%d",&Q[i].x,&Q[i].y);
Q[i].flag = ;
vec[idx++] = Q[i].y;
}
if (op[] == 'Q')
{
scanf ("%d%d%d",&Q[i].x,&Q[i].y,&Q[i].k);
Q[i].flag = ;
}
}
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int T;
scanf ("%d",&T);
while (T--)
{
idx = tot = ;
read();
sort(vec,vec+idx); //离散化
idx = unique(vec,vec+idx) - vec;
root[] = build(,idx);
for (int i = ; i <= n; i++)
{
int tmp = lower_bound(vec,vec+idx,a[i]) - vec ;
root[i] = update(root[i-],tmp,);
}
for (int i = ; i <= n; i++)
s[i] = root[];
for (int i = ; i < m; i++)
{
if (Q[i].flag == )
{
int tmp1 = lower_bound(vec,vec+idx,a[Q[i].x]) - vec ;
int tmp2 = lower_bound(vec,vec+idx,Q[i].y) - vec ;
add(Q[i].x,tmp1,-);
add(Q[i].x,tmp2,);
a[Q[i].x] = Q[i].y;
}
if (Q[i].flag == )
{
printf("%lld\n",vec[query(Q[i].x,Q[i].y,Q[i].k)]);
}
}
}
return ;
}
ZOJ2112--Dynamic Rankings (动态区间第k大)的更多相关文章
- ZOJ2112 Dynamic Rankings 动态区间第K最值 平方分割
有了上一题的经验(POJ的静态区间第K最值)再解决这道题就轻松多了 空间5256KB,时间3330ms,如果把动态开点的平衡树换成数组模拟的话应该会更快 之所以选择了平方分割而不是树套树,不仅是所谓趁 ...
- ZOJ 1112 Dynamic Rankings【动态区间第K大,整体二分】
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112 题意: 求动态区间第K大. 分析: 把修改操作看成删除与增加 ...
- hdu5412(动态区间第k大)
CRB and Queries Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- ZOJ 2112 Dynamic Rankings(动态区间第 k 大+块状链表)
题目大意 给定一个数列,编号从 1 到 n,现在有 m 个操作,操作分两类: 1. 修改数列中某个位置的数的值为 val 2. 询问 [L, R] 这个区间中第 k 大的是多少 n<=50,00 ...
- 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树套树)
http://www.lydsy.com/JudgeOnline/problem.php?id=1901 这题调了我相当长的时间,1wa1a,我是第一次写树套树,这个是树状数组套splay,在每个区间 ...
- 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树状数组套主席树)
http://www.lydsy.com/JudgeOnline/problem.php?id=1901 首先还是吐槽时间,我在zoj交无限tle啊!!!!!!!!我一直以为是程序错了啊啊啊啊啊啊. ...
- 整体二分求动态区间第k大
比树状数组套主席树不知道高到哪里去了,solve(l,r,L,R)就是对于L,R的操作区间的答案都在l,r区间里,然后递归下去 复杂度O(nlognlogn),每个操作会执行logn次就是o(nlog ...
- Dynamic_Rankings(动态区间第k大)
ZOJ - 2112 \[ \ \] (那些说这道题是树状数组套主席树的人一定对主席树有误解!) 这里我们用树状数组套线段树来解决来写 首先 , 我们需要有n棵线段树(不是\(n^2\)空间,别慌) ...
- 动态区间第K大
整体二分. 主要需要注意的一点是,对于每个删除操作,若删除操作被算入贡献,则最开始的插入操作也一定会被算入,所以不必担心删除删错. #include<cstdio> #include< ...
随机推荐
- android Json解析详解
JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语 言的支持),从而可以在不同平台间进行数 ...
- Zend Studio 10正式版破解(2013-02-26更新)
Zend Studio 10正式版注册破解(2013-02-26完成更新) 1.以下方法仅供技术交流学习,请勿非法使用,如长期使用请支持购买正版. 2.若你还没有最新安装程序? ZendStudio ...
- C++[类设计] ini配置文件读写类config
//in Config.h #pragma once #include <windows.h> #include <shlwapi.h> #pragma comment(l ...
- Java基础知识强化78:正则表达式之获取功能(案例)
1. 获取下面这个字符串中由三个字符组成的单词. da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu? 2. ...
- 二叉树(二叉链表实现)JAVA代码
publicclassTest{ publicstaticvoid main(String[] args){ char[] ch =newchar[]{'A','B ...
- VS2013配置文件常见问题解决方法
1.C++ VS2013出现LINK : fatal error LNK1104: 无法打开文件“kernel32.lib,”错误,什么原因啊?原因:项目->XX(项目名称)属性->链接器 ...
- rpm命令数据库修复日志
今天在linux安装软件过程中遇到了一个小坑,rpm数据库被破坏: 状况: #rpm -qa | grep rpm 返回: [解决方案] 删除旧数据库,然后重建数据库: 删除旧数据库: # rm /v ...
- 大型系统开发sql优化总结(转)
Problem Description: 1.每个表的结构及主键索引情况 2.每个表的count(*)记录是多少 3.对于创建索引的列,索引的类型是什么?count(distinct indexcol ...
- linux RedHat 5 更新vim.
概述: 想装 ctags,装不上.看到老外有篇日志,是在vi 7.2版本上运行.怕是vi版本的原因,于是想升级,网上升级的方法写得少,有的写的太无语了,只有他自己看得懂.这里,简单说下.搞半天了,终于 ...
- Nginx配置域名跳转实例
要求:浏览器地址栏输入qj.123.com之后,地址自动变成qj.abc.com 配置nginx跳转 server { listen 80; server_name qj.abc.com qj.123 ...