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< ...
随机推荐
- JBoss 系列九十九:Rest WebService jBPM 6 集成演示样例
概述 jBPM 6 提供 Rest API 供第三方应用整合使用 jBPM 6,本文演示假设通过 Rest API: 启动流程 获取流程实例信息 启动 User Task 完毕 User Task j ...
- struts2获取request、session、application
struts2获取request.session.application public class LoginAction extends ActionSupport implements Reque ...
- css控制背景图片在浏览器中居中,下拉浏览器的时候背景图一直不变
如 http://www.gm.com/ css样式如下 ;;} #con{ position:absolute; ; ; height:100%; width:100%; background-im ...
- ADB错误“more than one device and emulator”(转)
当我连着手机充电的时候,启动模拟器调试,执行ADB指令时,报错.C:\Users\gaojs>adb shellerror: more than one device and emulatorC ...
- 沙盒操作的核心函数 - NSSearchPathForDirectoriesInDomains用法
1. iPhone会为每一个应用程序生成一个私有目录,这个目录位于: /Users/sundfsun2009/Library/Application Support/iPhone Simulator/ ...
- C#基础学习第二天(.net菜鸟的成长之路-零基础到精通)
1.加号的使用 在我们c#当中,如果想要两个字符串相连接,那么我们可以使用+号连接. 加号两边如果有一边是字符串,那么此时字符串起到了一个连接的作用. 如果加号两遍都是数字,那么加号起到一个相加 ...
- UEFI模式下安装Win 7系统
转载自:http://huayi898.blog.163.com/blog/static/2581351620144442319155/ 下载win7_64bit原版官方系统 1.用软碟通制作U盘启动 ...
- GUI对话框
消息对话框 public static void showMessageDialog(Component parentComponent,String message,String title,int ...
- 《Linux内核分析》 week6作业-Linux内核fork()系统调用的创建过程
一.进程控制块PCB-stack_struct 进程在操作系统中都有一个结构,用于表示这个进程.这就是进程控制块(PCB),在Linux中具体实现是task_struct数据结构,它主要记录了以下信息 ...
- Python3学习之二Django搭建
严格来讲,这篇应该是前一篇 的续集吧,这也属于环境搭建:搭建一个Web开发环境. 1,官网下载最新的Django,当前最新的是1.8.2.所以我就下的这个版本,下载下来的是一个gz包Django-1. ...