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 pb push_back
#define F first
#define S second
#define int long long
using namespace std;
const int N = 1e5 + 10;
const int mod = 998244353;
int st[N * 4],lazy[N * 4];
void push(int node,int tl,int tr) {
st[node] += (tr - tl + 1) * lazy[node];
if(tl != tr) {
lazy[node * 2] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
}
lazy[node] = 0;
}
void modify(int node,int tl,int tr,int l,int r,int val) {
push(node,tl,tr);
if(tl > r || tr < l) return;
if(tl >= l && tr <= r) {
lazy[node] += val;
push(node,tl,tr);
return;
}
push(node,tl,tr);
int mid = (tl + tr) / 2;
modify(node * 2,tl,mid,l,r,val);
modify(node * 2 + 1,mid + 1,tr,l,r,val);
st[node] = st[node * 2] + st[node * 2 + 1];
}
int get(int node,int tl,int tr,int l,int r) {
push(node,tl,tr);
if(tl > r || tr < l) return 0;
if(tl >= l && tr <= r) return st[node];
int mid = (tl + tr) / 2;
return get(node * 2,tl,mid,l,r) + get(node * 2 + 1,mid + 1,tr,l,r);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,k;
cin >> n >> k;
vector<int>a(n + 1);
for(int i = 1; i <= n; i++) cin >> a[i];
vector<int>pref(n + 1),Pref(n + 1);
for(int i = 1; i <= n; i++) pref[i] = pref[i - 1] + a[i];
for(int i = 1; i <= n; i++) Pref[i] = Pref[i - 1] + pref[i];
for(int i = 1; i <= n; i++) modify(1,1,n,i,i,pref[i]);
int q;
cin >> q;
while(q--) {
int type;
cin >> type;
if(type == 1) {
vector<int>b(k + 1),c(k + 1);
for(int i = 1; i <= k; i++) cin >> b[i];
for(int i = 1; i <= k - 1; i++) c[i] = b[i + 1];
c[k] = b[1];
vector<int>cpy = a;
for(int i = 1; i <= k; i++) {
cpy[b[i]] = a[c[i]];
modify(1,1,n,c[i],n,-a[c[i]]);
modify(1,1,n,b[i],n,a[c[i]]);
}
a = cpy;
}else {
int l,r,m,res = 0;
cin >> l >> r >> m;
int L = l + m - 1;
if(L <= r) res += get(1,1,n,1,r);
if(L <= r) res -= get(1,1,n,1,L - 1);
//if(L <= r) res += Pref[r] - Pref[L - 1];
int R = r - m + 1;
if(R >= l) res -= get(1,1,n,1,R - 1);
//if(R >= l) res -= Pref[R - 1];
//if(R >= l && l >= 2) res += Pref[l - 2];
if(R >= l && l >= 2) res += get(1,1,n,1,l - 2);
cout << res << "\n";
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |