BZOJ 1036 树链剖分

 

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036

题意:中文题目

思路:树链剖分入门题, 剖分后用线段树维护即可。 

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<time.h>
#include<cmath>
#include<sstream>
#include<assert.h>
using namespace std;
#define L(x) x<<1
#define R(x) x<<1|1
typedef long long int LL;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
const int MAXN = 30000 + 10;
int val[MAXN], head[MAXN], tot, cnt;
struct Edge{
    int to,next;
    Edge(int _to = 0, int _next = 0) :to(_to), next(_next){};
}Edges[MAXN * 2];
void add(int u, int v){
    Edges[tot].to = v;
    Edges[tot].next = head[u];
    head[u] = tot++;
}
int id[MAXN], son[MAXN], deep[MAXN], size[MAXN], fa[MAXN], reid[MAXN], top[MAXN];
void Init(){
    tot = 0; cnt = 0;
    memset(head, -1, sizeof(head));
    memset(son, -1, sizeof(son));
}
void DFS1(int u, int p,int dep){
    fa[u] = p; size[u] = 1; deep[u] = dep;
    for (int i = head[u]; i != -1; i = Edges[i].next){
        if (Edges[i].to != p){
            DFS1(Edges[i].to, u,dep+1);
            size[u] += size[Edges[i].to];
            if (son[u] == -1 || size[Edges[i].to] > size[son[u]]){
                son[u] = Edges[i].to;
            }
        }
    }
}
void DFS2(int u, int tp){
    id[u] = ++cnt; reid[id[u]] = u; top[u] = tp;
    if (son[u] == -1){ return; }
    DFS2(son[u], tp);
    for (int i = head[u]; i != -1; i = Edges[i].next){
        if (son[u] != Edges[i].to&&Edges[i].to != fa[u]){
            DFS2(Edges[i].to, Edges[i].to);
        }
    }
}
struct Node{
    int st, ed;
    LL sum, Max;
}Seg[MAXN * 4];
void Build(int l, int r, int k){
    Seg[k].st = l; Seg[k].ed = r; 
    if (l == r){
        Seg[k].sum = val[reid[l]]; Seg[k].Max = val[reid[l]];
        return;
    }
    int mid = (l + r) / 2;
    Build(l, mid, L(k)); Build(mid + 1, r, R(k));
    Seg[k].sum = Seg[L(k)].sum + Seg[R(k)].sum;
    Seg[k].Max = max(Seg[L(k)].Max, Seg[R(k)].Max);
}
void pushUp(int k){
    Seg[k].sum = Seg[L(k)].sum + Seg[R(k)].sum;
    Seg[k].Max = max(Seg[L(k)].Max, Seg[R(k)].Max);
}
void Modify(int pos, int k,int val){
    if (Seg[k].st ==Seg[k].ed){
        Seg[k].sum = val;
        Seg[k].Max = val;
        return;
    }
    if (pos <= Seg[L(k)].ed){
        Modify(pos, L(k), val);
    }
    else{
        Modify(pos, R(k), val);
    }
    pushUp(k);
}
LL QuerySum(int l, int r, int k){
    if (Seg[k].st == l&&Seg[k].ed == r){
        return Seg[k].sum;
    }
    LL sum = 0;
    if (r <= Seg[L(k)].ed){
        sum = QuerySum(l, r, L(k));
    }
    else if (l >= Seg[R(k)].st){
        sum = QuerySum(l, r, R(k));
    }
    else{
        sum = QuerySum(l, Seg[L(k)].ed, L(k)) + QuerySum(Seg[R(k)].st, r, R(k));
    }
    pushUp(k);
    return sum;
}
LL QuerySum(int u,int v){
    LL ans = 0;
    int f1 = top[u], f2 = top[v];
    while (f1!=f2){
        if (deep[f1] < deep[f2]){
            swap(f1, f2);
            swap(u, v);
        }
        ans += QuerySum(id[f1], id[u],1);
        u = fa[f1]; f1 = top[u];
    }
    if (deep[u] > deep[v]){
        swap(u, v);
    }
    ans += QuerySum(id[u],id[v], 1);
    return ans;
}
LL QueryMax(int l, int r, int k){
    if (Seg[k].st == l&&Seg[k].ed == r){
        return Seg[k].Max;
    }
    LL _Max = -inf;
    if (r <= Seg[L(k)].ed){
        _Max = QueryMax(l, r, L(k));
    }
    else if (l >= Seg[R(k)].st){
        _Max = QueryMax(l, r, R(k));
    }
    else{
        _Max = max(QueryMax(l, Seg[L(k)].ed, L(k)), QueryMax(Seg[R(k)].st, r, R(k)));
    }
    pushUp(k);
    return _Max;
}
LL QueryMax(int u,int v){
    LL ans = -inf;
    int f1 = top[u], f2 = top[v];
    while (f1 != f2){
        if (deep[f1] < deep[f2]){
            swap(f1, f2);
            swap(u, v);
        }
        ans = max(ans, QueryMax(id[f1], id[u], 1));
        u = fa[f1]; f1 = top[u];
    }
    if (deep[u] > deep[v]){
        swap(u, v);
    }
    ans = max(ans, QueryMax(id[u], id[v], 1));
    return ans;
}
int main(){
//#ifdef kirito
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
//#endif
//    int start = clock();
    int n, m;
    while (~scanf("%d",&n)){
        Init();
        for (int i = 1; i < n; i++){
            int u, v;
            scanf("%d%d", &u, &v);
            add(u, v); add(v, u);
        }
        for (int i = 1; i <= n; i++){
            scanf("%d", &val[i]);
        }
        DFS1(1, 1, 0); DFS2(1, 1); Build(1, n, 1);
        scanf("%d", &m);
        while (m--){
            int x, a; char ope[10];
            scanf("%s%d%d", ope,&x,&a);
            switch (ope[1])
            {
            case 'M':printf("%lld\n", QueryMax(x, a)); break;
            case 'S':printf("%lld\n", QuerySum(x,a)); break;
            default: Modify(id[x], 1, a);  break;
            }
        }
    }
//#ifdef LOCAL_TIME
//    cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
    return 0;
}

 

posted @ 2017-02-28 13:35  キリト  阅读(145)  评论(0编辑  收藏  举报