1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| #include <iostream> using namespace std;
const double eps = 1e-10; const double pi = 3.1415926535897932384626433832795; const double eln = 2.718281828459045235360287471352;
#define f(i, a, b) for (int i = a; i <= b; i++) #define LL long long #define IN freopen("in.txt", "r", stdin) #define OUT freopen("out.txt", "w", stdout) #define scan(x) scanf("%d", &x) #define mp make_pair #define pb push_back #define sqr(x) (x) * (x) #define pr1(x) printf("Case %lld: ", x) #define pn1(x) printf("Case %lld:\n", x) #define pr2(x) printf("Case #%d: ", x) #define pn2(x) printf("Case #%d:\n", x) #define lowbit(x) (x & (-x))
typedef unsigned long long ull;
const LL N = 1e5 + 86; LL n, m, a[N], d[N << 2], tag[N << 2];
void build(LL l, LL r, LL p) { if (l == r) { d[p] = a[l]; return; } LL m = l + ((r - l) >> 1); build(l, m, p << 1); build(m + 1, r, (p << 1) | 1); d[p] = d[p << 1] + d[(p << 1) | 1]; }
void update(LL l, LL r, LL c, LL nl, LL nr, LL p) { if (l <= nl && r >= nr) { d[p] += (nr - nl + 1) * c; tag[p] += c; return; } LL m = nl + ((nr - nl) >> 1); if (tag[p]) { tag[p << 1] += tag[p]; tag[(p << 1) | 1] += tag[p]; d[p << 1] += (m - nl + 1) * tag[p]; d[(p << 1) | 1] += (nr - m) * tag[p]; tag[p] = 0; } if (l <= m) update(l, r, c, nl, m, p << 1); if (r > m) update(l, r, c, m + 1, nr, (p << 1) | 1); d[p] = d[p << 1] + d[(p << 1) | 1]; return; }
LL getsum(LL l, LL r, LL nl, LL nr, LL p) { if (l <= nl && nr <= r) return d[p]; LL m = nl + ((nr - nl) >> 1); LL res = 0; if (tag[p]) { tag[p << 1] += tag[p]; tag[(p << 1) | 1] += tag[p]; d[p << 1] += (m - nl + 1) * tag[p]; d[(p << 1) | 1] += (nr - m) * tag[p]; tag[p] = 0; } if (l <= m) res += getsum(l, r, nl, m, p << 1); if (r > m) res += getsum(l, r, m + 1, nr, (p << 1) | 1); return res; }
int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; f(i, 1, n) cin >> a[i]; build(1, n, 1); f(i, 1, m) { int temp, x, y, k; cin >> temp; if (temp & 1) { cin >> x >> y >> k; update(x, y, k, 1, n, 1); } else { cin >> x >> y; cout << getsum(x, y, 1, n, 1) << '\n'; } } return 0; }
|