AmiyaCast @ 2022-10-21 16:52:04
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int N = 1e5 +7;
inline int read(){
int x = 0, f = 1;
char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return x * f;
}
int n, a[N];
struct Tree{
int l, r;
long long sum, lz;
}t[N * 4];
void build(int p, int l, int r)
{
t[p].l = l;
t[p].r = r;
if(l == r)
{
t[p].sum = a[l];
return ;
}
int mid = (l + r) >> 1;
build(p * 2, l, mid);
build(p * 2 + 1, mid + 1, r);
t[p].sum = t[p * 2].sum + t[p * 2 + 1].sum;
return ;
// puts("22");
}
void sp(int p)
{
if(t[p].lz)
{
t[p * 2].sum += t[p].lz * (t[p * 2].r- t[p * 2].l + 1);
t[p * 2 + 1].sum = t[p].lz * (t[p * 2 + 1].r - t[p * 2 + 1].l + 1);
t[p * 2].lz += t[p].lz;
t[p * 2 + 1].lz += t[p].lz;
t[p].lz = 0;
}
}
void ch(int p, int x, int y, int d)
{
int l = t[p].l;
int r = t[p].r;
if(l >= x && r <= y)
{
t[p].sum += (long long)d * (t[p].r - t[p].l + 1);
t[p].lz += d;
return ;
}
sp(p);
int mid = (l + r) >> 1;
if(x <= mid)
{
ch(p * 2, x, y, d);
}
if(y >= mid + 1)
{
ch(p * 2 + 1, x, y, d);
}
t[p].sum += t[p * 2].sum + t[p * 2 + 1].sum;
return ;
}
long long ask(int p, int x, int y)
{
int l = t[p].l;
int r = t[p].r;
if(l >= x && r <= y)
{
return t[p].sum;
}
sp(p);
int mid = (l + r) >> 1;
long long val = 0;
if(x <= mid)
{
val += ask(p * 2, x, y);
}
if(y >= mid + 1)
{
val += ask(p * 2 + 1, x, y);
}
return val;
}
int main(){
n = read();
int m = read();
for(int i = 1; i <= n; ++i)
{
a[i] = read();
}
build(1, 1, n);
for(int i = 1; i <= m; ++i)
{
int t = read();
int x = read();
int y = read();
if(t == 1)
{
int k = read();
ch(1, x, y, k);
}else{
printf("%lld\n", ask(1, x, y));
}
}
return 0;
}
/*
5 5
1 5 4 2 3
2 2 4
1 2 3 2
2 3 4
1 1 5 1
2 1 4
*/
by zdl777 @ 2022-10-21 17:16:08
第44行
t[p * 2 + 1].sum = t[p].lz * (t[p * 2 + 1].r - t[p * 2 + 1].l + 1);
改为
t[p * 2 + 1].sum += t[p].lz * (t[p * 2 + 1].r - t[p * 2 + 1].l + 1);
另外,第70行
t[p].sum += t[p * 2].sum + t[p * 2 + 1].sum;
改为
t[p].sum = t[p * 2].sum + t[p * 2 + 1].sum;
by AmiyaCast @ 2022-10-21 17:24:21
@zdl777 谢谢大佬!!!!
by AmiyaCast @ 2022-10-21 17:30:13
@zdl777 但是10分qwq /xk
by zdl777 @ 2022-10-21 17:32:25
@Amiya1 第44行没改啊……
by AmiyaCast @ 2022-10-21 17:37:54
@zdl777 已过!!谢谢dalao!!!Orz!!!