萌新刚学树剖,0分全WA求助
查看原帖
萌新刚学树剖,0分全WA求助
198719
洛璟楼主2021/4/1 20:55

也不知道哪里错了,样例过了但是交上去全WA了wq

#include<bits/stdc++.h>
using namespace std;
struct cccp
{
    int tg, s, ma;
}t[500010];
string s;
int a[100010];
int n, m, mo;
int nxt[200010], h[200010], ver[200010], tot;
int dep[100010], fa[100010], son[100010], siz[100010], newid[100010], cnt, nws[100010], top[100010];
inline int read()
{
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c>'9')
    {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        x = (x << 3) + (x << 1) + (c ^ '0');
        c = getchar();
    }
    return x * f;
}
inline void add(int x, int y)
{
    tot++;
    nxt[tot] = h[x];
    h[x] = tot;
    ver[tot] = y;
}
inline void build(int x, int l, int r)
{
    if (l == r)
    {
        t[x].s = nws[l];
        t[x].ma = nws[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(x * 2, l, mid);
    build(x * 2 + 1, mid + 1, r);
    t[x].s = t[x * 2].s + t[x * 2 + 1].s;
    t[x].ma = max(t[x].ma, max(t[x * 2].ma, t[x * 2 + 1].ma));
    return;
}
inline void modify(int x, int e, int l, int r, int k)
{
    if (l == e && r == e)
    {
        t[x].ma = max(t[x].ma, k);
        t[x].s = k;
        return;
    }
    int mid = (l + r) >> 1;
    if (e <= mid)
    {
        modify(x * 2, e, l, mid, k);
    }
    if (e > mid)
    {
        modify(x * 2 + 1, e, mid + 1, r, k);
    }
    t[x].s = t[x * 2].s + t[x * 2 + 1].s;
    t[x].ma = max(t[x * 2].ma, max(t[x].ma, t[x * 2 + 1].ma));
    return;
}
inline int query(int x, int el, int er, int l, int r)
{
    if (l >= el && r <= er)
    {
        return t[x].s;
    }
    int mid = (l + r) >> 1;
    int querys = 0;
    if (el <= mid)
    {
        querys += query(x * 2, el, er, l, mid);
    }
    if (er > mid)
    {
        querys += query(x * 2 + 1, el, er, mid + 1, r);
    }
    return querys;
}
inline int queryma(int x, int el, int er, int l, int r)
{
    if (l >= el && r <= er)
    {
        return t[x].ma;
    }
    int mid = (l + r) >> 1;
    int querys = -1919810214;
    if (el <= mid)
    {
        querys = max(queryma(x * 2, el, er, l, mid), querys);
    }
    if (er > mid)
    {
        querys = max(queryma(x * 2 + 1, el, er, mid + 1, r), querys);
    }
    return querys;
}
inline int search(int x, int y)//查询链
{
    int ans = 0;
    while (top[x] != top[y])
    {
        if (dep[top[x]] < dep[top[y]]) swap(x, y);
        ans += query(1, newid[top[x]], newid[x], 1, n);;
        x = fa[top[x]];
    }
    if (dep[x] > dep[y]) swap(x, y);
    ans += query(1, newid[x], newid[y], 1, n);
    return ans;
}
inline int searchma(int x, int y)//查询链
{
    int ans = -1919810214;
    while (top[x] != top[y])
    {
        if (dep[top[x]] < dep[top[y]]) swap(x, y);
        ans = max(ans, queryma(1, newid[top[x]], newid[x], 1, n));
        x = fa[top[x]];
    }
    if (dep[x] > dep[y]) swap(x, y);
    ans = max(queryma(1, newid[x], newid[y], 1, n), ans);
    return ans;
}
inline void dfs1(int x, int f)
{
    int maxs = -1;
    dep[x] = dep[f] + 1;
    fa[x] = f;
    siz[x] = 1;
    for (int i = h[x];i != 0;i = nxt[i])
    {
        int y = ver[i];
        if (y == f) continue;
        dfs1(y, x);
        siz[x] += siz[y];
        if (siz[y] > maxs)
        {
            maxs = siz[y];
            son[x] = y;
        }
    }
}
inline void dfs2(int x, int tf)
{
    newid[x] = ++cnt;
    nws[cnt] = a[x];
    top[x] = tf;
    if (son[x] == 0) return;
    dfs2(son[x], tf);
    for (int i = h[x];i != 0;i = nxt[i])
    {
        int y = ver[i];
        if (y == fa[x] || y == son[x]) continue;
        dfs2(y, y);
    }
}
int main()
{
    n = read();
    for (int i = 1;i < n;++i)
    {
        int x = read(), y = read();
        add(x, y);
        add(y, x);
    }
    for (int i = 1;i <= n;++i)
    {
        a[i] = read();
    }
    dfs1(1, 0);
    dfs2(1, 1);
    build(1, 1, n);
    m = read();
    for (int i = 1;i <= m;++i)
    {
        cin >> s;
        if (s == "CHANGE")
        {
            int x = read(), y = read();
            modify(1, x, 1, n, y);
        }
        if (s == "QMAX")
        {
            int x = read(), y = read();
            printf("%d\n", searchma(x, y));
        }
        if (s == "QSUM")
        {
            int x = read(), y = read();
            printf("%d\n", search(x, y));
        }
    }
    return 0;
}
2021/4/1 20:55
加载中...