无端 RE 求助 qwq

P3038 [USACO11DEC] Grass Planting G

DYYqwq @ 2024-02-23 23:17:05

#include<bits/stdc++.h>
#define lson(root) (root << 1)
#define rson(root) (root << 1 | 1)
using namespace std;
struct node
{
    int to , nxt;
}e[800010];
struct edge
{
    int sum , lazy;
}tree[1600010];
int n , m , tot , head[400010] , dep[400010] , sz[400010] , son[400010] , fa[400010] , tp[400010] , dfn[400010] , cnt = 0 , rk[400010];
void add(int u , int v)
{
    ++ tot;
    e[tot].to = v;
    e[tot].nxt = head[u];
    head[u] = tot;
}
void dfs(int u , int pa)
{
    dep[u] = dep[pa] + 1 , sz[u] = 1 , fa[u] = pa;
    for(int i = head[u] ; i != 0 ; i = e[i].nxt)
    {
        int v = e[i].to;
        if(v == pa) continue;
        dfs(v , u);
        sz[u] += sz[v];
        if(sz[son[u]] < sz[v]) son[u] = v;
    }
}
void dfs2(int u , int tp_fa)
{
    dfn[u] = ++ cnt , rk[cnt] = u , tp[u] = tp_fa;
    if(son[u]) dfs2(son[u] , tp_fa);
    for(int i = head[u] ; i != 0 ; i = e[i].nxt)
    {
        int v = e[i].to;
        if(v == fa[u] || v == son[u]) continue;
        dfs2(v , v); 
    }
}
inline void pushup(int root)
{
    tree[root].sum = tree[lson(root)].sum + tree[rson(root)].sum;
}
void pushdown(int root , int l , int r)
{
    if(tree[root].lazy == 0) return;
    int mid = (l + r) >> 1;
    tree[lson(root)].sum += tree[root].lazy * (mid - l + 1);
    tree[rson(root)].sum += tree[root].lazy * (r - mid);
    tree[lson(root)].lazy += tree[root].lazy;
    tree[rson(root)].lazy += tree[root].lazy;
    tree[root].lazy = 0;
}
void update(int root , int l , int r , int L , int R , int val)
{
    if(L <= l && R >= r)
    {
        tree[root].sum += val * (r - l + 1) , tree[root].lazy += val;
        return;
    }
    pushdown(root , l , r);
    int mid = (l + r) >> 1;
    if(L <= mid) update(lson(root) , l , mid , L , R , val);
    if(R > mid) update(rson(root) , mid + 1 , r , L , R , val);
    pushup(root);
}
int query(int root , int l , int r , int pos)
{
    if(l == r) return tree[root].sum;
    int mid = (l + r) >> 1;
    pushdown(root , l , r);
    if(pos <= mid) return query(lson(root) , l , mid , pos);
    else return query(rson(root) , mid + 1 , r , pos);
}
void uv_update(int u , int v)
{
    while(tp[u] != tp[v])
    {
        if(dep[u] < dep[v]) swap(u , v);
        update(1 , 1 , n , dfn[tp[u]] , dfn[u] , 1);
        u = fa[tp[u]];
    }
    if(dep[u] < dep[v]) swap(u , v);
    update(1 , 1 , n , dfn[v] , dfn[u] , 1);
    update(1 , 1 , n , dfn[v] , dfn[v] , -1);
}
int main()
{
    // freopen("P3038_2.in" , "r" , stdin);
    scanf("%d%d" , &n , &m);
    for(int i = 1 ; i < n ; i ++)
    {
        int u , v;
        scanf("%d%d" , &u , &v);
        add(u , v) , add(v , u);
    }
    dfs(1 , 0);
    dfs2(1 , 1);
    while(m --)
    {
        char op;
        int u , v;
        cin >> op , scanf("%d%d" , &u , &v);
        //if(op == 'P') uv_update(u , v);
        //else printf("%d\n" , query(1 , 1 , n , max(dfn[u] , dfn[v])));
    }
    return 0;
}

经过粗略的分段注释,应该是 dfs1dfs2 的问题吧()


by DYYqwq @ 2024-02-23 23:18:41

不对,是哪儿哪儿都炸


by DYYqwq @ 2024-02-23 23:18:53

/oh


by DYYqwq @ 2024-02-23 23:29:20

好现在 query 没有问题


by DYYqwq @ 2024-02-23 23:38:28

我草 A 了!!!!


by DYYqwq @ 2024-02-23 23:39:06

(果然 tlq 是用来自言自语的/kel


|