#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define fi first
#define si second
#define ar array
typedef pair<int,int> pi;
typedef tuple<int,int,int> ti;
template<typename T> bool chmin(T &a, T b){return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chmax(T &a, T b){return (b > a) ? a = b, 1 : 0;}
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head _H, Tail... _T) {cerr<<" "<<to_string(_H);debug_out(_T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
const int MAXN = 1000010;
int n, k, q;
int arr[MAXN], ids[20];
struct node {
int s,e,m;
ll val, wsum;
node *l, *r;
node(int S,int E) {
s=S, e=E, m=(s+e)/2;
val = 0, wsum = 0;
if (s!=e) {
l = new node(s,m);
r = new node(m+1,e);
}
}
void set(int p, ll v) {
if (s==e) val = v, wsum = v;
else {
if (p <= m) l->set(p, v);
else r->set(p, v);
val = l->val+r->val;
wsum = l->wsum+r->wsum+r->val*(ll)(m-l->s+1);
}
}
ll query(int x, int y) {
if (s==x && e==y) return val;
else if (y <= m) return l->query(x,y);
else if (x > m) return r->query(x,y);
else return l->query(x,m)+r->query(m+1,y);
}
ll get(int x, int y) {
if (s==x && e==y) return wsum;
else if (y <= m) return l->get(x,y);
else if (x > m) return r->get(x,y);
else return l->get(x,m)+r->get(m+1,y)+r->query(m+1,y)*(ll)(m-x+1);
}
} *st, *st2;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin >> n >> k;
st = new node(1, n);
st2 = new node(1, n);
for (int i = 1; i <= n; ++i) {
cin >> arr[i];
st->set(i, arr[i]);
st2->set(n-i+1, arr[i]);
}
cin >> q;
while (q--) {
int op; cin >> op;
if (op == 1) {
for (int i = 1; i <= k; ++i) cin >> ids[i];
sort(ids+1,ids+1+k);
for (int i = 1; i <= k; ++i) {
if (i < k) swap(arr[ids[i]], arr[ids[i+1]]);
st->set(ids[i], arr[ids[i]]);
st2->set(n-ids[i]+1, arr[ids[i]]);
}
} else {
int l,r,m; cin >> l >> r >> m;
if (l+m-1 == r-m+1) {
cout << st->get(l,l+m-1)+st2->get(n-r+1,n-(r-m+2)+1) << '\n';
continue;
}
if (l+m-1 > r-m+1) m = r-l-m+2;
ll ans = st->get(l,l+m-1)+st2->get(n-r+1,n-(r-m+1)+1);
if (r-m+1 > l+m) ans += st->query(l+m,r-m) * (ll)m;
cout << ans << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
4 ms |
908 KB |
Output is correct |
4 |
Correct |
6 ms |
1112 KB |
Output is correct |
5 |
Correct |
7 ms |
1428 KB |
Output is correct |
6 |
Correct |
9 ms |
1748 KB |
Output is correct |
7 |
Correct |
11 ms |
1992 KB |
Output is correct |
8 |
Correct |
13 ms |
2260 KB |
Output is correct |
9 |
Correct |
21 ms |
3204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
6108 KB |
Output is correct |
2 |
Correct |
73 ms |
9092 KB |
Output is correct |
3 |
Correct |
106 ms |
11932 KB |
Output is correct |
4 |
Correct |
220 ms |
20940 KB |
Output is correct |
5 |
Correct |
343 ms |
29724 KB |
Output is correct |
6 |
Correct |
317 ms |
29644 KB |
Output is correct |
7 |
Correct |
310 ms |
29376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
234 ms |
15648 KB |
Output is correct |
2 |
Correct |
317 ms |
24180 KB |
Output is correct |
3 |
Correct |
642 ms |
31188 KB |
Output is correct |
4 |
Correct |
384 ms |
29992 KB |
Output is correct |