比赛 20161116 评测结果 AAAAAAAAAA
题目名称 新型武器 最终得分 100
用户昵称 KZNS 运行时间 1.725 s
代码语言 C++ 内存使用 11.23 MiB
提交时间 2016-11-16 09:28:34
显示代码纯文本
//KZNS
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#define Nmax 300003
int N;
vector<int> gp[Nmax];
int V[Nmax];
int Dtm[Nmax];
int Dcrg[Nmax];
int deep[Nmax];
int mxdp;
int Bls[Nmax];
int Btm[Nmax];
int Bcrg[Nmax][2];
int ttmm = 1;
void DFS(int x) {
    Dtm[x] = ttmm++;
    for (int i = 0; i < gp[x].size(); i++)
        DFS(gp[x][i]);
    Dcrg[x] = ttmm-1;
}
void BFS() {
    int dp = 1;
    int bu = 1;
    Bcrg[dp][0] = bu;
    deep[1] = dp;
    Btm[1] = bu;
    Bls[bu++] = 1;
    Bcrg[dp++][1] = bu-1;
    int l = 1, r = 2;
    int u;
    while (bu <= N) {
        Bcrg[dp][0] = bu;
        for (int j = l; j < r; j++) {
            u = Bls[j];
            for (int i = 0; i < gp[u].size(); i++) {
                deep[gp[u][i]] = dp;
                Btm[gp[u][i]] = bu;
                Bls[bu++] = gp[u][i];
            }
        }
        l = Bcrg[dp][0];
        r = bu;
        Bcrg[dp++][1] = bu-1;
    }
    mxdp = dp;
}
int BIT[Nmax];
inline int lowbit(int a) {
    return a&-a;
}
inline int qbit(int x) {
    int ans = 0;
    for (int i = x; i; i -= lowbit(i))
        ans += BIT[i];
    return ans;
}
inline void adbit(int x, int v) {
    for (int i = x; i <= N; i += lowbit(i))
        BIT[i] += v;
}
void mkbit() {
    for (int i = 1; i <= N; i++)
        adbit(i, V[Bls[i]]);
}
void init() {
    scanf("%d", &N);
    for (int i = 1; i <= N; i++)
        scanf("%d", V+i);
    int a, b;
    for (int i = 1; i < N; i++) {
        scanf("%d %d", &a, &b);
        gp[a].push_back(b);
    }
    DFS(1);
    BFS();
    mkbit();
}
void work() {
    int Q;
    int tp;
    int u, v, c;
    int dp;
    int l, r, md;
    int ll, rr;
    scanf("%d", &Q);
    for (int q = 0; q < Q; q++) {
        scanf("%d", &tp);
        if (tp == 1) {
            scanf("%d %d", &u, &v);
            adbit(Btm[u], v-V[u]);
            V[u] = v;
        }
        else {
            scanf("%d %d", &u, &c);
            dp = deep[u] + c;
            if (dp < mxdp) {
                l = Bcrg[dp][0];
                r = Bcrg[dp][1];
                while (r - l > 1) {
                    md = l+r>>1;
                    if (Dtm[Bls[md]] >= Dtm[u])
                        r = md;
                    else
                        l = md;
                }
                if (Dtm[Bls[l]] >= Dtm[u])
                    ll = l;
                else if (Dtm[Bls[r]] >= Dtm[u])
                    ll = r;
                else {
                    printf("0\n");
                    continue;
                }
                l = Bcrg[dp][0];
                r = Bcrg[dp][1];
                while (r - l > 1) {
                    md = l+r>>1;
                    if (Dtm[Bls[md]] <= Dcrg[u])
                        l = md;
                    else
                        r = md;
                }
                if (Dtm[Bls[r]] <= Dcrg[u])
                    rr = r;
                else if (Dtm[Bls[l]] <= Dcrg[u])
                    rr = l;
                else {
                    printf("0\n");
                    continue;
                }
                if (ll <= rr)
                    printf("%d\n", qbit(rr) - qbit(ll-1));
                else
                    printf("0\n");
            }
            else {
                printf("0\n");
            }
        }
    }
}
int main() {
    freopen("newweapon.in", "r", stdin);
    freopen("newweapon.out", "w", stdout);
    init();
    work();
    return 0;
}
//UBWH