답안 #636266

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
636266 2022-08-28T16:52:33 Z Valaki2 Addk (eJOI21_addk) C++14
0 / 100
157 ms 6044 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;
    }
    if(l2 == r2) {
        exit(1);
    }
    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);
    if((l2 + 1) <= (r2 - 1)) {
        res += m * 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];
            }
            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
*/
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 292 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 39 ms 2608 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 157 ms 6044 KB Output isn't correct
2 Halted 0 ms 0 KB -