bzoj 1901 主席树+树状数组
修改+查询第k小值
单纯主席树修改会打乱所有,所以再套一个树状数组维护前缀和使得修改,查询都是log
对了,bzoj上不需要读入组数,蜜汁re。。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m,sz,T,num_tot,num_cnt,num_l,num_r;
int sum[8000005],lon[8000005],ron[8000005],num[60005];
int a[50005],k[10005],p[10005],q[10005],root[60005];
bool bo[10005];
int L[500],R[500];
int lowbit(int x){return x&(-x);}
void update(int p,int &rt,int l,int r,int x,int y){
rt=++sz; sum[rt]=sum[p]+y;
lon[rt]=lon[p]; ron[rt]=ron[p];
if(l==r) return;
int mid=(l+r)/2;
if(x<=mid) update(lon[p],lon[rt],l,mid,x,y);
else update(ron[p],ron[rt],mid+1,r,x,y);
}
int query(int l,int r,int k){
if(l==r) return l;
int suml=0,sumr=0;
for(int i=1;i<=num_l;i++) suml+=sum[lon[L[i]]];
for(int i=1;i<=num_r;i++) sumr+=sum[lon[R[i]]];
int mid=(l+r)/2;
if(sumr-suml>=k){
for(int i=1;i<=num_l;i++) L[i]=lon[L[i]];
for(int i=1;i<=num_r;i++) R[i]=lon[R[i]];
return query(l,mid,k);
}
else{
for(int i=1;i<=num_l;i++) L[i]=ron[L[i]];
for(int i=1;i<=num_r;i++) R[i]=ron[R[i]];
return query(mid+1,r,k-(sumr-suml));
}
}
int main()
{
char s[5];
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
num[i]=a[i];
} num_tot=n;
for(int i=1;i<=m;i++){
scanf("%s",s);
if(s[0]=='Q')
scanf("%d%d%d",&p[i],&q[i],&k[i]);
else{
scanf("%d%d",&p[i],&q[i]);
num[++num_tot]=q[i]; bo[i]=1;
}
}
sort(num+1,num+num_tot+1);
int num_cnt=unique(num+1,num+num_tot+1)-num-1;
for(int i=1;i<=n;i++){
int t=lower_bound(num+1,num+num_cnt+1,a[i])-num;
for(int j=i;j<=n;j+=lowbit(j))
update(root[j],root[j],1,num_cnt,t,1);
}
for(int i=1;i<=m;i++){
if(bo[i]){
int t=lower_bound(num+1,num+num_cnt+1,a[p[i]])-num;
for(int j=p[i];j<=n;j+=lowbit(j))
update(root[j],root[j],1,num_cnt,t,-1);
a[p[i]]=q[i];
t=lower_bound(num+1,num+num_cnt+1,q[i])-num;
for(int j=p[i];j<=n;j+=lowbit(j))
update(root[j],root[j],1,num_cnt,t,1);
}
else{
p[i]--; num_l=num_r=0;
for(int j=p[i];j>0;j-=lowbit(j))
L[++num_l]=root[j];
for(int j=q[i];j>0;j-=lowbit(j))
R[++num_r]=root[j];
printf("%d\n",num[query(1,num_cnt,k[i])]);
}
}
return 0;
}
bzoj 1901 主席树+树状数组的更多相关文章
- BZOJ.1901.Dynamic Rankings(树状数组套主席树(动态主席树))
题目链接 BZOJ 洛谷 区间第k小,我们可以想到主席树.然而这是静态的,怎么支持修改? 静态的主席树是利用前缀和+差分来求解的,那么对于每个位置上的每棵树看做一个点,拿树状数组更新. 还是树状数组的 ...
- BZOJ 1901 Dynamic Rankings 树董事长
标题效果:间隔可以改变k少 我的两个天树牌主席... 隔断Count On A Tree 之后我一直认为,随着树的主席的变化是分域林木覆盖率可持久段树. .. 事实上,我是误导... 尼可持久化线段树 ...
- [BZOJ 1901] Dynamic Rankings 【树状数组套线段树 || 线段树套线段树】
题目链接:BZOJ - 1901 题目分析 树状数组套线段树或线段树套线段树都可以解决这道题. 第一层是区间,第二层是权值. 空间复杂度和时间复杂度均为 O(n log^2 n). 线段树比树状数组麻 ...
- CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)
The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...
- 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树状数组套主席树)
http://www.lydsy.com/JudgeOnline/problem.php?id=1901 首先还是吐槽时间,我在zoj交无限tle啊!!!!!!!!我一直以为是程序错了啊啊啊啊啊啊. ...
- BZOJ 1901 Zju2112 Dynamic Rankings ——树状数组套主席树
[题目分析] BZOJ这个题目抄的挺霸气. 主席树是第一时间想到的,但是修改又很麻烦. 看了别人的题解,原来还是可以用均摊的思想,用树状数组套主席树. 学到了新的姿势,2333o(* ̄▽ ̄*)ブ [代 ...
- Bzoj 1901: Zju2112 Dynamic Rankings 主席树,可持久,树状数组,离散化
1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6321 Solved: 2628[Su ...
- bzoj 1901: Zju2112 Dynamic Rankings -- 主席树,树状数组,哈希
1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec Memory Limit: 128 MB Description 给定一个含有n个数的序列a[1] ...
- 【BZOJ 1901】【Zju 2112】 Dynamic Rankings 动态K值 树状数组套主席树模板题
达神题解传送门:http://blog.csdn.net/dad3zz/article/details/50638360 说一下我对这个模板的理解: 看到这个方法很容易不知所措,因为动态K值需要套树状 ...
随机推荐
- 万水千山ABP - 时区问题
关于时间和时区问题,后面的参考文章中有详细的描述. 我遇到的问题是: 在MVC视图页面中,显示记录的生成时间 CreationTime <div> @Model.CreationTime ...
- merge intervals(合并间隔)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- Python版冒泡排序算法
0 为什么写本文 一方面对经典排序算法冒泡排序进行复习,另一方面通过实际应用来检验python基础知识的掌握情况,包括range函数.len函数.for循环.if语句.函数定义与调用.列表的排序等知识 ...
- maven常见配置
maven surefire plugin 默认执行失败后,不会继续执行,需要在</configuration>中设置参数 <testFailureIgnore>true< ...
- Java编程语言下Selenium 对于下拉框,单选,多选等选择器的操作
WebElement selector = driver.findElement(By.id("Selector")); Select select = new Select(se ...
- 自动生成MyEclipse 安装破解码
新建一个class 文件,Debug 模式运行一个,输入任意值 ,回车得到破解安装码 代码文件如下: import java.io.*; public class MyEclipseGen { pri ...
- TypeScript 的声明文件的使用与编写
https://fenying.net/2016/09/19/typings-for-typescript/ TypeScript 是 JavaScript 的超集,相比 JavaScript,其最关 ...
- springboot中使用分页,文件上传,jquery的具体步骤(持续更新)
分页: pom.xml 加依赖 <dependency> <groupId>com.github.pagehelper</groupId> <arti ...
- Mysql 快速指南
Mysql 快速指南 本文的示例在 Mysql 5.7 下都可以测试通过. 知识点 概念 数据库(database):保存有组织的数据的容器(通常是一个文件或一组文件). 数据表(table):某种特 ...
- python3.6 安装win32api时候找不到regitry的问题
首先下载 https://sourceforge.net/projects/pywin32/files/pywin32/ 找到对应的即可 我需要的是这个 打开之后会提示3.6未注册 在任意位置新建一个 ...