0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-26 19:32:02 +00:00
OI-codes/S2OJ/162/data/std.cpp

64 lines
1.4 KiB (Stored with Git LFS)
C++

#include <cstdio>
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
using namespace std;
char buf[1 << 21], *p1 = buf, *p2 = buf, obuf[1 << 21], *O = obuf;
inline int read() {
int x = 0;
char ch = getchar();
bool flag = true;
while (ch < '0' || ch > '9') {
if (ch == '-') {
flag = false;
}
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return flag ? x : ~(x - 1);
}
const int N = 100005;
long long sum[N], sum2[N], s[N];
int n, m;
inline void change(int x, int y) {
long long p = x;
while (x <= n) {
sum[x] += y;
sum2[x] += p * y;
x += x & -x;
}
}
inline long long ask(int x) {
long long ans = 0;
int p = x;
while (x) {
ans += (p + 1) * sum[x] - sum2[x];
x -= x & -x;
}
return ans;
}
int main() {
n = read();
m = read();
for (register int i = 1; i <= n; i++) {
s[i] = read() + s[i - 1];
}
while (m--) {
int flag = read(), x = read(), y = read(), k;
if (flag == 1) {
k = read();
change(x, k);
change(y + 1, -k);
} else {
printf("%lld\n", ask(y) - ask(x - 1) + s[y] - s[x - 1]);
}
}
return 0;
}