This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
class segtree {
private:
int n;
vector<int> tree;
public:
segtree() : n(0), tree(0) {}
segtree(int n_) : n(n_), tree(1 + (4 * n_), 0) {}
void update(int pos, int val, int v, int vl, int vr) {
if(vl == vr) {
tree[v] = val;
} else {
int mid = (vl + vr) / 2;
if(pos <= mid) {
update(pos, val, 2 * v, vl, mid);
} else {
update(pos, val, 2 * v + 1, mid + 1, vr);
}
tree[v] = tree[2 * v] + tree[2 * v + 1];
}
}
int query(int ql, int qr, int v, int vl, int vr) {
if(ql > vr || qr < vl) {
return 0;
}
if(ql == vl && qr == vr) {
return tree[v];
} else {
int mid = (vl + vr) / 2;
return query(ql, min(qr, mid), 2 * v, vl, mid)
+ query(max(ql, mid + 1), qr, 2 * v + 1, mid + 1, vr);
}
}
};
int n, k, q;
vector<int> v;
segtree sum, pref, suff;
void update_all(int i) {
int x = v[i];
sum.update(i, x, 1, 1, n);
pref.update(i, i * x, 1, 1, n);
suff.update(i, (n - i + 1) * x, 1, 1, n);
}
int ans_query(int l, int r, int m) {
if(l == r) {
return sum.query(l, r, 1, 1, n);
}
int l2 = -1, r2 = -1;
/*if((r - l + 1) < (2 * m)) {
int mid = (l + r) / 2;
l2 = mid, r2 = mid + 1;
} else {
l2 = l + m - 1;
r2 = r - m + 1;
}*/
l2 = min(l + m - 1, r - m + 1);
r2 = max(l + m - 1, r - m + 1);
if(l2 == r2) {
r2++;
}
int res = 0;
res += pref.query(l, l2, 1, 1, n) - (l - 1) * sum.query(l, l2, 1, 1, n);
res += suff.query(r2, r, 1, 1, n) - ((n - r + 1) - 1) * sum.query(r2, r, 1, 1, n);
int d = l2 - l + 1;
if((l2 + 1) <= (r2 - 1)) {
res += d * sum.query(l2 + 1, r2 - 1, 1, 1, n);
}
return res;
}
void solve() {
cin >> n >> k;
sum = segtree(n), pref = segtree(n), suff = segtree(n);
v.assign(1 + n, 0);
for(int i = 1; i <= n; i++) {
cin >> v[i];
update_all(i);
}
cin >> q;
while(q--) {
int type;
cin >> type;
if(type == 1) {
vector<int> pos(1 + k, 0);
vector<int> val(k, 0);
for(int i = 0; i < k; i++) {
cin >> pos[i];
}
if(k == 1) {
continue;
}
pos[k] = pos[0];
for(int i = 0; i < k; i++) {
val[i] = v[pos[i + 1]];
}
for(int i = 0; i < k; i++) {
v[pos[i]] = val[i];
}
for(int i = 0; i < k; i++) {
update_all(pos[i]);
}
} else {
int l, r, m;
cin >> l >> r >> m;
cout << ans_query(l, r, m) << "\n";
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
/*
8 3
7 2 5 1 9 3 4 6
3
2 2 7 4
1 2 5 8
2 2 7 3
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |