1. 程式人生 > >bzoj3685普通van Emde Boas樹 線段樹

bzoj3685普通van Emde Boas樹 線段樹

while 個數 最大 time push 線段 != submit using

3685: 普通van Emde Boas樹

Time Limit: 9 Sec Memory Limit: 128 MB
Submit: 1932 Solved: 626
[Submit][Status][Discuss]

Description

設計數據結構支持:
1 x 若x不存在,插入x
2 x 若x存在,刪除x
3 輸出當前最小值,若不存在輸出-1
4 輸出當前最大值,若不存在輸出-1
5 x 輸出x的前驅,若不存在輸出-1
6 x 輸出x的後繼,若不存在輸出-1
7 x 若x存在,輸出1,否則輸出-1

Input

第一行給出n,m 表示出現數的範圍和操作個數
接下來m行給出操作
n<=10^6,m<=2*10^6,0<=x<n

Output

Sample Input

10 11
1 1
1 2
1 3
7 1
7 4
2 1
3
2 3
4
5 3
6 2

Sample Output

1
-1
2
2
2
-1

HINT

Source

By Zky

常規操作
要用zkw減小常數,但我貌似卡過了

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define ls u<<1
#define rs ls|1
#define ll long long
#define N 1000050
using namespace
std; int n,m,sum[N<<2],vis[N]; void pushup(int u){sum[u]=sum[ls]+sum[rs];} int findpos(int u,int l,int r,int x){ if(!sum[u])return 0; if(l==r)return sum[u]; int mid=l+r>>1; if(x<=mid)return findpos(ls,l,mid,x); return findpos(rs,mid+1,r,x)+sum[ls]; } void
update(int u,int L,int R,int p,int val){ if(L==R){ sum[u]=val; return; } int mid=L+R>>1; if(p<=mid)update(ls,L,mid,p,val); else update(rs,mid+1,R,p,val); pushup(u); } int query(int u,int l,int r,int p){ if(l==r)return l; int mid=l+r>>1; if(p<=sum[ls])return query(ls,l,mid,p); return query(rs,mid+1,r,p-sum[ls]); } int main(){ scanf("%d%d",&n,&m); int op,x; while(m--){ scanf("%d",&op); if(op!=3&&op!=4)scanf("%d",&x); if(op==1)update(1,0,n-1,x,1),vis[x]=1; if(op==2)update(1,0,n-1,x,0),vis[x]=0; if(op==3){ if(sum[1]==0)puts("-1"); else printf("%d\n",query(1,0,n-1,1)); } if(op==4){ if(sum[1]==0)puts("-1"); else printf("%d\n",query(1,0,n-1,sum[1])); } if(op==5){ int p=findpos(1,0,n-1,x); if(vis[x])p--; if(p<=0)puts("-1"); else printf("%d\n",query(1,0,n-1,p)); } if(op==6){ int p=findpos(1,0,n-1,x);p++; if(p>sum[1])puts("-1"); else printf("%d\n",query(1,0,n-1,p)); } if(op==7){ if(vis[x])puts("1"); else puts("-1"); } } return 0; }

bzoj3685普通van Emde Boas樹 線段樹