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.... -------------------------------------------------------------------- ...
随机推荐
- SVG图形引用、裁切、蒙版
SVG图形引用.裁切.蒙版,使用三个标签: 1. <use>标签创建图形引用 2. <clipPath>标签裁切图形 3. <mask>标签创建蒙版 ...
- [Android Studio]SQLScout插件安装破解
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5972138.html [Android Studio]SQLS ...
- sublime text 输入法候选词不跟随光标
可以使用imesupport 插件解决 百度 : 搜狗 sublime 不跟 光标 找到这篇文章, 原始作者 http://qianduanblog.com/post/sublime-text-3-p ...
- Kali v2.1.2 for Raspberry Pi 3B
最新的下载地址是: https://www.offensive-security.com/kali-linux-arm-images/ 按照官网的说法是找不到树莓派版本的SHA1SUM和SHA1SUM ...
- 看看C# 6.0中那些语法糖都干了些什么(终结篇)
终于写到终结篇了,整个人像在梦游一样,说完这一篇我得继续写我的js系列啦. 一:带索引的对象初始化器 还是按照江湖老规矩,先扒开看看到底是个什么玩意. 1 static void Main(strin ...
- 在tmux中的vi 上下左右键变为了ABCD等字符
在本机上用vim编辑时,上下左右键没有问题,但是在tmux中确出现ABCD等字符. 原因是在tmux这个终端,默认做了字符转换,网上搜了很多答案,解决问题的设置是: set term=xterm
- SecondaryNameNode的工作流程
SecondaryNameNode是用来合并fsimage和edits文件来更新NameNode和metadata的. 其工作流程为: 1.secondary通知namenode切换edits文件 2 ...
- 不同数据库,查询前n条数据的SQL语句
不同的数据库,支持的SQL语法略有不同,以下是不同数据库查询前n条数据的SQl语句 SQL Server(MSSQL) SELECT TOP n * FROM table_name ORACLE SE ...
- 跳入linux的第一个坑-因为安装Ubuntu导致的硬盘被误格的恢复.(记TestDisk使用记录)
不看废话,直接跳到操作说明 前几日心血来潮想把家中的旧笔记本换成Linux操作系统,算是在业余生活中正式投入Linux的怀抱.说干就干,发行版选择了Ubuntu,下载了Ubuntu16.04的ISO, ...
- nginx整理
一.为什么选择Nginx搭建Web服务器 Apache和Nginx是目前使用最火的两种Web服务器,Apache出现比Nginx早.Apache HTTP Server(简称Apache)是世界使用排 ...