#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> <, 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
3 ms |
660 KB |
Output is correct |
5 |
Correct |
4 ms |
604 KB |
Output is correct |
6 |
Correct |
4 ms |
908 KB |
Output is correct |
7 |
Correct |
6 ms |
860 KB |
Output is correct |
8 |
Correct |
6 ms |
860 KB |
Output is correct |
9 |
Correct |
9 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
2136 KB |
Output is correct |
2 |
Correct |
29 ms |
2684 KB |
Output is correct |
3 |
Correct |
39 ms |
4004 KB |
Output is correct |
4 |
Correct |
73 ms |
7432 KB |
Output is correct |
5 |
Correct |
103 ms |
8692 KB |
Output is correct |
6 |
Correct |
111 ms |
8660 KB |
Output is correct |
7 |
Correct |
102 ms |
8528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
5004 KB |
Output is correct |
2 |
Correct |
124 ms |
8152 KB |
Output is correct |
3 |
Correct |
192 ms |
10064 KB |
Output is correct |
4 |
Correct |
143 ms |
9044 KB |
Output is correct |