性感3TLEsplaycpp代码,在线求调教
查看原帖
性感3TLEsplaycpp代码,在线求调教
1087796
yangmeinaixi楼主2025/2/7 10:22
#include<iostream>
using namespace std;
int n,tot,root,ans;
struct dog{int data,num,l,r,ln,rn,fa;}tree[160000];
#define data(i) (tree[i].data)
#define num(i) 1
#define ln(i) (tree[i].ln)
#define rn(i) (tree[i].rn)
#define ls(i) (tree[i].l)
#define rs(i) (tree[i].r)
#define fa(i) (tree[i].fa) 
#define il inline
#define rnt register int
char *p1,*p2,buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
il int read()
{
    rnt x=0,f=1;
    char ch=nc();
    while(ch<48||ch>57)
    {
        if(ch=='-')
            f=-1;
        ch=nc();
    }
    while(ch>=48&&ch<=57)
        x=x*10+ch-48,ch=nc();
   	return x*f;
}
template<typename type>
inline void write(type x,bool mode=1)//0为空格,1为换行
{
    x<0?x=-x,putchar('-'):0;static short Stack[50],top(0);
    do Stack[++top]=x%10,x/=10; while(x);
    while(top) putchar(Stack[top--]|48);
    mode?putchar('\n'):putchar(' ');
}
il void zag(rnt x){
	rnt y=fa(x);
	rs(y)=ls(x);
	if(ls(x))fa(ls(x))=y;
	fa(x)=fa(y);
	if(fa(y)) if(y==ls(fa(y)))ls(fa(y))=x;else rs(fa(y))=x;
	ls(x)=y,fa(y)=x,rn(y)=ln(x),ln(x)=rn(y)+ln(y)+num(y);
	return ;
}
il void zig(rnt x){
	rnt y=fa(x);
	ls(y)=rs(x);
	if(rs(x))fa(rs(x))=y;
	fa(x)=fa(y);
	if(fa(y)) if(y==ls(fa(y)))ls(fa(y))=x;else rs(fa(y))=x;
	rs(x)=y,fa(y)=x,ln(y)=rn(x),rn(x)=rn(y)+ln(y)+num(y);
	return ;
}
il void splay(rnt x){
	while(fa(x)){
		if(ls(fa(x))==x)zig(x);
		else zag(x);
	}
	root=x;
	return ;
}
il int find(rnt x){
	rnt p=root;
	while(p){
		if(data(p)==x){splay(p);return 1;}
		else if(data(p)<x)p=rs(p);
		else p=ls(p);
	}return 0;
}
il void insert(rnt x){
	rnt p=root,f;
	while(p)
	{
		f=p;
        // if(data(p)==x){num(p)++;splay(p);return ;}
		if(x<data(p)){ln(p)++;p=ls(p);}
		else{rn(p)++;p=rs(p);}
	}
	tot++;
	data(tot)=x;
	if(root==0){root=tot;return ;}
	fa(tot)=f;
	if(x<=data(f))ls(f)=tot;
	else rs(f)=tot;
	splay(tot);
	return ;
}
il void dlt(rnt p)
{
	find(p);
	p=root;
	// if(num(root)>1){num(root)--;return;}
	if(!ls(p)&&!rs(p)){root=0;return ;}
	if(!ls(p)){root=rs(p);fa(rs(p))=0;return ;}
	if(!rs(p)){root=ls(p);fa(ls(p))=0;return ;}
	rnt l=ls(p),r=rs(p);
	p=ls(p);
	fa(l)=0;
	while(rs(p))p=rs(p);
	splay(p);
	rs(p)=r;fa(r)=p;
	rn(p)=ln(r)+rn(r)+1;
	return ;
}
il int pred(rnt x){
	find(x);
	rnt p=ls(root);
	while(p)
	{   
		if(rs(p)==0||data(p)==x)break;
		p=rs(p);
	}
    if(p)return data(p);return 0x3f3f3f3f;
}
il int succ(rnt x){
	find(x);
	rnt p=rs(root);
	while(p)
	{
		if(ls(p)==0||data(p)==x)break;
		p=ls(p);
	}
    if(p) splay(p);
	if(p)return data(p);
    return 0x3f3f3f3f;
}

signed main(){
    bool idcard=0;
    long long sum=0;
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	n=read();
    for(rnt i=1;i<=n;i++)
    {
        rnt op,k;
        op=read();
        k=read();
        if(root==0)
        {
            idcard=op;
            insert(k);
        }
        else
        {
            if(op==idcard)
            {
                insert(k);
            }
            else
            {
                if(find(k))
                {
                    dlt(k);
                    continue;
                }
                insert(k);
                rnt pr=pred(k),nt=succ(k);
                rnt prs=abs(k-pr),nts=abs(nt-k);
                if(prs<=nts)
                {
                    dlt(k);
                    dlt(k-prs);
                    sum+=prs;
                }
                else
                {
                    dlt(k);
                    dlt(k+nts);
                    sum+=nts;
                }
            }
        }
    }
    write(sum%1000000,1);
}
/*
5                  
0 2                      
0 4                         
1 3
1 2
1 5

*/
2025/2/7 10:22
加载中...