本地卡死,测评机编译失败求助

P3384 【模板】重链剖分/树链剖分

LoveFrieren @ 2024-08-20 19:46:25

刚刚学完就来敲了,敲完还和答案核对了一遍,没任何差别(可能我有的地方没看到?)

代码:

#include<bits/stdc++.h>
using namespace std;
const int M = 1e5 + 10; typedef long long ll;
int n, m, r, p, cnt, wson[M], vl[M], sz[M], fa[M], dfn[M], rdfn[M], top[M], d[M];
ll sgt[M * 4];
vector<int> tr[M];
ll read(){
    int f = 1, a = 0;
    char c = getchar();
    while(c < '0' || c > '9'){
        if (c == '-') f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9'){
        a = (a << 1) + (a << 3) + (c ^ 48);
        c = getchar();
    }
    return f * a;
}
dfs1(int u, int f){//求父节点, 重儿子
    fa[u] = f;
    sz[u] = 1;
    for(int i = 0; i < tr[u].size(); ++i){
        int v = tr[u][i];
        if(v == f) continue;
        d[v] = d[u] + 1;
        dfs1(v, u);
        sz[u] += sz[v];
        if(sz[v] > sz[wson[u]]) wson[u] = v; 
    }
}
dfs2(int u, int topp){//求重链顶,dfs序
    dfn[u] = ++cnt;
    rdfn[cnt] = u;
    top[u] = topp;
    if(wson[u] != 0){
        dfs2(wson[u], topp);
        for(int i = 0; i < tr[u].size(); ++i)
            dfs2(tr[u][i], tr[u][i]);
    }
}
ll lzt[M * 4];//线段树,因为看过MLE的案例所以没写结构体,见谅
void pushup(int u){sgt[u] = sgt[u * 2] + sgt[u * 2 + 1] % p;}
void build(int u, int L, int R){
    if(L == R){
        sgt[u] = vl[rdfn[L]];
        return;
    }
    int mid = L + (R - L >> 1);
    build(u * 2, L, mid), build(u * 2 + 1, mid + 1, R);
    pushup(u);
}
void maketag(int u, int len, int vai){
    vl[u] += vai * len % p;
    lzt[u] += vai % p;
    lzt[u] %= p, vl[u] %= p;
}
void pushdown(int u, int L, int R){
    int mid = L + (R - L >> 1);
    maketag(u * 2, mid - L + 1, lzt[u]);
    maketag(u * 2 + 1, R - mid, lzt[u]);
    lzt[u] = 0;
}
void update(int u, int L, int R, int l, int r, int vai){
    if(l <= L && R <= r){
        maketag(u, R - L + 1, vai);
    }
    else if(!(r < L || R < l)){
        int mid = L + (R - L >> 1);
        pushdown(u, L, R);
        update(u * 2, L, mid, l, r, vai);
        update(u * 2 + 1, mid + 1, R, l, r, vai);
        pushup(u);
    }
}
ll query(int u, int L, int R, int l, int r){
    if(l <= L && R <= r)
        return sgt[u];
    else if(!(r < L || R < l)){
        int mid = L + (R - L >> 1);
        pushdown(u, L, R);
        return (query(u * 2, L, mid, l, r) + query(u * 2 + 1, mid + 1, R, l, r));
    }
    else return 0;
}
void upd(int u, int v, int vai){//题目描述上的1,2操作
    while(top[u] != top[v]){
        if(d[top[u]] < d[top[v]]) swap(u, v);
        update(1, 1, n, dfn[top[u]], dfn[u], vai);
        u = fa[top[u]];
    }
    update(1, 1, n, min(dfn[u], dfn[v]), max(dfn[u], dfn[v]), vai);
}
ll qry(int u, int v){
    ll re = 0;
    while(top[u] != top[v]){
        if(d[top[u]] < d[top[v]]) swap(u, v);
        re += query(1, 1, n, dfn[top[u]], dfn[u]) % p;
        re %= p;
        u = fa[top[u]];
    }
    re += query(1, 1, n, min(dfn[u], dfn[v]), max(dfn[u], dfn[v])) % p;
    return re % p;
}
int main(){
    n = read(), m = read(), r = read(), p = read();
    for(int i = 1; i <= n; ++i) vl[i] = read();
    for(int i = 1, u, v; i < n; ++i){
        u = read(), v = read();
        tr[u].push_back(v);
        tr[v].push_back(u);
    } 
    build(1, 1, n); ++d[r]; dfs1(r, 0); dfs2(r, 0);
    for(int i = 1, x, y, z, t; i <= m; ++i){
        t = read();
        if(t == 1){
            x = read(), y = read(), z = read();
            upd(x, y, z);
        }
        else if(t == 2){
            x = read(), y = read();
            cout << qry(x, y) << "\n";
        }
        else if (t == 3){
            x = read(), y = read();
            update(1, 1, n, dfn[x], dfn[x] + sz[x] - 1, y);
        }
        else{
            x = read();
            cout << query(1, 1, n, dfn[x], dfn[x] + sz[x] - 1) % p << "\n";
        }
    }
    return 0;
} 

by Grammar__hbw @ 2024-08-20 20:11:41

@ckkjz 树剖的时候传的区间有时候会+1


by Grammar__hbw @ 2024-08-20 20:13:22

@ckkjz dfs2错了,遍历树的时候要判断不等于父亲及重儿子,但你没有


by Grammar__hbw @ 2024-08-20 20:13:48

@ckkjz val * len会爆int


by LoveFrieren @ 2024-08-20 20:17:19

@Grammar__hbw 就是这个,ok了!(单纯是有输出了,对不对另说())


by LoveFrieren @ 2024-08-20 20:19:50

@Grammar__hbw 为什么有的时候会+1,不是从该节点到链顶吗,他们的dfs序不是连着的吗,为什么会有时候+1


by Grammar__hbw @ 2024-08-20 20:20:48

@ckkjz 改完应该就可以AC了qwqwq

取模不规范见祖宗

不开long long见祖宗

不判l>r见祖宗

dfs不规范见祖宗


by Grammar__hbw @ 2024-08-20 20:21:41

@ckkjz 有些题目lca不能算(主要是把边权转点权的题目)


by Grammar__hbw @ 2024-08-20 20:23:59

@ckkjz 重剖的dfs2的第二个参数应该是r,否则0作链顶见祖宗qwqwqwqwq


by Grammar__hbw @ 2024-08-20 20:24:55

@ckkjz 把我的线段树modify给你,你自己看看你少了什么

    void modify(int l,int r,int ll,int rr,int o,long long v){
        if(l<=ll&&rr<=r){val[o]=(val[o]+1ll*v*(rr-ll+1))%mod;lz[o]+=v;return;}
        pushdown(ll,rr,o);
        int mid=(ll+rr)>>1;
        if(l<=mid) modify(l,r,ll,mid,o*2,v);
        if(r>mid) modify(l,r,mid+1,rr,o*2+1,v);
        update(o);
    }

by Grammar__hbw @ 2024-08-20 20:36:15

@ckkjz 重剖的dfs2的第二个参数应该是r,否则0作链顶见祖宗qwqwqwqwq


上一页 | 下一页