2590 Tree Statistics

Lct see the solution to a problem is relatively small, so I have to contribute a

Note the place and pits

1. The only mmax [0] (maintained max) is initialized to a minimum value

2. sum [i], mmax [ i] can be assigned to val [i] in the input

link the first storage 3 point, the operation performed at the input link after completion of val

CHANGE operation

change (x, v) represents the val [x] into v

step:

1. The point x to the root splay

2. Update val [x] = v

3. pushup (x) Operation

QMAX and QSUM operation

Input x, y represents the maximum weight on the interrogation x, y, and the path or paths

step

1. Simply split (x, y), and then outputs mmax [y] or the sum [y] to

2. The Mmax [y] or SUM [y] x is the maximum value or the weight value and the weight on this chain represented by y

 

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define MN 4000001
#define re register int
#define ll long long
#define inf 0x7fffffff
using namespace std;
int f[MN], val[MN], sum[MN], r[MN], son[MN][2];
int mmax[MN], size[MN];
int fake1[MN], fake2[MN];
int zhan[MN];
int n, m, cnt;
int get(int x) {  //// Is determined whether a node Splay root (the difference between ordinary Splay. 1) 
    return Son [F [X]] [ 0 ] == X || Son [F [X]] [ . 1 ] == X; 
}   // / / if even the light side of his father, son there without it 
void a pushup ( int X) { 
    SUM [X] = SUM [son [X] [ 0 ]] + SUM [son [X] [ . 1 ]] + Val [X]; 
    Mmax [X] = max (max (Mmax [Son [X] [ 0 ]], Mmax [Son [X] [ . 1 ]]), Val [X]); 
} 
void the filp ( int X ) { 
    the swap (Son [X] [ 0 ], Son [X] [ . 1 ]); 
    R & lt [X] ^ = . 1  ;
}
void pushdown(int x) {
    if (!r[x])
        return;
    r[x] = 0;
    if (son[x][0])
        filp(son[x][0]);
    if (son[x][1])
        filp(son[x][1]);
}
void rotate(int x) {
    int y = f[x], z = f[y], k = (son[y][1] == x), s = son[x][!k];
    if (get(y))
        son[z][son[z][1] == y] = x;
    son[x][!k] = y;
    son[y][k] = s;
    if (s)
        f[s] = y;
    f[y] = x;
    f[x] = z;
    pushup(y);
    // pushup(x);
}
void splay(int x) {
    int y = x, top = 0;
    zhan[++top] = y;
    while (get(y)) zhan[++top] = f[y], y = f[y];
    while (top) pushdown(zhan[top--]);
    while (get(x)) {
        y = f[x], top = f[y];
        if (get(y))
            rotate((son[y][0] == x) ^ (son[top][0] == y) ? x : y);
        rotate(x);
    }
    pushup(x);
    return;
}
void access(int x) {
    for (re y = 0; x; y = x, x = f[x]) {
        splay(x);
        son[x][1] = y;
        pushup(x);
    }
}
void makeroot(int x) {
    access(x);
    splay(x);
    filp(x);
}
int findroot(int x) {
    access(x);
    splay(x);
    while (son[x][0]) pushdown(x), x = son[x][0];
    splay(x);
    return x;
}
void split(int x, int y) {
    makeroot(x);
    access(y);
    splay(y);
}
void cut(int x, int y) {
    split(x, y);
    if (findroot(y) == x && f[y] == x && !son[y][0]) {
        f[y] = son[x][1] = 0;
        pushup(x);
    }
    return;
}
void link(int x, int y) {
    makeroot(x);
    if (findroot(y) != x)
        f[x] = y;
}
void change(int x, int v) {
    splay(x);
    val[x] = v;
    pushup(x);
}
int main() {
    mmax[0 ] = - INF; 
    Scanf ( " % D " , & n-);
     for (Re I = . 1 ; I <= n-- . 1 ; I ++ ) { 
        Scanf ( " % D% D " , & [I] fake1, & fake2 [I]); 
    }   // prior to storage at link points, and the like after completion of the input operation val
     // crater 
    for (I = Re . 1 ; I <= n-; I ++ ) { 
        Scanf ( " % D " , & Val [I]); 
        [I] SUM = Mmax [I] = Val [I]; 
    } 
    int T;
     for (re i = 1; i <= n - 1; i++) link(fake1[i], fake2[i]);
    scanf("%d", &t);
    for (re i = 1; i <= t; i++) {
        char s[7];
        int a1, a2;
        scanf("%s", s);
        scanf("%d%d", &a1, &a2);
        if (s[0] == 'C') {
            change(a1, a2);
        }
        if (s[0] == 'Q' && s[1] == 'M') {
            split(a1, a2);
            printf("%d\n", mmax[a2]);
        }
        if (s[0] == 'Q' && s[1] == 'S') {
            split(a1, a2);
            printf("%d\n", sum[a2]);
        }
    }
    // for(re i=1;i<=n;i++)
    // printf("%d %d\n",mmax[i],sum[i]);

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/zw130-lzr-blogs/p/11220329.html