#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define sumof(x) accumulate(all(x), 0ll)
struct ST_node{
int x;
ST_node (int a=0): x(a){}
friend ST_node sus(const ST_node& a, const ST_node& b){
return ST_node(a.x+b.x);
}
};
struct SegmentTree{
int n; vector<ST_node> t;
void init(int _n){
n=_n;
t.assign(4*n+1, ST_node());
}
void build(int k, int l, int r, int* a){
if (l==r){
t[k]=ST_node(a[l]);
return;
}
int mid=(l+r)>>1;
build(k<<1, l, mid, a);
build(k<<1|1, mid+1, r, a);
t[k]=sus(t[k<<1], t[k<<1|1]);
}
void init(int _n, int* a){
init(_n);
build(1, 1, n, a);
}
void update(int k, int l, int r, int pos, int val){
if (l==r){
t[k]=ST_node(val);
return;
}
int mid=(l+r)>>1;
if (pos<=mid) update(k<<1, l, mid, pos, val);
else update(k<<1|1, mid+1, r, pos, val);
t[k]=sus(t[k<<1], t[k<<1|1]);
}
ST_node get(int k, int l, int r, int L, int R){
if (r<L || R<l) return t[0];
if (L<=l && r<=R) return t[k];
int mid=(l+r)>>1;
return sus(get(k<<1, l, mid, L, R), get(k<<1|1, mid+1, r, L, R));
}
void update(int pos, int val){
update(1, 1, n, pos, val);
}
int get(int l, int r){
if (l>r) return 0;
return get(1, 1, n, l, r).x;
}
} st, stl, str;
const int N=1e5+1;
int n, k, q, a[N];
void solve(int tc){
// cout << "Case #" << tc << ": ";
cin >> n >> k;
for (int i=1; i<=n; ++i) cin >> a[i];
st.init(n);
stl.init(n);
str.init(n);
cin >> q;
function<int(int, int)> getl=[&](int l, int r) -> int {
return stl.get(l, r)-st.get(l, r)*(l-1);
};
function<int(int, int)> getr=[&](int l, int r) -> int {
return str.get(l, r)-st.get(l, r)*(n-r);
};
function<void(int, int)> update=[&](int i, int x) -> void {
st.update(i, x);
stl.update(i, x*i);
str.update(i, x*(n-i+1));
};
for (int i=1; i<=n; ++i) update(i, a[i]);
while (q--){
int o; cin >> o;
if (o==1){
vector<int> v(k);
for (int& i:v) cin >> i;
for (int i=0; i<sz(v); ++i){
int j=(i+1)%sz(v);
update(v[i], a[v[j]]);
}
int tmp=a[v[0]];
for (int i=0; i<sz(v)-1; ++i) a[v[i]]=a[v[i+1]];
a[v.back()]=tmp;
}else{
int l, r, m;
cin >> l >> r >> m;
int max_val=min(m, (r-l+1)-m+1);
int i1=l+max_val-2, i2=r-max_val+2;
cout << getl(l, i1)+getr(i2, r)+st.get(i1+1, i2-1)*max_val << '\n';
}
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int ntests=1;
// cin >> ntests;
for (int i=1; i<=ntests; ++i) solve(i);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
4 ms |
596 KB |
Output is correct |
5 |
Correct |
6 ms |
724 KB |
Output is correct |
6 |
Correct |
7 ms |
904 KB |
Output is correct |
7 |
Correct |
8 ms |
980 KB |
Output is correct |
8 |
Correct |
9 ms |
1108 KB |
Output is correct |
9 |
Correct |
14 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
2644 KB |
Output is correct |
2 |
Correct |
46 ms |
3788 KB |
Output is correct |
3 |
Correct |
62 ms |
4940 KB |
Output is correct |
4 |
Correct |
129 ms |
8508 KB |
Output is correct |
5 |
Correct |
165 ms |
12004 KB |
Output is correct |
6 |
Correct |
159 ms |
11748 KB |
Output is correct |
7 |
Correct |
162 ms |
11912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
5804 KB |
Output is correct |
2 |
Correct |
154 ms |
9468 KB |
Output is correct |
3 |
Correct |
260 ms |
11172 KB |
Output is correct |
4 |
Correct |
180 ms |
11924 KB |
Output is correct |