Submission #978863

#TimeUsernameProblemLanguageResultExecution timeMemory
978863VMaksimoski008Addk (eJOI21_addk)C++17
100 / 100
286 ms11060 KiB
#include <bits/stdc++.h> #define int long long using namespace std; using ll = long long; struct BIT { int n; vector<ll> tree, val; void config(int _n) { n = _n + 10; tree.resize(_n+60); val.resize(_n+60); } void update(int p, int v) { val[p] += v; for(p++; p<n; p+=p&-p) tree[p] += v; } ll query(int p) { ll ans = 0; for(p++; p>0; p-=p&-p) ans += tree[p]; return ans; } ll query(int l, int r) { return query(r) - query(l-1); } void set(int p, int v) { update(p, v - val[p]); } }; int32_t main() { int n, k; cin >> n >> k; vector<int> v(n+2); for(int i=1; i<=n; i++) cin >> v[i]; BIT bit1, bit2, bit3; bit1.config(n+5); bit2.config(n+5); bit3.config(n+5); auto set = [&](int val, int pos) { bit1.set(pos, val); bit2.set(pos, (ll)pos * val); bit3.set(pos, (ll)(n - pos + 1) * val); }; for(int i=1; i<=n; i++) set(v[i], i); int q; cin >> q; while(q--) { int t; cin >> t; if(t == 1) { vector<int> a(k); for(int &x : a) cin >> x; a.push_back(a[0]); int x = v[a[0]]; for(int i=1; i<=k; i++) set(v[a[i]], a[i-1]); for(int i=1; i<=k; i++) v[a[i-1]] = v[a[i]]; v[a[k-1]] = x; } else { int l, r, m; cin >> l >> r >> m; if(r - l + 1 == m) { cout << bit1.query(l, r) << '\n'; continue; } int mp = (l + r) / 2; int mx = min(mp, r - m + 1) - max(l, mp - m + 1) + 1; int lp=l, rp=mp-1, L=l, R=r; while(lp <= rp) { int mid = (lp + rp) / 2; int val = min(mid, r - m + 1) - max(l, mid - m + 1) + 1; if(val < mx) L = mid, lp = mid + 1; else rp = mid - 1; } lp=mp+1, rp=r; while(lp <= rp) { int mid = (lp + rp) / 2; int val = min(mid, r - m + 1) - max(l, mid - m + 1) + 1; if(val < mx) R = mid, rp = mid - 1; else lp = mid + 1; } ll ans = (bit2.query(l, L) - (l - 1) * bit1.query(l, L)) + (bit3.query(R, r) - (n - r) * bit1.query(R, r)); if(R - L > 1) ans += bit1.query(L+1, R-1) * mx; cout << ans << '\n'; } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...