bzoj2157

来源:互联网 发布:js 点击div刷新页面 编辑:程序博客网 时间:2024/05/20 14:26

LCT板子,打个lazy即可

# include <stdio.h># include <stdlib.h># include <iostream># include <algorithm># include <string.h># include <map># define IL inline# define RG register# define Fill(a, b) memset(a, b, sizeof(a))using namespace std;typedef long long ll;IL ll Read(){    RG char c = getchar(); RG ll x = 0, z = 1;    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - '0';    return x * z;}const int MAXN(200010);int n, S[MAXN], ch[2][MAXN], fa[MAXN], mx[MAXN], mi[MAXN], sum[MAXN], rev[MAXN], w[MAXN], tag[MAXN];IL bool Son(RG int x){  return ch[1][fa[x]] == x;  }IL bool Isroot(RG int x){  return ch[0][fa[x]] != x && ch[1][fa[x]] != x;  }IL void Update(RG int x){    sum[x] = sum[ch[0][x]] + sum[ch[1][x]] + w[x];    mx[x] = max(mx[ch[0][x]], mx[ch[1][x]]);    mi[x] = min(mi[ch[0][x]], mi[ch[1][x]]);    if(x > n) mx[x] = max(mx[x], w[x]), mi[x] = min(mi[x], w[x]);}IL void Down(RG int x){  swap(mi[x], mx[x]); sum[x] = -sum[x]; mi[x] = -mi[x]; mx[x] = -mx[x]; w[x] = -w[x]; tag[x] ^= 1;  }IL void Pushdown(RG int x){    if(tag[x]){        tag[x] = 0;        if(ch[0][x]) Down(ch[0][x]);        if(ch[1][x]) Down(ch[1][x]);    }    if(rev[x]) rev[x] = 0, rev[ch[0][x]] ^= 1, rev[ch[1][x]] ^= 1, swap(ch[0][x], ch[1][x]);}IL void Rot(RG int x){    RG int y = fa[x], z = fa[y], c = Son(x);    if(!Isroot(y)) ch[Son(y)][z] = x; fa[x] = z;    ch[c][y] = ch[!c][x]; fa[ch[c][y]] = y;    ch[!c][x] = y; fa[y] = x; Update(y);}IL void Splay(RG int x){    RG int top = 0; S[++top] = x;    for(RG int y = x; !Isroot(y); y = fa[y]) S[++top] = fa[y];    while(top) Pushdown(S[top--]);    for(RG int y = fa[x]; !Isroot(x); Rot(x), y = fa[x])        if(!Isroot(y)) Son(x) ^ Son(y) ? Rot(x) : Rot(y);    Update(x);}IL void Access(RG int x){  for(RG int y = 0; x; y = x, x = fa[x]) Splay(x), ch[1][x] = y, Update(x);  }IL void Makeroot(RG int x){  Access(x); Splay(x); rev[x] ^= 1;  }IL int Findroot(RG int x){  Access(x); Splay(x); while(ch[0][x]) x = ch[0][x]; return x;  }IL void Split(RG int x, RG int y){  Makeroot(x); Access(y); Splay(y);  }IL void Link(RG int x, RG int y){  Makeroot(x); fa[x] = y;  }IL void Cut(RG int x, RG int y){  Split(x, y); fa[x] = ch[0][y] = 0;  }int main(RG int argc, RG char* argv[]){    n = Read();    for(RG int i = 0; i <= n; i++) mx[i] = -2e9, mi[i] = 2e9;    for(RG int i = 1; i < n; i++){        RG int u = Read() + 1, v = Read() + 1;        Link(u, n + i); Link(n + i, v);        sum[n + i] = mx[n + i] = mi[n + i] = w[n + i] = Read();    }    RG int Q = Read(), u, v; RG char c[10];    while(Q--){        scanf(" %s", c); u = Read(); v = Read();        if(c[0] == 'C') Splay(n + u), w[n + u] = v, Update(n + u);        else{            u++; v++; Split(u, v);            if(c[0] == 'N') Down(v);            else if(c[0] == 'S') printf("%d\n", sum[v]);            else if(c[1] == 'A') printf("%d\n", mx[v]);            else printf("%d\n", mi[v]);        }    }    return 0;}
原创粉丝点击