Cod sursă (job #465929)

Utilizator avatar Constantin. Constantin Dragancea Constantin. IP ascuns
Problemă Rafaela (lot liceu) Compilator cpp | 2,62 kb
Rundă Arhiva de probleme Status evaluat
Dată 3 apr. 2019 20:12:22 Scor 0
#include <bits/stdc++.h>
using namespace std;
typedef pair <int,int> pii;

#define N 200010
int n, a[N], q, sz[N], pnod[N], mxc[N], chain[N], nrl = 1, cnt[N], ord[N], k, fw[N], bgn[N];
vector <int> v[N];

int p;
#define dim 100000
char buff[dim];

void read(int &nr){
    nr = 0;
    bool sgn = 0;
    while (buff[p] < '0' || buff[p] > '9'){
        if (buff[p] == '-') sgn = 1;
        if (++p == dim) fread(buff, 1, dim, stdin), p = 0;
    }
    while (buff[p] >='0' && buff[p] <='9'){
        nr = nr*10 + buff[p] - '0';
        if (++p == dim) fread(buff, 1, dim, stdin), p = 0;
    }
    if (sgn) nr = -nr;
}

void readc(char &cc){
    while (buff[p] < 'A' || buff[p] > 'Z')
        if (++p == dim) fread(buff, 1, dim, stdin), p = 0;
    cc = buff[p];
    if (++p == dim) fread(buff, 1, dim, stdin), p = 0;
}

void update(int pos, int val){
    for (; pos <= n; pos += (pos & -pos)) fw[pos] += val;
}

int get(int pos){
    int rs = 0;
    for (; pos; pos -= (pos & -pos)) rs += fw[pos];
    return rs;
}

int query(int x, int y){
    return (get(y) - get(x - 1));
}

int getsz(int q, int pr){
    sz[q] = 1;
    int mx = 0, idx;
    for (auto it: v[q]){
        if (it == pr) continue;
        pnod[it] = q;
        int tt = getsz(it, q);
        if (tt > mx) mx = tt, idx = it;
        sz[q] += tt;
    }
    if (mx) mxc[q] = idx;
    return sz[q];
}

void hld(int q, int pr){
    ++k;
    chain[q] = nrl;
    ord[q] = ++cnt[nrl];
    if (ord[q] == 1) bgn[nrl] = k;
    update(k, a[q]);
    if (mxc[q]) hld(mxc[q], q);
    for (auto it: v[q]){
        if (it == pr || it == mxc[q]) continue;
        nrl++; hld(it, q);
    }
}

int ask(int id){
    int aux = 0, stotal = get(n), mx = 0;
    for (auto it: v[id]){
        if (it == pnod[id]) continue;
        int st = bgn[chain[it]] + ord[it] - 1;
        int tt = query(st, st + sz[it]-1);
        if (tt > mx) mx = tt;
        aux += tt;
    }
    if (stotal-aux > mx) mx = stotal - aux;
    return mx;
}

int main(){
    freopen("rafaela.in", "r", stdin);
    freopen("rafaela.out", "w", stdout);
    read(n); read(q);
    for (int i=1, x, y; i<n; i++){
        read(x); read(y);
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for (int i=1; i<=n; i++) read(a[i]);
    getsz(1, -1);
    hld(1, -1);
    for (int i=1, nr, id; i<=q; i++){
        char c;
        readc(c);
        if (c == 'U'){
            read(nr); read(id);
            update(bgn[chain[id]]+ord[id]-1, nr);
        }
        else{
            read(id);
            printf("%d\n", ask(id));
        }
    }
    return 0;
}