Submission #1039653

#TimeUsernameProblemLanguageResultExecution timeMemory
1039653juicyAddk (eJOI21_addk)C++17
100 / 100
192 ms10064 KiB
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #endif const int N = 1e5 + 5, K = 11; int n, k, q; int a[N], perm[K], val[K]; array<long long, 2> s[4 * N]; array<long long, 2> operator + (const array<long long, 2> &lt, const array<long long, 2> &rt) { return {lt[0] + rt[0], lt[1] + rt[1]}; } void upd(int i, int x, int id = 1, int l = 1, int r = n) { if (l == r) { s[id] = {(long long) i * x, x}; return; } int md = (l + r) / 2; if (i <= md) { upd(i, x, id * 2, l, md); } else { upd(i, x, id * 2 + 1, md + 1, r); } s[id] = s[id * 2] + s[id * 2 + 1]; } array<long long, 2> qry(int u, int v, int id = 1, int l = 1, int r = n) { if (u <= l && r <= v) { return s[id]; } int md = (l + r) / 2; if (v <= md) { return qry(u, v, id * 2, l, md); } if (md < u) { return qry(u, v, id * 2 + 1, md + 1, r); } return qry(u, v, id * 2, l, md) + qry(u, v, id * 2 + 1, md + 1, r); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i]; upd(i, a[i]); } cin >> q; while (q--) { int type; cin >> type; if (type == 1) { for (int i = 1; i <= k; ++i) { cin >> perm[i]; val[i] = qry(perm[i], perm[i])[1]; } for (int i = 1; i <= k; ++i) { upd(perm[i], val[i == k ? 1 : i + 1]); } } else { int l, r, m; cin >> l >> r >> m; int L = l + m - 1, R = r - m + 1; long long res = 0; if (L >= R) { auto dat = qry(l, R); res += dat[0] - (long long) (l - 1) * dat[1]; if (R + 1 < L) { res += (long long) (R - l + 1) * qry(R + 1, L - 1)[1]; } else { L = R + 1; } if (L <= r) { auto dat = qry(L, r); res += (long long) (R + m) * dat[1] - dat[0]; } } else { if (L + 1 < R) { res = (long long) m * qry(L + 1, R - 1)[1]; } auto dat = qry(l, L); res += dat[0] - (long long) (l - 1) * dat[1]; dat = qry(R, r); res += (long long) (R + m) * dat[1] - dat[0]; } cout << res << "\n"; } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...