码迷,mamicode.com
首页 > 其他好文 > 详细

Luogu2073 送花 (平衡树)

时间:2019-07-29 19:05:05      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:fine   line   ide   eof   mat   void   gis   long   EAP   

打感叹号处为傻逼处

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

//#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;

const int N = 100007;

long long ansCost, ansBeauty;

struct Treap{
    int ch[2], fa, val, beauty, siz;
}t[N];
int root, treeIndex;
inline void Pushup(int rt){
    t[rt].siz = t[t[rt].ch[0]].siz + t[t[rt].ch[1]].siz;
}
inline int Ident(int x){
    return t[t[x].fa].ch[1] == x;
}
inline void Rotate(int x){
    int y = t[x].fa, z = t[y].fa, k = Ident(x);
    t[z].ch[Ident(y)] = x, t[x].fa = z; // !
    t[y].ch[k] = t[x].ch[k ^ 1], t[t[x].ch[k ^ 1]].fa = y; // !
    t[x].ch[k ^ 1] = y, t[y].fa = x;
    Pushup(y), Pushup(x);
}
inline void Splay(int x, int pos){
    while(t[x].fa != pos){
        int y = t[x].fa, z = t[y].fa;
        if(z != pos){
            Ident(x) == Ident(y) ? Rotate(y) : Rotate(x);
        }
        Rotate(x);
    }
    if(!pos) root = x;
}
inline void Find(int x){
    int u = root;
    if(!u) return;
    while(t[u].ch[x > t[u].val] && t[u].val != x) u = t[u].ch[x > t[u].val];
    Splay(u, 0);
}
inline void Insert(int x, int beauty){
    int u = root, fa = 0;
    while(u && t[u].val != x){
        fa = u;
        u = t[u].ch[x > t[u].val];
    }
    if(u){
        return;
    }
    else{
        ansCost += x;
        ansBeauty += beauty;
        u = ++treeIndex;
        t[u].ch[0] = t[u].ch[1] = 0;
        t[u].fa = fa;
        t[u].siz = 1;
        t[u].val = x;
        t[u].beauty = beauty;
        if(fa) t[fa].ch[x > t[fa].val] = u; // !
    }
    Splay(u, 0);
}
inline int Next(int x, int type){
    Find(x); 
    int u = root;
    if(t[u].val > x && type) return u;
    if(t[u].val < x && !type) return u;
    u = t[u].ch[type];
    while(t[u].ch[type ^ 1]) u = t[u].ch[type ^ 1];
    return u;
}
inline void Delete(int x){
    int pre = Next(x, 0), nxt = Next(x, 1);
    Splay(pre, 0), Splay(nxt, pre);
    t[nxt].ch[0] = 0;
}

inline void Calc(int u){
    if(!u) return;
    if(t[u].val == 2147483647 || t[u].val == -2147483647) return;
    Calc(t[u].ch[0]);
    Calc(t[u].ch[1]);
    ansCost += t[u].val;
    ansBeauty += t[u].beauty;
}
int main(){
    int opt;
    Insert(2147483647, 0);
    Insert(-2147483647, 0);
    while(scanf("%d", &opt) && opt != -1){
        if(opt == 1){
            int beauty, x;
            io >> beauty >> x;
            Insert(x, beauty);
        }
        else if(opt == 2){
            int x = Next(2147483647, 0);
            if(t[x].val != -2147483647){
                ansCost -= t[x].val;
                ansBeauty -= t[x].beauty;
                Delete(t[x].val);
            }
        }
        else{
            int x = Next(-2147483647, 1);
            if(t[x].val != 2147483647){
                ansCost -= t[x].val;
                ansBeauty -= t[x].beauty;
                Delete(t[x].val);
            }
        }
    }
    
    //Calc(root);
    
    printf("%lld %lld\n", ansBeauty, ansCost);
    
    return 0;
}

WA得红红火火(20pts)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

//#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;

const int N = 100007;

struct Treap{
    int ch[2], fa, val, beauty, siz;
}t[N];
int root, treeIndex;
inline void Pushup(int rt){
    t[rt].siz = t[t[rt].ch[0]].siz + t[t[rt].ch[1]].siz;
}
inline int Ident(int x){
    return t[t[x].fa].ch[1] == x;
}
inline void Rotate(int x){
    int y = t[x].fa, z = t[y].fa, k = Ident(x);
    t[z].ch[Ident(y)] = x, t[x].fa = z; // !
    t[y].ch[k] = t[x].ch[k ^ 1], t[t[x].ch[k ^ 1]].fa = y; // !
    t[x].ch[k ^ 1] = y, t[y].fa = x;
    Pushup(y), Pushup(x);
}
inline void Splay(int x, int pos){
    while(t[x].fa != pos){
        int y = t[x].fa, z = t[y].fa;
        if(z != pos){
            Ident(x) == Ident(y) ? Rotate(y) : Rotate(x);
        }
        Rotate(x);
    }
    if(!pos) root = x;
}
inline void Find(int x){
    int u = root;
    if(!u) return;
    while(t[u].ch[x > t[u].val] && t[u].val != x) u = t[u].ch[x > t[u].val];
    Splay(u, 0);
}
inline void Insert(int x, int beauty){
    int u = root, fa = 0;
    while(u && t[u].val != x){
        fa = u;
        u = t[u].ch[x > t[u].val];
    }
    if(u) return;
    else{
        u = ++treeIndex;
        t[u].ch[0] = t[u].ch[1] = 0;
        t[u].fa = fa;
        t[u].siz = 1;
        t[u].val = x;
        t[u].beauty = beauty;
        if(fa) t[fa].ch[x > t[fa].val] = u; // !
    }
    Splay(u, 0);
}
inline int MAXX(){
    int u = root;
    while(t[u].ch[1]) u = t[u].ch[1];
    return t[u].fa;
}
inline int MINN(){
    int u = root;
    while(t[u].ch[0]) u = t[u].ch[0];
    return t[u].fa;
}
inline int Next(int x, int type){
    Find(x); 
    int u = root;
    if(t[u].val > x && type) return u;
    if(t[u].val < x && !type) return u;
    u = t[u].ch[type];
    while(t[u].ch[type ^ 1]) u = t[u].ch[type ^ 1];
    return u;
}
inline void Delete(int x){
    int pre = Next(x, 0), nxt = Next(x, 1);
    Splay(pre, 0), Splay(nxt, pre);
    t[nxt].ch[0] = 0;
}

long long ansCost, ansBeauty;
inline void Calc(int u){
    if(!u) return;
    if(t[u].val == 2147483647 || t[u].val == -2147483647) return;
    Calc(t[u].ch[0]);
    Calc(t[u].ch[1]);
    ansCost += t[u].val;
    ansBeauty += t[u].beauty;
}
int main(){
    int opt;
    Insert(2147483647, 0);
    Insert(-2147483647, 0);
    int tot = 0;
    while(scanf("%d", &opt) && opt != -1){
        if(opt == 1){
            int beauty, x;
            io >> beauty >> x;
            Insert(x, beauty);
            ++tot;
        }
        else if(opt == 2){
            if(tot == 0) continue;
            int x = MAXX();
            Delete(t[x].val);
            --tot;
        }
        else{
            if(tot == 0) continue;
            int x = MINN();
            Delete(t[x].val);
            --tot;
        }
    }
    
    Calc(root);
    
    printf("%lld %lld\n", ansBeauty, ansCost);
    
    return 0;
}

Luogu2073 送花 (平衡树)

标签:fine   line   ide   eof   mat   void   gis   long   EAP   

原文地址:https://www.cnblogs.com/bingoyes/p/11265714.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!