5TLE求助

P4114 Qtree1

danefishhh @ 2019-10-23 16:17:03

开O2也只有六十分,这哪里能T呢...

#include<bits/stdc++.h>
using namespace std;
char s[10];
int n, num, dfs_clock, res;
int x[100010], y[100010], head[100010], w[100010], dfn[100010], rem[100010], f[100010], deep[100010], siz[100010], tp[100010], id[100010];
struct edge {
    int to, nxt, w;
} e[200010];
struct tree {
    int l, r, w, maxn;
} T[400010];
void add(int u, int v, int w) {
    e[++num].to = v;
    e[num].w = w;
    e[num].nxt = head[u];
    head[u] = num;
}
void dfs1(int u) {
    siz[u] = 1, deep[u] = deep[f[u]] + 1;
    int maxn = 0;
    for(int i = head[u]; i; i = e[i].nxt) {
        int v = e[i].to;
        if(v == f[u]) continue;
        f[v] = u, w[v] = e[i].w;
        dfs1(v);
        siz[u] += v;
        if(maxn < siz[v]) maxn = siz[v], rem[u] = v;
    }
}
void dfs2(int u, int k) {
    tp[u] = k, dfn[u] = ++dfs_clock, id[dfs_clock] = u;
    if(rem[u]) dfs2(rem[u], k);
    for(int i = head[u]; i; i = e[i].nxt) {
        int v = e[i].to;
        if(v == f[u] || v == rem[u]) continue;
        dfs2(v, v);
    }
}
void pushup(int rt) {
    T[rt].maxn = max(T[rt << 1].maxn, T[rt << 1 | 1].maxn);
}
void build(int rt, int L, int R) {
    T[rt].l = L, T[rt].r = R;
    if(L == R) {
        T[rt].maxn = w[id[L]];
        return ;
    }
    int mid = L + R >> 1;
    build(rt << 1, L, mid), build(rt << 1 | 1, mid + 1, R);
    pushup(rt);
}
void update(int rt, int L, int R, int k) {
    if(T[rt].l >= L && T[rt].r <= R) {
        T[rt].maxn = k;
        return ;
    }
    int mid = T[rt].l + T[rt].r >> 1;
    if(L <= mid) update(rt << 1, L, R, k);
    if(R > mid) update(rt << 1 | 1, L, R, k);
    pushup(rt);
}
void query(int rt, int L, int R) {
    if(T[rt].l >= L && T[rt].r <= R) {
        res = max(res, T[rt].maxn);
        return ;
    }
    int mid = T[rt].l + T[rt].r >> 1;
    if(L <= mid) query(rt << 1, L, R);
    if(R > mid) query(rt << 1 | 1, L, R);
}
void queryRange(int x, int y) {
    res = 0;
    while(tp[x] != tp[y]) {
        if(deep[tp[x]] < deep[tp[y]]) swap(x, y);
        query(1, dfn[tp[x]], dfn[x]);
        x = f[tp[x]];
    }
    if(x == y) return ;
    if(deep[x] < deep[y]) swap(x, y);
    query(1, dfn[y] + 1, dfn[x]);
}
int main() {
    scanf("%d", &n);
    for(int i = 1; i < n; i ++) {
        int z;
        scanf("%d%d%d", &x[i], &y[i], &z);
        add(x[i], y[i], z), add(y[i], x[i], z);
    }
    dfs1(1);
    dfs2(1, 1);
    build(1, 1, n);
    while(scanf("%s", s)) {
        if(s[0] == 'D') break;
        int i, t;
        if(s[0] == 'C') {
            scanf("%d%d", &i, &t);
            i = (deep[x[i]] < deep[y[i]] ? y[i] : x[i]);
            update(1, dfn[i], dfn[i], t);
        } else {
            scanf("%d%d", &i, &t);
            if(i == t) {
                printf("0\n");
                continue;
            }
            queryRange(i, t);
            printf("%d\n", res);
        }
    }
    return 0;
}

by danefishhh @ 2019-10-23 16:28:10

dfs1里siz[u] += siz[v] 写成 siz[u] += v 竟然有五十分...此贴终结已AC


by Retired @ 2021-09-23 21:28:53

ORZ,感谢老哥, 我和你犯了同样的错误,为啥能a50分啊!!!


|