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>
#define f first
#define s second
#define int long long
using namespace std;
const int N = 1e6 + 5;
int n , k , q,a[N] , Y[11] ,H[N];
pair<int, int> F[2*N];
void upd(int i , int d1 , int d2){
while(i <= n) {
F[i].f += d1 , F[i].s += d2;
i += i & -i;
}
}
pair<int , int> get(int i){
pair<int , int> sum = {0,0};
while(i) {
sum.f += F[i].f , sum.s += F[i].s;
i -= i & -i;
}
return sum;
}
main (){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; ++i)cin >> a[i];
for(int i = 1; i<= n; ++i) upd(i , a[i] , a[i]*i);
cin >> q;
while(q--){
int tp;
cin >> tp;
if(tp==1){
for(int i = 0; i < k; ++i)cin >> Y[i];
sort(Y , Y + k);
for(int i = 0; i < k; ++i) H[Y[i]] = a[Y[i]];
for(int i = 1; i < k; ++i) swap(a[Y[i]] , a[Y[i - 1]]);
for(int i = 0; i < k; ++i) upd(Y[i] , a[Y[i]] - H[Y[i]] , (a[Y[i]] - H[Y[i]])*Y[i]);
}else {
int l , r , m;
cin >> l >> r >> m;
m = min(m , r - l - m + 2);
int ans = 0;
pair<int,int> gR = get(l + m - 1);
pair<int,int> gL = get(l - 1);
ans += (gR.s - gL.s - (l - 1) * (gR.f - gL.f));
ans += (get(r - m).f - get(l + m - 1).f)*m;
gL = get(r-m);
gR = get(r);
ans += (r+1)*(gR.f-gL.f)-(gR.s-gL.s);
cout << ans << endl;
}
}
}
Compilation message (stderr)
Main.cpp:25:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
25 | main (){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |