全RE,求调
查看原帖
全RE,求调
1023054
Kuyohana楼主2024/7/21 11:04

本地测试时能过样例,但交上去后就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;
}

2024/7/21 11:04
加载中...