全RE,求调

P3372 【模板】线段树 1

Kuyohana @ 2024-07-21 11:04:20

本地测试时能过样例,但交上去后就RE

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N = 1e5 + 1;

int a[N], n, m;

struct tree{
    int l, r;
    ll pre, add;    
}tree[4 * N];

void build(int p,int l,int r){
    tree[p].l = l;
    tree[p].r = r;
    if(l == r){
        tree[p].pre = a[l];
        return;
    }
    int mid = l + r >> 1;
    build(p * 2,l , mid);
    build(p * 2 + 1,mid + 1, r);
    tree[p].pre = tree[p * 2].pre + tree[p * 2 + 1].pre;
}

void SpreadLazy(int p){ 
    if(tree[p].add){
        tree[p * 2].pre += tree[p].add * (tree[p * 2].r - tree[p * 2].l + 1);
        tree[p * 2 + 1].pre += tree[p].add * (tree[p * 2 + 1].r - tree[p * 2 + 1].l + 1);
        tree[p * 2].add += tree[p].add;
        tree[p * 2 + 1].add = tree[p].add;
        tree[p].add = 0; 
    }
}

void query(int p,int x,int y,int z){
    if(x <= tree[p].l && y >= tree[p].r){
        tree[p].pre += ll(z) * (tree[p].r - tree[p].l + 1);
        tree[p].add += z;
        return; 
    } 
    SpreadLazy(p);
    int mid = tree[p].l + tree[p].r >> 1;
    if(x <= mid)query(p * 2,x ,y, z);
    if(y >= mid)query(p * 2 + 1,x ,y, z);
    tree[p].pre = tree[p * 2].l + tree[p * 2 + 1].pre; 
}

ll ask(int p,int x,int y){
    if(x <= tree[p].l && y >= tree[p].r)return tree[p].pre;
    SpreadLazy(p);
    int mid = tree[p].l + tree[p].r >> 1;
    ll ans = 0;
    if(x <= mid) ans += ask(p * 2,x ,y);
    if(y > mid) ans += ask(p * 2 + 1,x ,y);
    return ans;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> m;
    for(int i = 1;i <= n;i ++)cin >> a[i];
    build(1, 1, n);
    for(int i = 1;i <= m;i ++){
        int a, x, y, k;
        cin >> a;
        if(a == 1){
            cin >> x >> y >> k;
            query(1, x, y, k);
        }
        if(a == 2){
            cin >> x >> y;
            cout << ask(1, x, y) << endl;
        }
    }
    return 0;
}

by quanquhengzhezou @ 2024-07-21 11:15:49

@Ayxrakzil 你的改了也RE


by quanquhengzhezou @ 2024-07-21 11:17:26

结帖

----------------------------------------------------------------


by Ayxrakzil @ 2024-07-21 11:17:42

@quanquhengzhezou 我一共说了三个错误,第一个错误改了RE,但是肯定是错的,第二个改了之后变成WA,是导致RE的根本原因,第三个改完之后才能AC


上一页 |