【ZOJ2112】【整体二分+树状数组】带修改区间第k大
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://www.cnblogs.com/zhj5chengfeng/archive/2013/08/19/3268162.html
/*
宋代晏殊
《蝶恋花·槛菊愁烟兰泣露》 槛菊愁烟兰泣露。罗幕轻寒,燕子双飞去。明月不谙离恨苦。斜光到晓穿朱户。
昨夜西风凋碧树。独上高楼,望尽天涯路。欲寄彩笺兼尺素。山长水阔知何处。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#define LOCAL
const int INF = ;
const int MAXN = + ;
using namespace std;
struct QUERY{
int x, y;
int k, s, type, cur;//cur用来记录前面的值
}q[MAXN], q1[MAXN], q2[MAXN];
int Ans[MAXN];
int tmp[MAXN], c[MAXN];
int n, m, num, cnt;
int data[MAXN]; inline int lowbit(int x){return x&-x;}
void add(int x, int val){
while (x <= n){
c[x] += val;
x += lowbit(x);
}
return;
}
int sum(int x){
int cnt = ;
while (x > ){
cnt += c[x];
x -= lowbit(x);
}
return cnt;
}
//整体二分
void solve(int l, int r, int L, int R){
//这两个都是结束条件
if (l > r) return;
if (L == R){//更新答案
for (int i = l; i <= r; i++)
if (q[i].type == ) Ans[q[i].s] = L;
return;
}
int mid = (L + R) >> ;
for (int i = l; i <= r; i++){
if (q[i].type == && q[i].y <= mid) add(q[i].x, );
else if (q[i].type == && q[i].y <= mid) add(q[i].x, -);
else if (q[i].type == ) tmp[i] = sum(q[i].y) - sum(q[i].x - );
}
//更新完了就要清除标记了
for (int i = l; i <= r; i++){
if (q[i].type == && q[i].y <= mid) add(q[i].x, -);
else if (q[i].type == && q[i].y <= mid) add(q[i].x, );
}
int l1 = , l2 = ;
for (int i = l; i <= r; i++){
if (q[i].type == ){
//不用id就直接改
if (q[i].cur + tmp[i] > q[i].k - ) q1[++l1] = q[i];
else {
q[i].cur += tmp[i];
q2[++l2] = q[i];
}
}else{
if (q[i].y <= mid) q1[++l1] = q[i];
else q2[++l2] = q[i];
}
}
for (int i = ; i <= l1; i++) q[i + l - ] = q1[i];
for (int i = ; i <= l2; i++) q[i + l1 + l - ] = q2[i];
solve(l, l + l1 - , L, mid);
solve(l + l1, r, mid + , R);
}
void init(){
memset(c, , sizeof(c));
cnt = num = ;//指针初始化,num记录总的操作数量
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++){
num++;
scanf("%d", &data[i]);
q[num].x = i;q[num].type = ;//1代表插入
q[num].s = ;q[num].y = data[i];//没有用y就当val用
}
for (int i = ; i <= m; i++){
char str[];
num++;
scanf("%s", str);
if (str[] == 'Q'){
int l, r, k;
scanf("%d%d%d", &l, &r, &k);
q[num].x = l;q[num].y = r;
q[num].type = ; q[num].s = ++cnt;
q[num].k = k;
}else{
int l, x;
scanf("%d%d", &l, &x);
q[num].x = l;q[num].y = data[l];//2为删除
q[num].type = ;q[num].s = ;
q[++num].x = l;
q[num].y = x;//删除后插入
q[num].type = ;
q[num].s = ;
data[l] = x;//注意这里一定要改,不然会影响到后面的更新
}
}
for (int i = ; i <= num; i++) q[i].cur = ;
} int main(){
int T; scanf("%d", &T);
while (T--){
init();
solve(, num, , INF);
for (int i = ; i <= cnt; i++) printf("%d\n", Ans[i]);
}
return ;
}
【ZOJ2112】【整体二分+树状数组】带修改区间第k大的更多相关文章
- 主席树套树状数组——带修区间第k大zoj2112
主席树带修第k大 https://www.cnblogs.com/Empress/p/4659824.html 讲的非常好的博客 首先按静态第k大建立起一组权值线段树(主席树) 然后现在要将第i个值从 ...
- 【bzoj3110】[Zjoi2013]K大数查询 整体二分+树状数组区间修改
题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c.如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数 ...
- 【BZOJ3110】【整体二分+树状数组区间修改/线段树】K大数查询
Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位 ...
- 【BZOJ-2527】Meteors 整体二分 + 树状数组
2527: [Poi2011]Meteors Time Limit: 60 Sec Memory Limit: 128 MBSubmit: 831 Solved: 306[Submit][Stat ...
- 【bzoj2527】[Poi2011]Meteors 整体二分+树状数组
题目描述 有N个成员国.现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站. 这个星球经常会下陨石雨.BIU已经预测了接下来K场陨石雨的情况.BI ...
- BZOJ_3110_[Zjoi2013]K大数查询_整体二分+树状数组
BZOJ_3110_[Zjoi2013]K大数查询_整体二分+树状数组 Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位 ...
- 【bzoj4009】[HNOI2015]接水果 DFS序+树上倍增+整体二分+树状数组
题目描述 给出一棵n个点的树,给定m条路径,每条路径有一个权值.q次询问求一个路径包含的所有给定路径中权值第k小的. 输入 第一行三个数 n和P 和Q,表示树的大小和盘子的个数和水果的个数. 接下来n ...
- 少年,想学带修改主席树吗 | BZOJ1901 带修改区间第k小
少年,想学带修改主席树吗 | BZOJ1901 带修改区间第k小 有一道题(BZOJ 1901)是这样的:n个数,m个询问,询问有两种:修改某个数/询问区间第k小. 不带修改的区间第k小用主席树很好写 ...
- BZOJ 2527 [POI2011]MET-Meteors (整体二分+树状数组)
题目大意:略 洛谷传送门 整体二分裸题 考虑只有一个国家的情况如何处理 对询问数量二分答案,暴力$O(m)$打差分,求前缀和验证,时间是$O(mlogK)$ 如果有$n$个国家,就是$O(nmlogK ...
随机推荐
- 使用 Gradle 实现 TFS 构建自动化
发布于 2014-07-16 作者 陈 忠岳 感谢微软开放技术有限公司(简称"微软开放技术")发布的构建模板,我们现在便可以在 Team Foundation Server(TFS ...
- 2015第42周六Pgsql全文索引
全文搜索通常也就是文本搜索,它可以提供满足查询的识别自然语言的能力,并且任意性地通过相关性查询进行排序.搜索最常见的类型就是找到所有包含给定的查询术语的记录,并且以相似性的查询顺序返回它们. 对于普通 ...
- RAM和DDR
DDR内存现在渐渐成为内存市场中新的宠儿,因其合理的性价比从其诞生以来一直受到人们热烈的期望,希望这一新的内存产品全面提升系统的处理速度和带宽,就连对Rambus抱有无限希望的Intel公司也向外界宣 ...
- Java GC专家系列2:Java 垃圾回收的监控
这是”成为GC专家系列”文章的第二篇.在第一篇理解Java垃圾回收中我们学习了几种不同的GC算法的处理过程,GC的工作方式,新生代与老年代的区别.到目前为止,你应该已经了解了JDK 7中的5种GC类型 ...
- 钥匙计数之一 - HDU 1438(状态压缩打表)
分析:首先想到每个钥匙的结尾有4种状态,不过题目还需要判断有三种不同的钥匙深度,所以每种深度结尾后有2^4种状态,0000->1111,不过题目还需需要有相邻的钥匙深度大于等于3,所以需要两种不 ...
- SVN 资源库报错 E175002
遇到一个问题, svn: E175002: OPTIONS request failed on '/' Connection timed out: connect 试了网上好多办法,都没有,最后公司大 ...
- nORA-01000: 超出打开游标的最大数(SDE连接)
1.运行ArcObjects代码报错: IFeatureCursor pCursor = null; try { pCursor = m_pCatchLayer.m_pFeatureClass.Sea ...
- CMS收集器
CMS收集周期 CMS并非没有暂停,而是用两次短暂停来替代串行标记整理算法的长暂停,它的收集周期是这样:初始标记(CMS-initial-mark) -> 并发标记(CMS-concurrent ...
- MySQL查询过程中出现lost connection to mysql server during query 的解决办法
window7 64位系统,MySQL5.7 问题:在使用shell进行数据表更新操作的过程,输入以下查询语句: ,; 被查询的表记录数达到500W条,在查询过程中出现如题目所示的问题,提示" ...
- C#中的线程(上)-入门 分类: C# 线程 2015-03-09 10:56 53人阅读 评论(0) 收藏
1. 概述与概念 C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为"主线 ...