一开始没懂题目。。。。后来才发现这么水
#include<cstdio> #include<iostream> #include<cstring> using namespace std; char c; inline int read(int &a) { a=0;do c=getchar();while(c<'0'||c>'9'); while(c<='9'&c>='0')a=(a<<3)+(a<<1)+c-'0',c=getchar(); } inline int abs(int a){return a<0?-a:a;} int ans; int con; struct Spaly { struct Node { int data; Node *f,*lc,*rc; }; Node *root; int spaly_flag; Node *empty; inline Node *New_Node(){Node *tp=new Node; tp->lc=empty;tp->rc=empty;return tp;} inline Spaly(){empty=new Node;empty->data=-1;empty->lc=empty;empty->rc=empty;root=NULL;} inline void Left(Node *a) { Node *tp; if(a->f==root) root=a; else if(a->f->f->lc==a->f) a->f->f->lc=a; else a->f->f->rc=a; if(root!=a) tp=a->f->f; a->f->f=a; a->f->lc=a->rc; a->rc->f=a->f; a->rc=a->f; if(root!=a) a->f=tp; else a->f=a; } inline void Right(Node *a) { Node *tp; if(a->f==root) root=a; else if(a->f->f->rc==a->f) a->f->f->rc=a; else a->f->f->lc=a; if(root!=a) tp=a->f->f; a->f->f=a; a->f->rc=a->lc; a->lc->f=a->f; a->lc=a->f; if(root!=a) a->f=tp; else a->f=a; } inline void Change(Node *a){if(a->f->lc==a)Left(a);else Right(a); } inline void Twice_Change(Node *a){if(a->f->f->lc==a->f)if(a->f->lc==a)Change(a->f),Change(a);else Change(a),Change(a);else if(a->f->lc==a)Change(a),Change(a);else Change(a->f),Change(a); } inline void Rotota(Node *a) { while(a->f->f!=a->f) Twice_Change(a); while(a->f!=a) Change(a); } inline void insert(int data) { if(root==NULL) { root=New_Node(); root->f=root; root->data=data; return ; } Node *now=root,*tp=New_Node(); tp->data=data; while(true) { if(data<now->data) if(now->lc==empty) {now->lc=tp,tp->f=now;break;} else now=now->lc; else if(now->rc==empty) { now->rc=tp; tp->f=now; break; } else now=now->rc; } Rotota(tp); } inline Node*Pre(int data) { Node *p=empty,*now=root; while(true) { if(data>now->data) if(now->rc!=empty) {p=now,now=now->rc;} else return now;//(p==empty||now->data>p->data)?now:p; else if(data==now->data) return now; else if(now->lc==empty) return p; else {now=now->lc;} } } inline Node*Aft(int data) { Node *p=empty,*now=root; while(true) { if(data<now->data) if(now->lc!=empty) {p=now,now=now->lc;} else return now;//(p==empty||now->data<p->data)?now:p; else if(data==now->data) return now; else if(now->rc==empty) return p; else {now=now->rc;} } } inline void del(Node *a) { Rotota(a); if(a->lc==a->rc&&a->lc==empty) { root=NULL; delete a; return ; } while(a->lc!=empty||a->rc!=empty) if(a->lc!=empty) Change(a->lc); else Change(a->rc); if(a->f->lc==a) a->f->lc=empty; else a->f->rc=empty; delete a; } inline void del(int data) { Node *p=Pre(data),*a=Aft(data); // printf("%d:%d %d\n",++con,p->data,a->data); if(p==empty) ans+=abs(a->data-data),del(a); else if(a==empty) ans+=abs(p->data-data),del(p); else if(abs(p->data-data)<=abs(a->data-data)) ans+=abs(p->data-data),del(p); else ans+=abs(a->data-data),del(a); ans%=1000000; } inline void opera() { int flag,data; read(flag); if(root==NULL) spaly_flag=-1; if(spaly_flag==-1) spaly_flag=flag,read(data),insert(data); else if(flag==spaly_flag){read(data);insert(data);} else read(data),del(data); } void DFS(Node *a) { if(!spaly_flag) return ; if(a->lc!=empty) DFS(a->lc); if(a->rc!=empty) DFS(a->rc); ans+=a->data; } }S; int n; int main() { read(n); for(int i=1;i<=n;i++) S.opera(); // if(S.root)S.DFS(S.root); printf("%d\n",ans%1000000); return 0; }