답안 #582653

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
582653 2022-06-24T08:12:34 Z tengiz05 Addk (eJOI21_addk) C++17
100 / 100
168 ms 6492 KB
#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

template <typename T>
struct Fenwick {
    int n;
    std::vector<T> a;
    Fenwick(int n) : n(n), a(n) {}
    void add(int x, T v) {
        for (int i = x + 1; i <= n; i += i & -i) {
            a[i - 1] += v;
        }
    }
    T sum(int x) {
        T ans = 0;
        for (int i = x; i > 0; i -= i & -i) {
            ans += a[i - 1];
        }
        return ans;
    }
    T rangeSum(int l, int r) {
        return sum(r) - sum(l);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n, k;
    cin >> n >> k;
    
    vector<i64> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    
    Fenwick<i64> left(n), right(n), sum(n);
    
    auto add = [&](int i, i64 x) {
        left.add(i, (i + 1) * x);
        right.add(i, (n - i) * x);
        sum.add(i, x);
    };
    
    for (int i = 0; i < n; i++) {
        add(i, a[i]);
    }
    
    int q;
    cin >> q;
    
    while (q--) {
        int type;
        cin >> type;
        
        if (type == 1) {
            vector<int> p(k);
            for (int i = 0; i < k; i++) {
                cin >> p[i];
                p[i]--;
            }
            
            int x = a[p[0]];
            for (int i = 0; i < k; i++) {
                add(p[i], -a[p[i]]);
                if (i < k - 1) {
                    a[p[i]] = a[p[i + 1]];
                    add(p[i], a[p[i]]);
                } else {
                    a[p[i]] = x;
                    add(p[i], a[p[i]]);
                }
            }
        } else {
            i64 l, r, m;
            cin >> l >> r >> m;
            l--;
            
            i64 ans = 0;
            
            if (2 * m <= r - l) {
                ans += left.rangeSum(l, l + m) - l * sum.rangeSum(l, l + m);
                ans += right.rangeSum(r - m, r) - (n - r) * sum.rangeSum(r - m, r);
                ans += sum.rangeSum(l + m, r - m) * m;
            } else {
                m = r - l - m + 1;
                ans += left.rangeSum(l, l + m) - l * sum.rangeSum(l, l + m);
                ans += right.rangeSum(r - m, r) - (n - r) * sum.rangeSum(r - m, r);
                if (2 * m - 1 == r - l) {
                    ans -= a[l + m - 1] * m;
                } else {
                    ans += sum.rangeSum(l + m, r - m) * m;
                }
            }
            
            cout << ans << "\n";
        }
    }
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 3 ms 468 KB Output is correct
7 Correct 3 ms 468 KB Output is correct
8 Correct 4 ms 600 KB Output is correct
9 Correct 6 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 1236 KB Output is correct
2 Correct 21 ms 1668 KB Output is correct
3 Correct 28 ms 2108 KB Output is correct
4 Correct 45 ms 3532 KB Output is correct
5 Correct 61 ms 4924 KB Output is correct
6 Correct 62 ms 4740 KB Output is correct
7 Correct 66 ms 4812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 2356 KB Output is correct
2 Correct 89 ms 5420 KB Output is correct
3 Correct 168 ms 5692 KB Output is correct
4 Correct 85 ms 6492 KB Output is correct