답안 #978863

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
978863 2024-05-09T20:15:49 Z VMaksimoski008 Addk (eJOI21_addk) C++17
100 / 100
286 ms 11060 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]);

            int x = v[a[0]];
            for(int i=1; i<=k; i++) set(v[a[i]], a[i-1]);
            for(int i=1; i<=k; i++) v[a[i-1]] = v[a[i]];
            v[a[k-1]] = x;

        } 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 5 ms 604 KB Output is correct
4 Correct 8 ms 444 KB Output is correct
5 Correct 9 ms 604 KB Output is correct
6 Correct 13 ms 776 KB Output is correct
7 Correct 14 ms 860 KB Output is correct
8 Correct 16 ms 860 KB Output is correct
9 Correct 23 ms 1288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 2276 KB Output is correct
2 Correct 75 ms 3012 KB Output is correct
3 Correct 95 ms 3924 KB Output is correct
4 Correct 177 ms 6324 KB Output is correct
5 Correct 244 ms 8872 KB Output is correct
6 Correct 225 ms 8788 KB Output is correct
7 Correct 256 ms 8700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 134 ms 4944 KB Output is correct
2 Correct 229 ms 8112 KB Output is correct
3 Correct 286 ms 11060 KB Output is correct
4 Correct 257 ms 10068 KB Output is correct