Submission #1025314

#TimeUsernameProblemLanguageResultExecution timeMemory
1025314coolboy19521Addk (eJOI21_addk)C++17
100 / 100
108 ms9556 KiB
#include "bits/stdc++.h" #define int long long using namespace std; const int sz = 1e5 + 5; const int sm = 11; int pf[sz], sf[sz]; int pj[sz], sj[sz]; int a[sz], b[sm]; int an, k; void add(int v, int k, int a[]) { for (; v <= an; v += v & -v) a[v] += k; } int sum(int l, int r, int a[]) { l --; int ps = 0; for (; 0 < l; l -= l & -l) ps += a[l]; int pe = 0; for (; 0 < r; r -= r & -r) pe += a[r]; return pe - ps; } void solve() { int t; cin >> t; if (1 == t) { for (int i = 0; i < k; i ++) cin >> b[i]; for (int j = 0; j < k; j ++) { int i = b[j]; add(i, -a[i], pf); add(i, i * -a[i], pj); } for (int j = 0; j < k; j ++) { int i = b[j]; add(i, -a[i], sf); add(i, (an - i + 1) * -a[i], sj); } for (int i = 0; i < k - 1; i ++) { int tx = (i + k - 1) % k; swap(a[b[i]], a[b[tx]]); } for (int j = 0; j < k; j ++) { int i = b[j]; add(i, a[i], pf); add(i, i * a[i], pj); } for (int j = 0; j < k; j ++) { int i = b[j]; add(i, a[i], sf); add(i, (an - i + 1) * a[i], sj); } return; } int l, r, m; cin >> l >> r >> m; int n = r - l + 1; int mxv = n - m + 1; int le = l + mxv - 2; int rs = r - mxv + 2; int pjl = sum(l, le, pj); int pfl = sum(l, le, pf); int fpl = pjl - pfl * (l - 1); int sjr = sum(rs, r, sj); int sfr = sum(rs, r, sf); int fsr = sjr - sfr * (an - r); int rsms = l + mxv - 1; int rsme = r - mxv + 1; int rsm = sum(rsms, rsme, pf); int tsm = fpl + fsr + rsm * mxv; cout << tsm << '\n'; } signed main() { cin.tie(nullptr)->sync_with_stdio(false); int n; cin >> n >> k; an = n; for (int i = 1; i <= n; i ++) cin >> a[i]; for (int i = 1; i <= n; i ++) { add(i, a[i], pf); add(i, i * a[i], pj); } for (int i = 1; i <= n; i ++) { int rx = n - i + 1; add(rx, a[rx], sf); add(rx, i * a[rx], sj); } int q; cin >> q; while (q --) solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...