HDU-4010 Query on The Trees(LCT)

Query on The Trees

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 6129    Accepted Submission(s): 2460


Problem Description
We have met so many problems on the tree, so today we will have a query problem on a set of trees. 
There are N nodes, each node will have a unique weight Wi. We will have four kinds of operations on it and you should solve them efficiently. Wish you have fun! 

 

Input
There are multiple test cases in our dataset. 
For each case, the first line contains only one integer N.(1 ≤ N ≤ 300000) The next N‐1 lines each contains two integers x, y which means there is an edge between them. It also means we will give you one tree initially. 
The next line will contains N integers which means the weight Wi of each node. (0 ≤ Wi ≤ 3000) 
The next line will contains an integer Q. (1 ≤ Q ≤ 300000) The next Q lines will start with an integer 1, 2, 3 or 4 means the kind of this operation. 
1. Given two integer x, y, you should make a new edge between these two node x and y. So after this operation, two trees will be connected to a new one. 
2. Given two integer x, y, you should find the tree in the tree set who contain node x, and you should make the node x be the root of this tree, and then you should cut the edge between node y and its parent. So after this operation, a tree will be separate into two parts. 
3. Given three integer w, x, y, for the x, y and all nodes between the path from x to y, you should increase their weight by w. 
4. Given two integer x, y, you should check the node weights on the path between x and y, and you should output the maximum weight on it. 
 

Output
For each query you should output the correct answer of it. If you find this query is an illegal operation, you should output ‐1. 
You should output a blank line after each test case.
 

Sample Input
  
  
5 1 2 2 4 2 5 1 3 1 2 3 4 5 6 4 2 3 2 1 2 4 2 3 1 3 5 3 2 1 4 4 1 4
 

Sample Output
  
  
3 -1 7

LCT,每次更新一条链的时候,先找到LCA然后更新LCA的左右儿子

#include<cstdio>
#include<string.h>
#include<queue>
#include<algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int MX = 3e5 + 5;
const int inf = 0x3f3f3f3f;
int n, m;
int ch[MX][2], pre[MX], val[MX];
int add[MX], rev[MX], Max[MX];
bool root[MX];
void init() {
    for (int i = 0; i <= n; i++) {
        ch[i][0] = ch[i][1] = 0;
        pre[i] = rev[i] = add[i] = 0;
        root[i] = 1;
    }
    Max[0] = -2000000000;
}
void up_val(int x, int d) {
    val[x] += d;
    add[x] += d;
    Max[x] += d;
}
void up_rev(int x) {
    swap(ch[x][0], ch[x][1]);
    rev[x] ^= 1;
}
void pushdown(int x) {
    if (add[x]) {
        if (ch[x][0]) up_val(ch[x][0], add[x]);
        if (ch[x][1]) up_val(ch[x][1], add[x]);
        add[x] = 0;
    }
    if (rev[x]) {
        if (ch[x][0]) up_rev(ch[x][0]);
        if (ch[x][1]) up_rev(ch[x][1]);
        rev[x] = 0;
    }
}
void pushup(int x) {
    Max[x] = max(max(Max[ch[x][0]], Max[ch[x][1]]), val[x]);
}
void Rotate(int x) {
    int y = pre[x], kind = ch[y][1] == x;
    ch[y][kind] = ch[x][!kind];
    pre[ch[y][kind]] = y;
    pre[x] = pre[y];
    pre[y] = x;
    ch[x][!kind] = y;
    if (root[y]) root[y] = 0, root[x] = 1;
    else ch[pre[x]][ch[pre[x]][1] == y] = x;
    pushup(y);
}
//P函数先将根结点到r的路径上所有的结点的标记逐级下放
void P(int x) {
    if (!root[x]) P(pre[x]);
    pushdown(x);
}
void Splay(int x) {
    P(x);
    while (!root[x]) {
        int f = pre[x], ff = pre[f];
        if (!root[f]) {
            if ((ch[ff][1] == f) == (ch[f][1] == x)) Rotate(f);
            else Rotate(x);
        }
        Rotate(x);
    }
    pushup(x);
}
int Access(int x) {
    int y = 0;
    while (x) {
        Splay(x);
        root[ch[x][1]] = 1;     //在辅助树中切除右子树(曾经的偏爱子节点)
        root[ch[x][1] = y] = 0;
        pushup(x);
        x = pre[y = x];
    }
    return y;
}
//判断是否是同根(真实的树,非splay)
bool judge(int u, int v) {
    while (pre[u]) u = pre[u];
    while (pre[v]) v = pre[v];
    return u == v;
}
//使r成为它所在的树的根
void makeroot(int x) {
    Access(x);
    Splay(x);
    up_rev(x);
}
//调用后u是原来u和v的lca
//v和ch[u][1]分别存着lca的2个儿子(原来u和v所在的2颗子树)
void lca(int &u, int &v) {
    Access(v), v = 0;
    while (u) {
        Splay(u);
        if (!pre[u])return;
        root[ch[u][1]] = 1;
        root[ch[u][1] = v] = 0;
        pushup(u);
        u = pre[v = u];
    }
}
void link(int u, int v) {
    if (judge(u, v)) {
        printf("-1\n");
        return;
    }
    makeroot(u);
    pre[u] = v;
}
//使u成为u所在树的根,并且v和它父亲的边断开
void cut(int u, int v) {
    if (u == v || !judge(u, v)) {
        printf("-1\n");
        return;
    }
    makeroot(u);
    Splay(v);
    pre[ch[v][0]] = pre[v];
    pre[v] = 0;
    root[ch[v][0]] = 1;
    ch[v][0] = 0;
    pushup(v);
}
void update(int u, int v, int w) {
    if (!judge(u, v)) {
        printf("-1\n");
        return;
    }
    lca(u, v);
    up_val(ch[u][1], w);
    up_val(v, w);
    val[u] += w;
    pushup(u);
}
int query(int u, int v) {
    if (!judge(u, v)) return -1;
    lca(u, v);
    return max(max(Max[v], Max[ch[u][1]]), val[u]);
}

PII edge[MX];
int main() {
    //freopen("in.txt", "r", stdin);
    while (~scanf("%d", &n)) {
        init();
        for (int i = 1, u, v; i < n; i++) {
            scanf("%d%d", &u, &v);
            edge[i] = PII(u, v);
        }
        for (int i = 1; i <= n; i++) {
            scanf("%d", &val[i]);
            Max[i] = val[i];
        }
        for (int i = 1; i < n; i++) link(edge[i].x, edge[i].y);
        scanf("%d", &m);
        int op, x, y, w;
        while (m--) {
            scanf("%d", &op);
            if (op == 1) {
                scanf("%d%d", &x, &y);
                link(x, y);
            }
            if (op == 2) {
                scanf("%d%d", &x, &y);
                cut(x, y);
            }
            if (op == 3) {
                scanf("%d%d%d", &w, &x, &y);
                update(x, y, w);
            }
            if (op == 4) {
                scanf("%d%d", &x, &y);
                printf("%d\n", query(x, y));
            }
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值