Submission #582653

#TimeUsernameProblemLanguageResultExecution timeMemory
582653tengiz05Addk (eJOI21_addk)C++17
100 / 100
168 ms6492 KiB
#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; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...