搞不懂为啥这样写有问题
不就是线段树的板子吗!!!
为啥会有问题啊QAQ!!!
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
ll a[N];
struct Tree{
int l, r;
ll sum, mark;
}tree[N * 4];
void push_up(int i){
tree[i].sum = tree[i * 2 + 1].sum + tree[i * 2].sum;
return;
}
void push_down(int i){
if(tree[i].mark == 0)return;
tree[i * 2].mark += tree[i].mark;
tree[i * 2 + 1].mark += tree[i].mark;
int mid = (tree[i].l + tree[i].r) >> 1;
tree[i * 2].sum += tree[i].mark * (mid - tree[i].l + 1);
tree[i * 2 + 1].sum += tree[i].mark * (tree[i].r - mid);
tree[i].mark = 0;
return;
}
void build_tree(int l, int r, int i){
tree[i].l = l, tree[i].r = r, tree[i].mark = 0;
if(l == r){
tree[i].sum = a[l];
return;
}
int mid = (l + r) >> 1;
build_tree(l, mid, i * 2);
build_tree(mid + 1, r, i * 2 + 1);
push_up(i);
return;
}
void modify_tree(int i, int x, int y, int k){
if(x <= tree[i].l && y >= tree[i].r){
tree[i].sum += k * (tree[i].r - tree[i].l + 1);
tree[i].mark += k;
return;
}
push_down(i);
if(tree[i * 2].r >= x) modify_tree(i * 2, x, y, k);
if(tree[i * 2 + 1].l <= y) modify_tree(i * 2 + 1, x, y, k);
push_up(i);
return;
}
ll query_tree(int i, int x, int y){
ll ans = 0;
if(x <= tree[i].l && y >= tree[i].r)
return tree[i].sum;
if(x <= tree[i * 2].r) ans += query_tree(i * 2, x, y);
if(y >= tree[i * 2 + 1].l) ans += query_tree(i * 2 + 1, x, y);
return ans;
}
int main(){
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1;i <= n;i ++) scanf("%lld", &a[i]);
build_tree(1, n, 1);
for(int i = 0;i < m;i ++){
int p;
scanf("%d", &p);
if(p == 1){
int l, r, k;
scanf("%d%d%d", &l, &r, &k);
modify_tree(1, l, r, k);
}
else if(p == 2){
int k;
scanf("%d", &k);
modify_tree(1, 1, 1, k);
}
else if(p == 3){
int k;
scanf("%d", &k);
modify_tree(1, 1, 1, -k);
}
else if(p == 4){
int l, r;
scanf("%d%d", &l, &r);
printf("%lld\n", query_tree(1, l, r));
}
else
printf("%lld\n", query_tree(1, 1, 1));
}
return 0;
}