zoukankan      html  css  js  c++  java
  • luogu2072

    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;
    }
  • 相关阅读:
    Go之运算符
    前端开发之工具库
    MVC与MVVM
    开发工具之Vscode编辑器
    常用名词汇总
    python常见错误总结
    Python之常用第三方库总结
    PHP程序员的成长路线
    web 应用常见安全漏洞
    redis和memcached的区别详解
  • 原文地址:https://www.cnblogs.com/MikuKnight/p/9159178.html
Copyright © 2011-2022 走看看