MLE一般出问题的点有哪些(新手入坑)

P3372 【模板】线段树 1

GyIxSB @ 2023-01-29 16:22:58

RT


by InversionShadow @ 2023-01-29 16:24:29

@GyIxSB 爆内存、爆栈空间(无限递归)


by yukimianyan @ 2023-01-29 16:33:49

其实你可以直接发代码的


by GyIxSB @ 2023-01-29 16:43:11

#include<bits/stdc++.h>
using namespace std;
int n, m;
const int maxn = 1e5 + 10;
int a[maxn], Ad[4 * maxn], fans[4 * maxn];
void build_tree(int left, int right, int k){
    if(left == right){
        fans[k] = a[left];
        return;
    }
    int mid = (left + right) / 2;
    build_tree(2 * k, left, mid);
    build_tree(2 * k + 1, mid + 1, right);
    fans[k] = fans[k * 2] + fans[k * 2 + 1];
    return;
}
void Add(int k, long long ans, int left, int right, int x, int y){
    if(x <= left && right <= y){
        Ad[k] += ans;
        return;
    }
    int mid = (left + right) / 2;
    fans[k] = ans * min(y, right) - ans * max(x, left) + ans;
    if(mid < y) Add(2 * k + 1, ans, mid + 1, right, x, y);
    if(x <= mid) Add(2 * k, ans, left, mid, x, y); 
}
long long Query(int k, int left, int right, int x, int y){
    if(x <= left && right <= y)
        return fans[k] + Ad[k] * right - Ad[k] * left + Ad[k];
    int mid = (left + right) / 2;
    long long ans = Ad[k] * min(y, right) - Ad[k] * max(x, left) + Ad[k];
    if(x <= mid) ans += Query(2 * k, left, mid, x, y);
    if(mid < y) ans += Query(2 * k + 1, mid + 1, right, x, y);
    return ans;
}
int main(){
    cin >> n >> m;
    for(int i=1;i<=n;i++) cin >> a[i];
    build_tree(1, n, 1);
    for(int i=1;i<=m;i++){
        int f, x, y;
        cin >> f >> x >> y;
        if(f == 1){
            long long k;
            cin >> k;
            Add(1, k, 1, n, x, y);
        }
        if(f == 2)
            cout <<  Query(1, 1, n, x, y) << endl;
    }
    return 0;
}

by GyIxSB @ 2023-01-29 16:43:40

@yukimianyan


by yukimianyan @ 2023-01-29 16:55:04

@GyIxSB build 传错参


by GyIxSB @ 2023-01-29 16:56:48

@yukimianyan 谢


|