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

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 LoveFrieren @ 2024-08-20 19:49:13

刚刚发现快读返回值写的ll(),不删了,留着给大家嘲笑


by LoveFrieren @ 2024-08-20 19:49:59

@ckkjz 不对和这个没关系


by Grammar__hbw @ 2024-08-20 19:51:50

dfs1和dfs2没返回 @ckkjz


by CarroT1212 @ 2024-08-20 19:53:00

@ckkjz dfs1 dfs2 前面不加类型不应该过得了洛谷编译吧

本地卡死就不知道了


by liuyutao_ @ 2024-08-20 19:53:51

@ckkjz 做为一名小白,我表示完全看不懂。


by LoveFrieren @ 2024-08-20 19:55:58

@Grammar__hbw 感谢感谢


by qczrz6v4nhp6u @ 2024-08-20 19:57:00

maketag 里面,vai*len 是不是会爆 int


by Grammar__hbw @ 2024-08-20 19:57:32

@CarroT1212 不加类型会被默认int,然后就得到了“int函数不return见祖宗”


by LoveFrieren @ 2024-08-20 20:01:18

@Grammar__hbw 加了还是不行[裂开]


by JOKER_chu @ 2024-08-20 20:01:43

在线膜拜


| 下一页