Submission #978846

# Submission time Handle Problem Language Result Execution time Memory
978846 2024-05-09T19:11:10 Z VMaksimoski008 Addk (eJOI21_addk) C++17
92 / 100
305 ms 7604 KB
#include <bits/stdc++.h>
#define int long long

using namespace std;
using ll = long long;

struct BIT {
    int n;
    vector<ll> tree, val;

    void config(int _n) {
        n = _n + 10;
        tree.resize(_n+60);
        val.resize(_n+60);
    }

    void update(int p, int v) {
        val[p] += v;
        for(p++; p<n; p+=p&-p) tree[p] += v;
    }

    ll query(int p) {
        ll ans = 0;
        for(p++; p>0; p-=p&-p) ans += tree[p];
        return ans;
    }

    ll query(int l, int r) { return query(r) - query(l-1); }
    void set(int p, int v) { update(p, v - val[p]); }
};

int32_t main() {
    int n, k;
    cin >> n >> k;

    vector<int> v(n+2);
    for(int i=1; i<=n; i++) cin >> v[i];

    BIT bit1, bit2, bit3;
    bit1.config(n+5);
    bit2.config(n+5);
    bit3.config(n+5);

    auto set = [&](int val, int pos) {
        bit1.set(pos, val);
        bit2.set(pos, (ll)pos * val);
        bit3.set(pos, (ll)(n - pos + 1) * val);
    };

    for(int i=1; i<=n; i++) set(v[i], i);

    int q;
    cin >> q;

    while(q--) {
        int t;
        cin >> t;

        if(t == 1) {
            vector<int> a(k);
            for(int &x : a) cin >> x;
            a.push_back(a[0]);

            for(int i=1; i<=k; i++) {
                set(v[a[i]], a[i-1]);
                swap(v[a[i]], v[a[i-1]]);
            }

        } else {
            int l, r, m;
            cin >> l >> r >> m;

            if(r - l + 1 == m) {
                cout << bit1.query(l, r) << '\n';
                continue;
            }

            int mp = (l + r) / 2;
            int mx = min(mp, r - m + 1) - max(l, mp - m + 1) + 1;
            
            int lp=l, rp=mp-1, L=l, R=r;
            while(lp <= rp) {
                int mid = (lp + rp) / 2;
                int val = min(mid, r - m + 1) - max(l, mid - m + 1) + 1;
                if(val < mx) L = mid, lp = mid + 1;
                else rp = mid - 1;
            }

            lp=mp+1, rp=r;
            while(lp <= rp) {
                int mid = (lp + rp) / 2;
                int val = min(mid, r - m + 1) - max(l, mid - m + 1) + 1;
                if(val < mx) R = mid, rp = mid - 1;
                else lp = mid + 1;
            }

            ll ans = (bit2.query(l, L) - (l - 1) * bit1.query(l, L)) + (bit3.query(R, r) - (n - r) * bit1.query(R, r));
            if(R - L > 1) ans += bit1.query(L+1, R-1) * mx;
            cout << ans << '\n';
        }
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 5 ms 560 KB Output is correct
4 Correct 8 ms 604 KB Output is correct
5 Correct 9 ms 464 KB Output is correct
6 Correct 12 ms 772 KB Output is correct
7 Correct 13 ms 604 KB Output is correct
8 Correct 16 ms 888 KB Output is correct
9 Correct 22 ms 976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 1628 KB Output is correct
2 Correct 74 ms 2640 KB Output is correct
3 Correct 97 ms 3060 KB Output is correct
4 Correct 168 ms 5304 KB Output is correct
5 Correct 305 ms 7604 KB Output is correct
6 Correct 227 ms 7252 KB Output is correct
7 Correct 228 ms 7220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 143 ms 4028 KB Output isn't correct
2 Halted 0 ms 0 KB -