1、题目大意:bzoj1798的lct版本

2、分析:这个把线段树改成splay就好

#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
namespace LinkCutTree{
    struct Node{
        Node *ch[2], *fa;
        LL sum, num;
        LL size;
        bool rev;
        LL mul, plu; 

        inline int which();

        inline void reverse(){
            if(this) rev ^= 1;
        }

        inline void pd();

        inline void maintain(){
            sum = (num + ch[0] -> sum + ch[1] -> sum) % 51061;
            size = (1 + ch[0] -> size + ch[1] -> size) % 51061;
        }

        Node();
    } *null = new Node, tree[100010], *pos[100010];

    Node::Node(){
        num = sum = 1;
        rev = false;
        ch[0] = ch[1] = fa = null;
        mul = 1;
        plu = 0;
        size = 1;
    }

    inline void Node::pd(){
            if(rev){
                swap(ch[0], ch[1]);
                ch[0] -> reverse();
                ch[1] -> reverse();
                rev = false;
            }
            if(ch[0] != null){
                ch[0] -> mul *= mul;
                ch[0] -> plu *= mul;
                ch[0] -> plu += plu;
                ch[0] -> num *= mul;
                ch[0] -> num += plu;
                ch[0] -> sum *= mul;
                ch[0] -> sum += plu * ch[0] -> size;
                ch[0] -> mul %= 51061;
                ch[0] -> plu %= 51061;
                ch[0] -> num %= 51061;
                ch[0] -> sum %= 51061;
            }
            if(ch[1] != null){
                ch[1] -> mul *= mul;
                ch[1] -> plu *= mul;
                ch[1] -> plu += plu;
                ch[1] -> num *= mul;
                ch[1] -> num += plu;
                ch[1] -> sum *= mul;
                ch[1] -> sum += plu * ch[1] -> size;
                ch[1] -> mul %= 51061;
                ch[1] -> plu %= 51061;
                ch[1] -> num %= 51061;
                ch[1] -> sum %= 51061;
            }
            mul = 1;
            plu = 0;
    }

    inline int Node::which(){
        if(fa == null || (this != fa -> ch[0] && this != fa -> ch[1])) return -1;
        return this == fa -> ch[1];
    }

    inline void rotate(Node *o){
        Node *p = o -> fa;
        int l = o -> which(), r = l ^ 1;
        o -> fa = p -> fa;
        if(p -> which() != -1) p -> fa -> ch[p -> which()] = o;
        p -> ch[l] = o -> ch[r];
        if(o -> ch[r]) o -> ch[r] -> fa = p;
        o -> ch[r] = p; p -> fa = o;
        o -> ch[r] -> maintain();
        o -> maintain();
    }

    inline void splay(Node *o){
        static stack<Node*> st;
        if(!o) return;
        Node *p = o;
        while(1){
            st.push(p);
            if(p -> which() == -1) break;
            p = p -> fa;
        }
        while(!st.empty()){
            st.top() -> pd(); st.pop();
        }

        while(o -> which() != -1){
            p = o -> fa;
            if(p -> which() != -1){
                if(p -> which() ^ o -> which()) rotate(o);
                else rotate(p);
            }
            rotate(o);
        }
    }

    inline void Access(Node *o){
        Node *y = null;
        while(o != null){
            splay(o);
            o -> ch[1] = y;
            o -> maintain();
            y = o; o = o -> fa;
        }
    }

    inline void MovetoRoot(Node *o){
        Access(o);
        splay(o);
        o -> reverse();
    }

    inline Node* FindRoot(Node *o){
        Access(o);
        splay(o);
        while(o -> ch[0] != null) o = o -> ch[0];
        return o;
    }

    inline void Link(Node *x, Node *y){
        MovetoRoot(x);
        x -> fa = y;
    }

    inline void Cut(Node *x, Node *y){
        MovetoRoot(x);
        Access(y);
        splay(y);
        y -> ch[0] = x -> fa = null;
        y -> maintain();
    }
}
int main(){
    using namespace LinkCutTree;
    null -> mul = 1;
    null -> size = 0;
    null -> plu = 0;
    null -> sum = 0;
    null -> num = 0;
    null -> ch[0] = null -> ch[1] = null -> fa = NULL;
    int n, q;
    scanf("%d%d", &n, &q);
    for(int i = 1; i <= n; i ++) pos[i] = &tree[i];
    for(int i = 1; i < n; i ++){
        int u, v;
        scanf("%d%d", &u, &v);
        Link(pos[u], pos[v]);
    }
    char op[5];
    int x1, y1, x2, y2, c;
    while(q --){
        scanf("%s", op);
        if(op[0] == '+'){
            scanf("%d%d%d", &x1, &y1, &c);
            MovetoRoot(pos[x1]);
            Access(pos[y1]);
            splay(pos[y1]);
            pos[y1] -> num += c;
            pos[y1] -> num %= 51061;
            pos[y1] -> sum += pos[y1] -> size * c;
            pos[y1] -> sum %= 51061;
            pos[y1] -> plu += c;
            pos[y1] -> plu %= 51061;
        }
        else if(op[0] == '-'){
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            Cut(pos[x1], pos[y1]);
            Link(pos[x2], pos[y2]);
        }
        else if(op[0] == '*'){
            scanf("%d%d%d", &x1, &y1, &c);
            MovetoRoot(pos[x1]);
            Access(pos[y1]);
            splay(pos[y1]);
            pos[y1] -> num *= c;
            pos[y1] -> num %= 51061;
            pos[y1] -> sum *= c;
            pos[y1] -> sum %= 51061;
            pos[y1] -> mul *= c;
            pos[y1] -> mul %= 51061;
            pos[y1] -> plu *= c;
            pos[y1] -> plu %= 51061;
        }
        else{
            scanf("%d%d", &x1, &y1);
            MovetoRoot(pos[x1]);
            Access(pos[y1]);
            splay(pos[y1]);
            pos[y1] -> sum %= 51061;
            printf("%lld\n", pos[y1] -> sum);
        }
    }
    return 0;
}

BZOJ2631——tree的更多相关文章

  1. bzoj2631: tree

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  2. [BZOJ2631]tree 动态树lct

    2631: tree Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 5171  Solved: 1754[Submit][Status][Discus ...

  3. [Link-Cut-Tree][BZOJ2631]Tree

    题面 Description: 一棵\(n\)个点的树,每个点的初始权值为\(1\).对于这棵树有\(q\)个操作,每个操作为以下四种操作之一: + u v c:将\(u\)到\(v\)的路径上的点的 ...

  4. bzoj2631 tree LCT 区间修改,求和

    tree Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 4962  Solved: 1697[Submit][Status][Discuss] Des ...

  5. bzoj2631: tree lct

    要打mul和add的lct 50000+的mod用unsigned int好了TAT (坑爹没打pc('\n');(静态)调了好久,样例竟然只输出一个,orz,也不提示PE T_T) #include ...

  6. BZOJ2631 tree(伍一鸣) LCT 秘制标记

    这个题一看就是裸地LCT嘛,但是我wa了好几遍,这秘制标记...... 注意事项:I.*对+有贡献 II.先下传*再下传+(因为我们已经维护了+,不能再让*对+产生贡献)III.维护+用到size # ...

  7. [bzoj2631]tree——lct

    Brief Description 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: u v c:将u到v的路径上的点的权值都加上自然数c: u1 v1 u2 ...

  8. BZOJ2631 tree 【LCT】

    题目 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的边( ...

  9. BZOJ2631: tree(LCT)

    Description 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2 ...

随机推荐

  1. Install R & RStudio for Ubuntu

    Install R r-project.org official source to install the latest R system. add R source   sudo vi /etc/ ...

  2. PHP-权限控制类

    http://blog.csdn.net/painsonline/article/details/7183679 <?php /** * 权限控制类 */ class include_purvi ...

  3. BPEL 实例教程

    http://www.oracle.com/technetwork/cn/articles/matjaz-bpel1-090722-zhs.html BPEL 实例教程 作者:Matjaz Juric ...

  4. JavaScriptCore框架介绍

    http://www.cocoachina.com/ios/20140409/8127.html 这个框架其实只是基于webkit中以C/C++实现的JavaScriptCore的一个包装,在旧版本i ...

  5. 使用Navicat导入导出表的数据做测试(转载)

    当我们对MySQL数据库进行了误操作,造成某个数据表中的部分数据丢失时,肯定就要利用备份的数据库,对丢失部分的数据进行导出.导入操作了.Navicat工具正好给我们提供了一个数据表的导入导出功能. 1 ...

  6. nodejs fs module

    fs.watchFile(filename[, options], listener)# Added in: v0.1.31 filename <String> | <Buffer& ...

  7. Java排序算法——选择排序

    import java.util.Arrays; //================================================= // File Name : Select_S ...

  8. 通过Unity3D制作天空盒

    1. 将全景图片转换为6面的立方体 最初的原始图片 通过PTGui 软件将图片分解为6个部分 2. 通过Unity进行操作 创建3D项目工程 将之前的6张图片导入到Assets中 创建一个Metria ...

  9. .NET Reflector Visual Studio Extension

    https://visualstudiogallery.msdn.microsoft.com/95789cdb-08f9-4dae-9b2f-fc45a452ad77/

  10. JS获取字符串实际长度(包含汉字)

    方法一: var jmz = {}; jmz.GetLength = function(str) { ///<summary>获得字符串实际长度,中文2,英文1</summary&g ...