Submission #636275

# Submission time Handle Problem Language Result Execution time Memory
636275 2022-08-28T17:22:08 Z Valaki2 Addk (eJOI21_addk) C++14
100 / 100
382 ms 15752 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

class segtree {
private:
    int n;
    vector<int> tree;
public:
    segtree() : n(0), tree(0) {}
    segtree(int n_) : n(n_), tree(1 + (4 * n_), 0) {}

    void update(int pos, int val, int v, int vl, int vr) {
        if(vl == vr) {
            tree[v] = val;
        } else {
            int mid = (vl + vr) / 2;
            if(pos <= mid) {
                update(pos, val, 2 * v, vl, mid);
            } else {
                update(pos, val, 2 * v + 1, mid + 1, vr);
            }
            tree[v] = tree[2 * v] + tree[2 * v + 1];
        }
    }

    int query(int ql, int qr, int v, int vl, int vr) {
        if(ql > vr || qr < vl) {
            return 0;
        }
        if(ql == vl && qr == vr) {
            return tree[v];
        } else {
            int mid = (vl + vr) / 2;
            return query(ql, min(qr, mid), 2 * v, vl, mid)
                + query(max(ql, mid + 1), qr, 2 * v + 1, mid + 1, vr);
        }
    }
};

int n, k, q;
vector<int> v;
segtree sum, pref, suff;

void update_all(int i) {
    int x = v[i];
    sum.update(i, x, 1, 1, n);
    pref.update(i, i * x, 1, 1, n);
    suff.update(i, (n - i + 1) * x, 1, 1, n);
}

int ans_query(int l, int r, int m) {
    if(l == r) {
        return sum.query(l, r, 1, 1, n);
    }
    int l2 = -1, r2 = -1;
    /*if((r - l + 1) < (2 * m)) {
        int mid = (l + r) / 2;
        l2 = mid, r2 = mid + 1;
    } else {
        l2 = l + m - 1;
        r2 = r - m + 1;
    }*/
    l2 = min(l + m - 1, r - m + 1);
    r2 = max(l + m - 1, r - m + 1);
    if(l2 == r2) {
        r2++;
    }
    int res = 0;
    res += pref.query(l, l2, 1, 1, n) - (l - 1) * sum.query(l, l2, 1, 1, n);
    res += suff.query(r2, r, 1, 1, n) - ((n - r + 1) - 1) * sum.query(r2, r, 1, 1, n);
    int d = l2 - l + 1;
    if((l2 + 1) <= (r2 - 1)) {
        res += d * sum.query(l2 + 1, r2 - 1, 1, 1, n);
    }
    return res;
}

void solve() {
    cin >> n >> k;
    sum = segtree(n), pref = segtree(n), suff = segtree(n);
    v.assign(1 + n, 0);
    for(int i = 1; i <= n; i++) {
        cin >> v[i];
        update_all(i);
    }
    cin >> q;
    while(q--) {
        int type;
        cin >> type;
        if(type == 1) {
            vector<int> pos(1 + k, 0);
            vector<int> val(k, 0);
            for(int i = 0; i < k; i++) {
                cin >> pos[i];
            }
            if(k == 1) {
                continue;
            }
            pos[k] = pos[0];
            for(int i = 0; i < k; i++) {
                val[i] = v[pos[i + 1]];
            }
            for(int i = 0; i < k; i++) {
                v[pos[i]] = val[i];
            }
            for(int i = 0; i < k; i++) {
                update_all(pos[i]);
            }
        } else {
            int l, r, m;
            cin >> l >> r >> m;
            cout << ans_query(l, r, m) << "\n";
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}

/*
8 3
7 2 5 1 9 3 4 6
3
2 2 7 4
1 2 5 8
2 2 7 3
*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 3 ms 568 KB Output is correct
4 Correct 5 ms 724 KB Output is correct
5 Correct 6 ms 852 KB Output is correct
6 Correct 8 ms 972 KB Output is correct
7 Correct 10 ms 1108 KB Output is correct
8 Correct 12 ms 1236 KB Output is correct
9 Correct 17 ms 1656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 2556 KB Output is correct
2 Correct 57 ms 4412 KB Output is correct
3 Correct 79 ms 5836 KB Output is correct
4 Correct 143 ms 10188 KB Output is correct
5 Correct 209 ms 14472 KB Output is correct
6 Correct 199 ms 14188 KB Output is correct
7 Correct 219 ms 14172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 144 ms 5840 KB Output is correct
2 Correct 217 ms 11904 KB Output is correct
3 Correct 382 ms 15752 KB Output is correct
4 Correct 245 ms 14664 KB Output is correct