BZOJ3196: Tyvj 1730 二逼平衡树
主席树的常数蜜汁优越,在BZOJ上跑了rnk1。
做法很简单,主席树套BIT。
1-3做法很简单,第四个和第五个做法转换成前两个就行了。
//BZOJ 3196
//by Cydiater
//2016.12.10
#include <iostream>
#include <queue>
#include <map>
#include <ctime>
#include <cmath>
#include <cstring>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define up(i,j,n) for(int i=j;i<=n;i++)
#define down(i,j,n) for(int i=j;i>=n;i--)
#define cmax(a,b) a=max(a,b)
#define cmin(a,b) a=min(a,b)
#define FILE "psh"
const int MAXN=5e4+5;
const int oo=0x3f3f3f3f;
inline int read(){
char ch=getchar();int x=0,f=1;
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int N,M,root[MAXN<<1],arr[MAXN],fsort[MAXN<<1],rnum,cnt=0,top[2],preL[MAXN],preR[MAXN],tmpL[MAXN],tmpR[MAXN],TMP;
struct Chair_man_Tree{
int son[2],sum;
}t[MAXN<<6];
struct Query{
int opt,L,R,pos,K;
}query[MAXN];
namespace solution{
inline int lowbit(int i){return ((i)&(-i));}
int NewNode(int sum,int son0,int son1){
t[++cnt].sum=sum;t[cnt].son[0]=son0;t[cnt].son[1]=son1;
return cnt;
}
void insert(int leftt,int rightt,int &Root,int last,int pos){
Root=NewNode(t[last].sum+1,t[last].son[0],t[last].son[1]);
int mid=(leftt+rightt)>>1;
if(leftt==rightt) return;
if(pos<=mid) insert(leftt,mid,t[Root].son[0],t[last].son[0],pos);
else insert(mid+1,rightt,t[Root].son[1],t[last].son[1],pos);
}
void PushTree(int L,int R){
top[0]=top[1]=0;
preL[++top[0]]=root[(L-1==0?0:(N+L-1))];
preR[++top[1]]=root[R+N];
for(int j=L-1;j>=1;j-=lowbit(j)) preL[++top[0]]=root[j];
for(int j=R;j>=1;j-=lowbit(j)) preR[++top[1]]=root[j];
}
void ChooseLeft(){
up(i,1,top[0])preL[i]=t[preL[i]].son[0];
up(i,1,top[1])preR[i]=t[preR[i]].son[0];
}
void ChooseRight(){
up(i,1,top[0])preL[i]=t[preL[i]].son[1];
up(i,1,top[1])preR[i]=t[preR[i]].son[1];
}
int Col(){
int sum=0;
up(i,1,top[0])sum-=t[t[preL[i]].son[0]].sum;
up(i,1,top[1])sum+=t[t[preR[i]].son[0]].sum;
return sum;
}
void Prepare(){
rnum=N=read();M=read();
up(i,1,N)arr[i]=fsort[i]=read();
up(i,1,M){
query[i].opt=read();
if(query[i].opt==3){
query[i].pos=read();query[i].K=read();
fsort[++rnum]=query[i].K;continue;
}
query[i].L=read();query[i].R=read();query[i].K=read();
if(query[i].opt==4||query[i].opt==5)
fsort[++rnum]=query[i].K;
}
sort(fsort+1,fsort+rnum+1);
rnum=unique(fsort+1,fsort+rnum+1)-(fsort+1);
up(i,1,N){
arr[i]=lower_bound(fsort+1,fsort+rnum+1,arr[i])-fsort;
insert(1,rnum,root[i+N],root[i+N-1],arr[i]);
}
}
int Rank(int leftt,int rightt,int pos){
if(leftt==rightt){
TMP=0;
up(i,1,top[0])TMP-=t[preL[i]].sum;
up(i,1,top[1])TMP+=t[preR[i]].sum;
return 1;
}
int sum=0,mid=(leftt+rightt)>>1;
sum=Col();
if(pos<=mid){
ChooseLeft();
return Rank(leftt,mid,pos);
}else{
ChooseRight();
return sum+Rank(mid+1,rightt,pos);
}
}
int Get(int leftt,int rightt,int rnk){
int sum,mid=(leftt+rightt)>>1;
if(leftt==rightt) return fsort[leftt];
sum=Col();
if(rnk<=sum){
ChooseLeft();
return Get(leftt,mid,rnk);
}else{
ChooseRight();
return Get(mid+1,rightt,rnk-sum);
}
}
void Insert(int leftt,int rightt,int &Root,int pos,int tag){
if(!Root)Root=NewNode(0,0,0);
t[Root].sum+=tag;
if(leftt==rightt) return;
int mid=(leftt+rightt)>>1;
if(pos<=mid) Insert(leftt,mid,t[Root].son[0],pos,tag);
else Insert(mid+1,rightt,t[Root].son[1],pos,tag);
}
void Slove(){
up(i,1,M){
int opt=query[i].opt;
if(opt==1){
int L=query[i].L,R=query[i].R,K=lower_bound(fsort+1,fsort+rnum+1,query[i].K)-fsort;
PushTree(L,R);
printf("%d\n",Rank(1,rnum,K));
}
if(opt==2){
int L=query[i].L,R=query[i].R,K=query[i].K;
PushTree(L,R);
printf("%d\n",Get(1,rnum,K));
}
if(opt==3){
int Pos=query[i].pos,K=query[i].K;
for(int j=Pos;j<=N;j+=lowbit(j))
Insert(1,rnum,root[j],arr[Pos],-1);
arr[Pos]=lower_bound(fsort+1,fsort+rnum+1,K)-fsort;
for(int j=Pos;j<=N;j+=lowbit(j))
Insert(1,rnum,root[j],arr[Pos],1);
}
if(opt==4){
int L=query[i].L,R=query[i].R,K=lower_bound(fsort+1,fsort+rnum+1,query[i].K)-fsort;
PushTree(L,R);
int rank=Rank(1,rnum,K);
PushTree(L,R);
printf("%d\n",Get(1,rnum,rank-1));
}
if(opt==5){
int L=query[i].L,R=query[i].R,K=lower_bound(fsort+1,fsort+rnum+1,query[i].K)-fsort;
PushTree(L,R);
int rank=Rank(1,rnum,K);
PushTree(L,R);
printf("%d\n",Get(1,rnum,rank+TMP));
}
}
}
}
int main(){
using namespace solution;
Prepare();
Slove();
return 0;
}
BZOJ3196: Tyvj 1730 二逼平衡树的更多相关文章
- bzoj3196: Tyvj 1730 二逼平衡树 树套树
地址:http://www.lydsy.com/JudgeOnline/problem.php?id=3196 题目: 3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec ...
- 【线段树套平衡树】【pb_ds】bzoj3196 Tyvj 1730 二逼平衡树
线段树套pb_ds里的平衡树,在洛谷OJ上测试,后三个测试点TLE #include<cstdio> #include<algorithm> #include<ext/p ...
- [bzoj3196]Tyvj 1730 二逼平衡树——线段树套平衡树
题目 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的数值 4.查 ...
- [bzoj3196][Tyvj 1730][二逼平衡树] (线段树套treap)
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的数值 4.查询k在 ...
- 【分块】bzoj3196 Tyvj 1730 二逼平衡树
分块 或 树套树. 在每个块中维护一个有序表,查询时各种二分,全都是分块的经典操作,就不详细说了. 块的大小定为sqrt(n*log2(n))比较快. #include<cstdio> # ...
- 【带修莫队】【权值分块】bzoj3196 Tyvj 1730 二逼平衡树
这题用了三种算法写: 分块+二分:O(n*sqrt(n*log(n)) 函数式权值分块:O(n*sqrt(n)) 带修莫队+权值分块:O(n5/3) 结果……复杂度越高的实际上跑得越快……最后这个竟然 ...
- 【函数式权值分块】【分块】bzoj3196 Tyvj 1730 二逼平衡树
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define ...
- bzoj 3196 Tyvj 1730 二逼平衡树(线段树套名次树)
3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1807 Solved: 772[Submit][Stat ...
- BZOJ 3196: Tyvj 1730 二逼平衡树( 树套树 )
这道题做法应该很多吧.... 我用了线段树套treap.... -------------------------------------------------------------------- ...
随机推荐
- Android中使用GridView和ImageViewSwitcher实现电子相册简单功能
我们在手机上查看相册时,首先看到的是网格状的图片展示界面,然后我们选择想要欣赏的照片点击进入,这样就可以全屏观看该照片,并且可以通过左右滑动来切换照片.如下图的显示效果: 首先我们先罗列一下本次实现所 ...
- 在Asp.net MVC 3 web应用程序中,我们会用到ViewData与ViewBag,对比一下:
Asp.net MVC中的ViewData与ViewBag ViewData ViewBag 它是Key/Value字典集合 它是dynamic类型对像 从Asp.net MVC 1 就有了 ASP. ...
- ASP.NET MVC 5 01 - ASP.NET概述
本篇目录: ASP.NET 概述 .NET Framework 与 ASP.NET ASP.NET MVC简介 ASP.NET的特色和优势 典型案例 ▁▃▅ ASP.NET概述 ▅▃▁ 目前开发B/S ...
- EasyUI combobox
高度自适应 data-options="required:true,editable:false,panelHeight:'auto',panelMaxHeight:170" 加上 ...
- 详解Linux交互式shell脚本中创建对话框实例教程_linux服务器
本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一下. 当你在终端环境下安装新的软件时,你可以经常看到信息 ...
- shell——awk
awk -F"分隔符" "command" filename awk -F":" '{print $1}' /etc/passwd 字段引用 ...
- sublime text 3 快捷键大全以及配置编译环境
Sublime text 3是码农最喜欢的代码编辑器,每天和代码打交道,必先利其器,掌握基本的代码编辑器的快捷键,能让你打码更有效率.刚开始可能有些生疏,只要花一两个星期坚持使用并熟悉这些常用的快捷键 ...
- js判断用户是否正在滚动滚动条,滚动条滚动是否停止
js智能判断是否可以自动滚动 比如,做一个音乐播放器,边播放,边定位歌词,播放的时候,需要自动定位到播放语句,但是用户去拖动或者滚动div(歌词面板)时,这时就必须停止自动滚动,或者说是不能自动滚动, ...
- 【原】移动web资源整理
2013年初接触移动端,简单做下总结,首先了解下移动web带来的问题 设备更新换代快——低端机遗留下问题.高端机带来新挑战 浏览器厂商不统一——兼容问题多 网络更复杂——弱网络,页面打开慢 低端机性能 ...
- JQuery 滚动轮播
css: *{margin: 0;padding: 0;}body{font-size: 12px;line-height: 24px;text-algin: center; }a{color: #f ...