#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]);
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]];
} 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Correct |
7 ms |
604 KB |
Output is correct |
5 |
Correct |
9 ms |
604 KB |
Output is correct |
6 |
Correct |
11 ms |
604 KB |
Output is correct |
7 |
Correct |
16 ms |
776 KB |
Output is correct |
8 |
Correct |
21 ms |
860 KB |
Output is correct |
9 |
Correct |
23 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
1628 KB |
Output is correct |
2 |
Correct |
71 ms |
2384 KB |
Output is correct |
3 |
Correct |
101 ms |
3156 KB |
Output is correct |
4 |
Correct |
181 ms |
5232 KB |
Output is correct |
5 |
Correct |
245 ms |
7232 KB |
Output is correct |
6 |
Correct |
227 ms |
7128 KB |
Output is correct |
7 |
Correct |
226 ms |
7204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
132 ms |
3664 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |