社区讨论
10分求调
P2286[HNOI2004] 宠物收养场参与者 3已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @lyjhtxys
- 此快照首次捕获于
- 2024/07/13 10:14 2 年前
- 此快照最后确认于
- 2024/08/12 22:39 2 年前
CPP
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int a,b,op,x,ans1,root,sz;
int cnta,cntb;
#define MAXN 1000000
int ch[MAXN][2],f[MAXN],size[MAXN],cnt[MAXN],key[MAXN];
inline void clear(int x){
ch[x][0]=ch[x][1]=f[x]=size[x]=cnt[x]=key[x]=0;
}
inline bool get(int x){
return ch[f[x]][1]==x;
}
inline void update(int x){
if (x){
size[x]=cnt[x];
if (ch[x][0]) size[x]+=size[ch[x][0]];
if (ch[x][1]) size[x]+=size[ch[x][1]];
}
}
inline void rotate(int x){
int old=f[x],oldf=f[old],whichx=get(x);
ch[old][whichx]=ch[x][whichx^1]; f[ch[old][whichx]]=old;
ch[x][whichx^1]=old; f[old]=x;
f[x]=oldf;
if (oldf)
ch[oldf][ch[oldf][1]==old]=x;
update(old); update(x);
}
inline void splay(int x){
for (int fa;fa=f[x];rotate(x))
if (f[fa])
rotate((get(x)==get(fa))?fa:x);
root=x;
}
inline void insert(int x){
if (root==0){sz++; ch[sz][0]=ch[sz][1]=f[sz]=0; root=sz; size[sz]=cnt[sz]=1; key[sz]=x; return;/*sz表示点数*/}
int now=root,fa=0;
while(1){
if (x==key[now]){
cnt[now]++; update(now); update(fa); splay(now); break;
}
fa=now;
now=ch[now][key[now]<x];
if (now==0){
sz++;
ch[sz][0]=ch[sz][1]=0;
f[sz]=fa;
size[sz]=cnt[sz]=1;
ch[fa][key[fa]<x]=sz;
key[sz]=x;
update(fa);
splay(sz);
break;
}
}
}
inline int pre(){
int now=ch[root][0];
while (ch[now][1]) now=ch[now][1];
return now;
}
inline int next(){
int now=ch[root][1];
while (ch[now][0]) now=ch[now][0];
return now;
}
inline void del(int x){
splay(x);
if (cnt[root]>1){cnt[root]--; update(root); return;}
if (!ch[root][0]&&!ch[root][1]) {clear(root); root=0; return;}
if (!ch[root][0]){
int oldroot=root; root=ch[root][1]; f[root]=0; clear(oldroot); return;
}
else if (!ch[root][1]){
int oldroot=root; root=ch[root][0]; f[root]=0; clear(oldroot); return;
}
int leftbig=pre(),oldroot=root;
splay(leftbig);
ch[root][1]=ch[oldroot][1];
f[ch[oldroot][1]]=root;
clear(oldroot);
update(root);
}
signed main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>op>>x;
if(op==0){
cntb++;
if(cntb<cnta){
insert(x);
if(cnt[root]>1){
ans1+=0;
del(root);
continue;;
}
a=pre();
b=next();
if(abs(x-key[a])<=abs(x-key[b])){
ans1+=abs(x-key[a]);del(a);
}
else{
ans1+=abs(x-key[b]);del(b);
}
cntb--;
}else{
insert(x);
}
}else{
cnta++;
if(cnta<cntb){
insert(x);
if(cnt[root]>1){
ans1+=0;
del(root);
continue;
}
a=pre();
b=next();
if(abs(x-key[a])<=abs(x-key[b])){
ans1+=abs(x-key[a]);del(a);
ans1%=1000000;
}
else if(abs(x-key[b])<abs(x-key[a])){
ans1+=abs(x-key[b]);del(b);
ans1%=1000000;
}
cnta--;
}else{
insert(x);
}
}
}
cout<<ans1;
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...