刚学1s的萌新机房调树剖求助

P4114 Qtree1

梧桐灯 @ 2019-09-25 17:28:41

RT,全WA QAQ

#include <cstdio>
using namespace std;

inline void read (int& s) {
    s = 0; int f = 0;
    static char c = getchar ();
    while (c < '0' || c > '9') {if (c == '-') f = 1; c = getchar ();}
    while (c >= '0' && c <= '9') s = (s << 3) + (s << 1) + (c & 15), c = getchar ();
    s = f ? -s : s; return ;
}

int qu[19];
inline void write (int s) {
    if (!s) {putchar ('0'); return ;}
    if (s < 0) {
        s = -s;
        putchar ('-');
    }
    int v = 0;
    while (s) qu[++v] = s % 10, s /= 10;
    while (v) putchar (qu[v--] + '0');
    return ;
}

inline int max (const int p, const int q) {return p > q ? p : q;}
inline void swap (int& p, int& q) {int t = p; p = q; q = t;}

const int N = 100003;
int n, h[N], tot, a[N];
int e[N][2];
struct stu {
    int v;
    int next;
    int w;
}s[N << 1];

inline void add (const int x, const int y, const int z) {
    ++tot;
    s[tot].v = y;
    s[tot].w = z;
    s[tot].next = h[x];
    h[x] = tot;
    return ;
}

int d[N], fa[N], son[N], sz[N];
int id[N], rv[N], top[N], k;

void dfs1 (const int x, const int pr) {
    d[x] = d[pr] + 1;
    fa[x] = pr;
    sz[x] = 1;
    int i, y; for (i = h[x]; i; i = s[i].next) {
        y = s[i].v;
        if (y == pr) continue;
        a[y] = s[i].w;
        dfs1 (y, x);
        sz[x] += sz[y];
        if (sz[y] > sz[son[x]]) son[x] = y;
    }
    return ;
}

void dfs2 (const int x, const int tf) {
    id[x] = ++k;
    rv[k] = x;
    top[x] = tf;
    if (son[x]) dfs2 (son[x], tf);
    int i, y; for (i = h[x]; i; i = s[i].next) {
        y = s[i].v;
        if (!id[x]) dfs2 (y, y);
    }
    return ;
}

int mx[N << 2];

void Bu (const int v, const int L, const int R) {
    if (L == R) {
        mx[v] = a[rv[L]];
        return ;
    }
    int mid = L + R >> 1;
    Bu (v << 1, L, mid);
    Bu (v << 1 | 1, mid + 1, R);
    mx[v] = max (mx[v << 1], mx[v << 1 | 1]);
    return ;
}

void chan (const int v, const int L, const int R, const int x, const int z) {
    if (L == R) {
        mx[v] = z;
        return ;
    }
    int mid = L + R >> 1;
    if (mid >= x) chan (v << 1, L, mid, x, z);
    else chan (v << 1 | 1, mid + 1, R, x, z);
    mx[v] = max (mx[v << 1], mx[v << 1 | 1]);
    return ;
}

int findmax (const int v, const int L, const int R, const int x, const int y) {
    if (L >= x && R <= y) return mx[v];
    int mid = L + R >> 1, r = -(1 << 30);
    if (mid >= x) r = findmax (v << 1, L, mid, x, y);
    if (mid < y) r = max (r, findmax (v << 1 | 1, mid + 1, R, x, y));
    return r;
}

inline void work (int x, int y, const int z) {
    if (id[x] < id[y]) swap (x, y);
    chan (1, 1, n, id[x], z);
    return ;
}

inline void ask (int x, int y) {
    int ans = -2147483647;
    while (top[x] != top[y]) {
        if (d[top[x]] < d[top[y]]) swap (x, y);
        if (id[top[x]] + 1 <= id[x]) ans = max (ans, findmax (1, 1, n, id[top[x]] + 1, id[x]));
        x = fa[top[x]];
    }
    if (d[x] > d[y]) swap (x, y);
    if (id[x] + 1 <= id[y]) ans = max (ans, findmax (1, 1, n, id[x] + 1, id[y]));
    write (ans), putchar ('\n');
    return ;
}

int main () {
    read (n);
    int i, x, y, z; for (i = 1; i < n; ++i) {
        read (x), read (y), read (z);
        e[i][0] = x, e[i][1] = y;
        add (x, y, z);
        add (y, x, z);
    }
    dfs1 (1, 0);
    dfs2 (1, 1);
    Bu (1, 1, n);
    char c; while (true) {
        c = getchar ();
        while (c != 'C' && c != 'Q' && c != 'D') c = getchar ();
        if (c == 'D') break;
        else if (c == 'C') {
            read (x), read (z);
            work (e[x][0], e[x][1], z);
        }
        else {
            read (x), read (y);
            if (x == y) puts ("0");
            else ask (x, y);
        }
    }
    return 0;
}

然而我并不想强调自己是不是妹子


by REFLAME_ASH @ 2019-09-25 17:39:11

大家快来帮帮这个妹子。。。。我们机房的这个妹子真的超级可爱!


by 雪颜 @ 2019-09-25 17:39:14

@光随影走

dfs2里面

if(!id[x])

应该是if(!id[y])吧


by 梧桐灯 @ 2019-09-25 17:40:06

@雪颜 orz 谢谢dalao %%


by 雪颜 @ 2019-09-25 17:40:53

@光随影走

只是我犯过这种错而已。。所以一来就先看这个了。。

我是全国最弱蒟蒻。。


by 梧桐灯 @ 2019-09-25 17:42:07

@REFLAME_ASH fake QwQ


by 梧桐灯 @ 2019-09-25 17:46:34

然而依然不对嘤嘤嘤 QwQ


by NaCly_Fish @ 2019-09-25 17:49:04

@光随影走 沿着重链向上跳的时候,top 那里不用 +1


by qwerty0862 @ 2019-09-25 17:49:41

%%%%%%


by NaCly_Fish @ 2019-09-25 17:50:06

不然统计的时候就忽略了每条重链的顶点


by 梧桐灯 @ 2019-09-25 17:51:25

@NaCly_Fish orz %% 谢谢dalao QwQ


| 下一页