Submission #1025314

# Submission time Handle Problem Language Result Execution time Memory
1025314 2024-07-16T19:45:35 Z coolboy19521 Addk (eJOI21_addk) C++17
100 / 100
108 ms 9556 KB
#include "bits/stdc++.h"
#define int long long

using namespace std;

const int sz = 1e5 + 5;
const int sm = 11;

int pf[sz], sf[sz];
int pj[sz], sj[sz];
int a[sz], b[sm];
int an, k;

void add(int v, int k, int a[]) {
    for (; v <= an; v += v & -v)
        a[v] += k;
}

int sum(int l, int r, int a[]) {
    l --;
    int ps = 0;
    for (; 0 < l; l -= l & -l)
        ps += a[l];
    int pe = 0;
    for (; 0 < r; r -= r & -r)
        pe += a[r];
    return pe - ps;
}

void solve() {
    int t;
    cin >> t;

    if (1 == t) {
        for (int i = 0; i < k; i ++)
            cin >> b[i];
        for (int j = 0; j < k; j ++) {
            int i = b[j];
            add(i, -a[i], pf);
            add(i, i * -a[i], pj);
        }
        for (int j = 0; j < k; j ++) {
            int i = b[j];
            add(i, -a[i], sf);
            add(i, (an - i + 1) * -a[i], sj);
        }
        for (int i = 0; i < k - 1; i ++) {
            int tx = (i + k - 1) % k;
            swap(a[b[i]], a[b[tx]]);
        }
        for (int j = 0; j < k; j ++) {
            int i = b[j];
            add(i, a[i], pf);
            add(i, i * a[i], pj);
        }
        for (int j = 0; j < k; j ++) {
            int i = b[j];
            add(i, a[i], sf);
            add(i, (an - i + 1) * a[i], sj);
        }
        return;
    }

    int l, r, m;
    cin >> l >> r >> m;

    int n = r - l + 1;
    int mxv = n - m + 1;
    int le = l + mxv - 2;
    int rs = r - mxv + 2;
    int pjl = sum(l, le, pj);
    int pfl = sum(l, le, pf);
    int fpl = pjl - pfl * (l - 1);
    int sjr = sum(rs, r, sj);
    int sfr = sum(rs, r, sf);
    int fsr = sjr - sfr * (an - r);
    int rsms = l + mxv - 1;
    int rsme = r - mxv + 1;
    int rsm = sum(rsms, rsme, pf);
    int tsm = fpl + fsr + rsm * mxv;

    cout << tsm << '\n';
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int n;
    cin >> n >> k;

    an = n;

    for (int i = 1; i <= n; i ++)
        cin >> a[i];

    for (int i = 1; i <= n; i ++) {
        add(i, a[i], pf);
        add(i, i * a[i], pj);
    }

    for (int i = 1; i <= n; i ++) {
        int rx = n - i + 1;
        add(rx, a[rx], sf);
        add(rx, i * a[rx], sj);
    }

    int q;
    cin >> q;

    while (q --)
        solve();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 3 ms 2804 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 3 ms 2652 KB Output is correct
8 Correct 3 ms 2908 KB Output is correct
9 Correct 4 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 3676 KB Output is correct
2 Correct 17 ms 4188 KB Output is correct
3 Correct 18 ms 4688 KB Output is correct
4 Correct 30 ms 6480 KB Output is correct
5 Correct 49 ms 8104 KB Output is correct
6 Correct 42 ms 7976 KB Output is correct
7 Correct 56 ms 8020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 5716 KB Output is correct
2 Correct 47 ms 7252 KB Output is correct
3 Correct 108 ms 9556 KB Output is correct
4 Correct 51 ms 8532 KB Output is correct