Submission #582635

# Submission time Handle Problem Language Result Execution time Memory
582635 2022-06-24T07:49:10 Z tengiz05 Addk (eJOI21_addk) C++17
0 / 100
83 ms 3656 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<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    
    Fenwick<i64> left(n), right(n), sum(n);
    
    auto add = [&](int i, int x) {
        left.add(i, (i + 1) * 1LL * x);
        right.add(i, (n - i) * 1LL * 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) / 2;
                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 ((r - l) % 2 != 0) {
                    ans += a[l + m] * m;
                }
            }
            
            cout << ans << "\n";
        }
    }
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 1580 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 83 ms 3656 KB Output isn't correct
2 Halted 0 ms 0 KB -