답안 #531134

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
531134 2022-02-27T19:54:10 Z fabijan_cikac Addk (eJOI21_addk) C++17
100 / 100
86 ms 4648 KB
#include <bits/stdc++.h>

using namespace std;

#define ll long long int
#define F first
#define S second
#define pll pair<ll, ll>

const int N = 1e5 + 10;

int n, k, q;
ll a[N]; pll fen[N];

void update(int x, ll val){
    a[x] += val; ++x; int y = x;
    while (x <= n){
        fen[x] = {fen[x].F + val, fen[x].S + y * val}; x += x & (-x);
    }
}

pll query(int x){
    ++x; pll val = {0, 0};
    while (x > 0){
        val = {val.F + fen[x].F, val.S + fen[x].S}; x -= x & (-x);
    }
    return val;
}

pll range_sum(int l, int r){
    if (l > r) return {0, 0};
    pll val1 = query(l - 1); pll val2 = query(r);
    return {val2.F - val1.F, val2.S - val1.S - l * (val2.F - val1.F)};
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> k;
    for (int i = 0; i < n; ++i){
        int x; cin >> x; update(i, x);
    }
    cin >> q;
    for (int i = 0; i < q; ++i){
        int u; cin >> u;
        if (u == 1){
            vector<int> v; v.resize(k);
            for (int i = 0; i < k; ++i){
                cin >> v[i]; --v[i];
            }
            ll cp = a[v[0]];
            for (int i = 0; i < k - 1; ++i)
                update(v[i], a[v[i + 1]] - a[v[i]]);
            update(v[k - 1], cp - a[v[k - 1]]);
        }
        else{
            int l, r, m; cin >> l >> r >> m; --l; --r;
            int siz = (r - l + 1);
            if (siz >= 2 * m){
                ll sum1 = range_sum(l, l + m - 1).S;
                ll sum2 = m * range_sum(l + m, r - m).F; pll val = range_sum(r - m + 1, r);
                ll sum3 = (m + 1) * val.F - val.S;
                cout << sum1 + sum2 + sum3;
            }
            else if (m < siz){
                if (siz == 2 * m - 1){
                    ll sum1 = range_sum(l, l + m - 2).S;
                    ll sum2 = m * range_sum(l + m - 1, r - m + 1).F; pll val = range_sum(r - m + 2, r);
                    ll sum3 = m * val.F - val.S;
                    cout << sum1 + sum2 + sum3;
                }
                else{
                    ll sum1 = range_sum(l, l + (siz - m)).S;
                    ll sum2 = (siz - m + 1) * range_sum(l + (siz - m) + 1, r - (siz - m + 1)).F; pll val = range_sum(r - (siz - m), r);
                    ll sum3 = (siz - m + 2) * val.F - val.S;
                    cout << sum1 + sum2 + sum3;
                }
            }
            else cout << range_sum(l, r).F;
            cout << '\n';
        }
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 2 ms 404 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 3 ms 588 KB Output is correct
7 Correct 3 ms 588 KB Output is correct
8 Correct 4 ms 716 KB Output is correct
9 Correct 5 ms 892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 1356 KB Output is correct
2 Correct 18 ms 1796 KB Output is correct
3 Correct 22 ms 2120 KB Output is correct
4 Correct 38 ms 3340 KB Output is correct
5 Correct 61 ms 4500 KB Output is correct
6 Correct 50 ms 4348 KB Output is correct
7 Correct 52 ms 4368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 2728 KB Output is correct
2 Correct 53 ms 3760 KB Output is correct
3 Correct 86 ms 3796 KB Output is correct
4 Correct 60 ms 4648 KB Output is correct