社区讨论
求解qwq50分treap不知道错在哪里了……
P2286[HNOI2004] 宠物收养场参与者 2已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @mi5hlqc7
- 此快照首次捕获于
- 2025/11/19 12:13 4 个月前
- 此快照最后确认于
- 2025/11/19 12:13 4 个月前
C
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int p=1000000;
int root,ans,size,sum=0,ch=0,rn=0;
struct node{
int l,r,size,w,v,rnd;
}t[500005];
inline int update(int k)
{
t[k].size=t[t[k].l].size+t[t[k].r].size+t[k].w;
}
inline int lturn(int &k)
{
int tt=t[k].r;
t[k].r=t[tt].l;
t[tt].l=k;
t[tt].size=t[k].size;
update(k);
k=tt;
}
inline int rturn(int &k)
{
int tt=t[k].l;
t[k].l=t[tt].r;
t[tt].r=k;
t[tt].size=t[k].size;
update(k);
k=tt;
}
inline void insert(int &k,int x)
{
if(!k)
{
size++;
k=size;
t[k].size=t[k].w=1;
t[k].v=x;
t[k].rnd=rand();
return ;
}
t[k].size++;
if(x==t[k].v) t[k].w++;
else
{
if(x>t[k].v)
{
insert(t[k].r,x);
if(t[t[k].r].rnd<t[k].rnd) lturn(k);
}
else
{
insert(t[k].l,x);
if(t[t[k].l].rnd<t[k].rnd) rturn(k);
}
}
}
inline int pro(int k,int x)
{
if(k==0) return 0;
if(t[k].v<=x)
{
ans=k;
pro(t[k].r,x);
}
else pro(t[k].l,x);
}
inline int succ(int k,int x)
{
if(k==0) return 0;
if(t[k].v>=x)
{
ans=k;
succ(t[k].l,x);
}
else succ(t[k].r,x);
}
inline void del(int &k,int x)
{
if(!k) return ;
if(t[k].v==x)
{
if(t[k].w>1)
{
t[k].w--;
t[k].size--;
return ;
}
else
{
if(t[k].l*t[k].r==0) k=t[k].l+t[k].r;
else
{
if(t[t[k].l].rnd<t[t[k].r].rnd)
{
rturn(k);
del(k,x);
}
else
{
lturn(k);
del(k,x);
}
}
}
}
else
{
if(x>t[k].v)
{
t[k].size--;
del(t[k].r,x);
}
else
{
t[k].size--;
del(t[k].l,x);
}
}
}
inline int qiu(int x)
{
int a,b,a1,b1;
pro(root,x);a=t[ans].v;a1=a;
succ(root,x);b=t[ans].v;b1=b;
a=a-x;b=b-x;
if(a<0) a=-a;
if(b<0) b=-b;
if(a<=b)
{
sum=(sum+a)%p;
del(root,a1);
}
if(a>b)
{
sum=(sum+b)%p;
del(root,b1);
}
}
int main()
{
int n;
scanf("%d",&n);
int x,y;
while(n--)
{
scanf("%d%d",&x,&y);
if(x==0)
{
if(rn==0) insert(root,y),ch++;
else rn--,qiu(y);
}
else
{
if(ch==0) insert(root,y),rn++;
else ch--,qiu(y);
}
}
printf("%d",sum%p);
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...