zoukankan      html  css  js  c++  java
  • 洛谷P1501 Tree II

    LCT

    还是LCT的板子,下放标记和那道线段树2一样,先放乘。。之前用char忘记getchar,调了好久。。。
    注意开long long!!

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define full(a, b) memset(a, b, sizeof a)
    using namespace std;
    typedef long long ll;
    inline int lowbit(int x){ return x & (-x); }
    inline int read(){
        int X = 0, w = 0; char ch = 0;
        while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
        while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
        return w ? -X : X;
    }
    inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
    inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
    template<typename T>
    inline T max(T x, T y, T z){ return max(max(x, y), z); }
    template<typename T>
    inline T min(T x, T y, T z){ return min(min(x, y), z); }
    template<typename A, typename B, typename C>
    inline A fpow(A x, B p, C lyd){
        A ans = 1;
        for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
        return ans;
    }
    const ll N = 200005;
    ll n, q, tot, p, ch[N][2], size[N], rev[N], val[N], fa[N], sum[N], mul[N], add[N], st[N];
    
    void newNode(ll v){
        val[++tot] = v; size[tot] = mul[tot] = 1, sum[tot] = v;
        ch[tot][0] = ch[tot][1] = fa[tot] = rev[tot] = add[tot] = 0;
    }
    
    bool isRoot(ll x){
        return (ch[fa[x]][0] != x && ch[fa[x]][1] != x);
    }
    
    void pushMul(ll x, ll c){
        mul[x] *= c, add[x] *= c, sum[x] *= c, val[x] *= c;
        mul[x] %= p, add[x] %= p, sum[x] %= p, val[x] %= p;
    }
    
    void pushAdd(ll x, ll c){
        add[x] += c, sum[x] += size[x] * c, val[x] += c;
        add[x] %= p, sum[x] %= p, val[x] %= p;
    }
    
    void pushRev(ll x){
        rev[x] ^= 1, swap(ch[x][0], ch[x][1]);
    }
    
    void push_up(ll x){
        size[x] = size[ch[x][0]] + size[ch[x][1]] + 1;
        sum[x] = (sum[ch[x][0]] + sum[ch[x][1]] + val[x]) % p;
    }
    
    void push_down(ll x){
        ll l = ch[x][0], r = ch[x][1];
        if(mul[x] != 1){
            if(l) pushMul(l, mul[x]);
            if(r) pushMul(r, mul[x]);
        }
        if(add[x]){
            if(l) pushAdd(l, add[x]);
            if(r) pushAdd(r, add[x]);
        }
        if(rev[x]){
            if(l) pushRev(l);
            if(r) pushRev(r);
        }
        mul[x] = 1, add[x] = 0, rev[x] = 0;
    }
    
    void rotate(ll x){
        ll y = fa[x], z = fa[y], p = (ch[y][1] == x) ^ 1;
        push_down(y), push_down(x);
        ch[y][p^1] = ch[x][p], fa[ch[x][p]] = y;
        if(!isRoot(y)) ch[z][ch[z][1] == y] = x;
        fa[x] = z, fa[y] = x, ch[x][p] = y;
        push_up(y), push_up(x);
    }
    
    void splay(ll x){
        ll pos = 0; st[++pos] = x;
        for(ll i = x; !isRoot(i); i = fa[i]) st[++pos] = fa[i];
        while(pos) push_down(st[pos--]);
        while(!isRoot(x)){
            ll y = fa[x], z = fa[y];
            if(!isRoot(y)){
                if((ch[y][0] == x) ^ (ch[z][0] == y)) rotate(x);
                else rotate(y);
            }
            rotate(x);
        }
        push_up(x);
    }
    
    void access(ll x){
        for(ll pre = 0; x; pre = x, x = fa[x])
            splay(x), ch[x][1] = pre, push_up(x);
    }
    
    void makeRoot(ll x){
        access(x), splay(x), pushRev(x);
    }
    
    void split(ll x, ll y){
        makeRoot(x), access(y), splay(y);
    }
    
    ll findRoot(ll x){
        access(x), splay(x);
        while(ch[x][0]) x = ch[x][0];
        splay(x);
        return x;
    }
    
    void link(ll x, ll y){
        makeRoot(x);
        if(findRoot(y) != x) fa[x] = y;
        push_up(y);
    }
    
    void cut(ll x, ll y){
        makeRoot(x);
        if(findRoot(y) == x && fa[y] == x && !ch[y][0]){
            fa[y] = ch[x][1] = 0, push_up(x);
        }
    }
    
    int main(){
    
        p = 51061;
        scanf("%lld%lld", &n, &q);
        for(int i = 1; i <= n; i ++) newNode(1);
        for(int i = 1; i <= n - 1; i ++){
            ll u, v; scanf("%lld%lld", &u, &v);
            link(u, v);
        }
        while(q --){
            char ch[10]; scanf("%s", &ch);
            if(ch[0] == '+'){
                ll u, v; scanf("%lld%lld", &u, &v);
                ll c; scanf("%lld", &c);
                split(u, v), pushAdd(v, c);
            }
            else if(ch[0] == '-'){
                ll u, v; scanf("%lld%lld", &u, &v);
                ll x, y; scanf("%lld%lld", &x, &y);
                cut(u, v), link(x, y);
            }
            else if(ch[0] == '*'){
                ll u, v; scanf("%lld%lld", &u, &v);
                ll c; scanf("%lld", &c);
                split(u, v), pushMul(v, c);
            }
            else if(ch[0] == '/'){
                ll u, v; scanf("%lld%lld", &u, &v);
                split(u, v);
                printf("%lld
    ", sum[v] % p);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    subprocess模块
    面向对象进阶
    python---面向对象学习
    vim命令---存阅
    python基础-软件目录开发规范
    装饰器、迭代器、生成器
    Python基础类型
    使用Git来撤销修改
    使用Git去管理修改
    了解Git的工作区和暂存区
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10704649.html
Copyright © 2011-2022 走看看