社区讨论
就是想知道大家的马蜂都是什么样?
灌水区参与者 26已保存回复 72
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 72 条
- 当前快照
- 1 份
- 快照标识符
- @lo37pbe4
- 此快照首次捕获于
- 2023/10/24 02:09 2 年前
- 此快照最后确认于
- 2023/10/24 02:09 2 年前
我先来(自认为很规矩,而且管看够):
CPP#include<bits/stdc++.h>
using namespace std;
struct node
{
int lc,rc;
int key;
int size;
int priority;
}tr[100001];
int tot=0,Root=0,n;
inline void Update(int root)
{
if(!root)
{
return;
}
tr[root].size=tr[tr[root].lc].size+tr[tr[root].rc].size+1;
return;
}
pair<int,int> Split(int root,int x)
{
if(!root)
{
return make_pair(0,0);
}
if(x>=tr[root].key)
{
pair<int,int>tmp=Split(tr[root].rc,x);
tr[root].rc=tmp.first;
Update(root);
return make_pair(root,tmp.second);
}
else
{
pair<int,int>tmp=Split(tr[root].lc,x);
tr[root].lc=tmp.second;
Update(root);
return make_pair(tmp.first,root);
}
}
int Merge(int u,int v)
{
if(!u)
{
return v;
}
else if(!v)
{
return u;
}
if(tr[u].priority>tr[v].priority)
{
tr[u].rc=Merge(tr[u].rc,v);
Update(u);
return u;
}
else
{
tr[v].lc=Merge(u,tr[v].lc);
Update(v);
return v;
}
}
inline void Insert(int x)
{
tot++;
tr[tot].key=x;
tr[tot].priority=rand();
tr[tot].size=1;
pair<int,int>tmp=Split(Root,x);
Root=Merge(Merge(tmp.first,tot),tmp.second);
return;
}
void Delete(int& root,int x)
{
if(!root)
{
return;
}
if(tr[root].key==x)
{
root=Merge(tr[root].lc,tr[root].rc);
return;
}
else if(x<tr[root].key)
{
Delete(tr[root].lc,x);
Update(root);
}
else
{
Delete(tr[root].rc,x);
Update(root);
}
}
int Rank(int& root,int x)
{
pair<int,int>tmp=Split(Root,x-1);
int res;
if(!tmp.first)
{
res=1;
}
else
{
res=tr[tmp.first].size+1;
}
Root=Merge(tmp.first,tmp.second);
return res;
}
int Findkth(int root,int k)
{
if(!root)
{
return -1;
}
if(tr[tr[root].lc].size>=k)
{
return Findkth(tr[root].lc,k);
}
if(k>tr[tr[root].lc].size+1)
{
return Findkth(tr[root].rc,k-tr[tr[root].lc].size-1);
}
return tr[root].key;
}
int Prev(int root,int x)
{
if(!root)
{
return -1;
}
if(x<=tr[root].key)
{
return Prev(tr[root].lc,x);
}
else
{
int res=Prev(tr[root].rc,x);
if(res==-1)
{
return root;
}
return res;
}
}
int Next(int root,int x)
{
if(!root)
{
return -1;
}
if(x>=tr[root].key)
{
return Next(tr[root].rc,x);
}
else if(x<tr[root].key)
{
int res=Next(tr[root].lc,x);
if(res==-1)
{
return root;
}
return res;
}
}
int main()
{
#ifdef M_D
freopen("i.in","r",stdin);
freopen("i.out","w",stdout);
#endif
ios::sync_with_stdio(false);
srand(time(0));
cin >> n;
for(int i=1;i<=n;i++)
{
int op,x;
cin >> op >> x;
if(op==1)
{
Insert(x);
}
else if(op==2)
{
Delete(Root,x);
}
else if(op==3)
{
cout<<Rank(Root,x)<<endl;
}
else if(op==4)
{
cout<<Findkth(Root,x)<<endl;
}
else if(op==5)
{
cout<<tr[Prev(Root,x)].key<<endl;
}
else
{
cout<<tr[Next(Root,x)].key<<endl;
}
}
return 0;
}
回复
共 72 条回复,欢迎继续交流。
正在加载回复...