Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <vector>
- #include <array>
- #include <set>
- using namespace std;
- #define int long long
- const long long INF = 1e18 + 7;
- const int MAXN = 1e5 + 100;
- const int N = 1e5 + 10;
- const int MOD = 1e9 + 7;
- array<int, 4 * MAXN> tree, upd, simple;
- array<int, MAXN> a;
- void build(int x, int lx, int rx) {
- if (lx + 1 == rx) {
- tree[x] = a[lx] * (lx + 1);
- simple[x] = a[lx];
- return;
- }
- int mid = (rx + lx) / 2;
- build(2 * x + 1, lx, mid);
- build(2 * x + 2, mid, rx);
- tree[x] = tree[2 * x + 1] + tree[2 * x + 2];
- simple[x] = simple[2 * x + 1] + simple[2 * x + 2];
- }
- void push(int x, int lx, int rx) {
- if (lx + 1 != rx) {
- upd[2 * x + 1] += upd[x];
- upd[2 * x + 2] += upd[x];
- }
- int a1 = lx + 1, an = rx, n = rx - lx;
- tree[x] += upd[x] * (((a1 + an) * n) / 2);
- simple[x] += (rx - lx) * upd[x];
- upd[x] = 0;
- }
- void update(int x, int lx, int rx, int l, int r, int d) {
- push(x, lx, rx);
- if (lx >= r || rx <= l) {
- return;
- }
- if (lx >= l && rx <= r) {
- upd[x] += d;
- push(x, lx, rx);
- return;
- }
- int mid = (rx + lx) / 2;
- update(2 * x + 1, lx, mid, l, r, d);
- update(2 * x + 2, mid, rx, l, r, d);
- tree[x] = tree[2 * x + 1] + tree[2 * x + 2];
- simple[x] = simple[2 * x + 1] + simple[2 * x + 2];
- }
- int sum(int x, int lx, int rx, int l, int r) {
- push(x, lx, rx);
- if (lx >= r || rx <= l) {
- return 0;
- }
- if (lx >= l && rx <= r) {
- return tree[x] - simple[x] * l;
- }
- int mid = (rx + lx) / 2;
- return sum(2 * x + 1, lx, mid, l, r) + sum(2 * x + 2, mid, rx, l, r);
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n, m;
- cin >> n >> m;
- for (int i = 0; i < n; ++i) {
- cin >> a[i];
- }
- build(0, 0, n);
- for (int i = 0; i < m; ++i) {
- int type, l, r;
- cin >> type >> l >> r;
- --l;
- if (type == 1) {
- int d;
- cin >> d;
- update(0, 0, n, l, r, d);
- } else {
- cout << sum(0, 0, n, l, r) << '\n';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment