treap模板题
#include<cstdio> #include<cctype> #include<algorithm> using namespace std; int cnt,rt,w,c; long long sumc,sumw; struct data{int w,c,ls,rs;}a[100002]; inline void read(int &x){ char ch=getchar();x=0;int f=1; while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} x*=f; } void rturn(int &x){int tmp=a[x].ls;a[x].ls=a[tmp].rs;a[tmp].rs=x;x=tmp;} void lturn(int &x){int tmp=a[x].rs;a[x].rs=a[tmp].ls;a[tmp].ls=x;x=tmp;} inline void insert(int &now){ if(now==0){now=++cnt;a[now].w=w;a[now].c=c;sumw+=w*1LL;sumc+=c*1LL;return;} if(a[now].c==c)return; if(a[now].c>c){insert(a[now].ls);if(a[a[now].ls].w<a[now].w)rturn(now);} else {insert(a[now].rs);if(a[a[now].rs].w<a[now].w)lturn(now);} } inline void del_ex(int &x){ if(x==0)return; if(a[x].rs==0){sumw-=a[x].w*1LL;sumc-=a[x].c*1LL;x=a[x].ls;return;} del_ex(a[x].rs); } inline void del_ch(int &x){ if(x==0)return; if(a[x].ls==0){sumw-=a[x].w*1LL;sumc-=a[x].c*1LL;x=a[x].rs;return;} del_ch(a[x].ls); } int main(){ int typ;read(typ); while(typ!=-1){ switch(typ){ case 1:read(w);read(c);insert(rt);break; case 2:del_ex(rt);break; case 3:del_ch(rt);break; } read(typ); } printf("%lld %lld",sumw,sumc); return 0; }
set的效率不高
#include<cstdio> #include<cctype> #include<set> using namespace std; #define maxn 1000002 set<int>q; int vis[maxn],sumw,sumc; inline void read(int &x){ char ch=getchar();x=0;int f=1; while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} x*=f; } int main(){ set<int>::iterator it; int typ,w,c;read(typ); while(typ!=-1){ switch(typ){ case 1:read(w);read(c);if(vis[c]!=0){read(typ);continue;}else{vis[c]=w;q.insert(c);sumw+=w;sumc+=c;}break; case 2:if(!q.empty()){it=q.end();it--;sumw-=vis[*it];sumc-=*it;q.erase(it);vis[*it]=0;}break; case 3:if(!q.empty()){it=q.begin();sumw-=vis[*it];sumc-=*it;q.erase(it);vis[*it]=0;}break; } read(typ); } printf("%d %d",sumw,sumc); return 0; }