This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifndef DEBUG
#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#endif
#include <bits/stdc++.h>
#define int long long
#define d_struct tuple<int, int, int>
using namespace std;
struct Segtree {
int n;
vector<d_struct> data;
Segtree(const vector<int>& v) {
n = v.size();
data = vector<d_struct>(4*n);
build(v, 1, 0, n);
}
public:
void set(int i, int x) {
set(i, x, 1, 0, n);
}
int query(int beg, int ed) {
return get<0>(query(beg, ed, 1, 0, n));
}
private:
d_struct combine(d_struct a, d_struct b) {
auto [res1, flat1, h1] = a;
auto [res2, flat2, h2] = b;
return {res1 + (res2 + (flat2 * h1)), flat1 + flat2, h1 + h2};
}
void update(int pos) {
data[pos] = combine(data[pos*2], data[pos*2+1]);
}
void build(const vector<int>& v, int pos, int left, int right) {
if (left+1 == right) {
data[pos] = {v[left], v[left], 1};
} else {
int mid = (left+right) / 2;
build(v, pos*2, left, mid);
build(v, pos*2+1, mid, right);
update(pos);
}
}
void set(int i, int x, int pos, int left, int right) {
if (left+1 == right) {
data[pos] = {x, x, 1};
} else {
int mid = (left+right) / 2;
if (i < mid) {
set(i, x, pos*2, left, mid);
} else {
set(i, x, pos*2+1, mid, right);
}
update(pos);
}
}
d_struct query(int beg, int ed, int pos, int left, int right) {
if (beg <= left && ed >= right) {
return data[pos];
} else if (ed <= left || beg >= right) {
return {0, 0, 0};
} else {
int mid = (left+right) / 2;
return (combine(query(beg, ed, pos*2, left, mid),
query(beg, ed, pos*2+1, mid, right)));
}
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n);
vector<int> a_rev(n);
for (int i=0; i<n; i++) {
int ai;
cin >> ai;
a[i] = ai;
a_rev[i] = ai;
}
reverse(a_rev.begin(), a_rev.end());
Segtree s_inc(a);
Segtree s_dec(a_rev);
int q;
cin >> q;
for (int i=0; i<q; i++) {
int type;
cin >> type;
if (type == 1) {
vector<int> idx(k);
for (int j=0; j<k; j++) {
cin >> idx[j];
}
int tmp = a[idx[0]-1];
for (int j=0; j<(k-1); j++) {
s_inc.set(idx[j]-1, a[idx[j+1]-1]);
s_dec.set(n-idx[j], a[idx[j+1]-1]);
a[idx[j]-1] = a[idx[j+1]-1];
}
s_inc.set(idx[k-1]-1, tmp);
s_dec.set(n-idx[k-1], tmp);
a[idx[k-1]-1] = tmp;
} else {
int l, r, m;
cin >> l >> r >> m;
m = min(m, (r - l + 1) - m + 1);
int sum = 0;
if ((r - l + 1) % 2 == 0) {
sum += s_inc.query(l-1, (l - 1) + ((r - l + 1) / 2));
sum += s_dec.query(n-r, (n - r) + ((r - l + 1) / 2));
//cerr << "run1";
if (m < ((r - l + 1) / 2)) {
sum -= s_inc.query(l+m-1, (l - 1) + ((r - l + 1) / 2));
sum -= s_dec.query(n-(r-m), (n - r) + ((r - l + 1) / 2));
}
} else {
sum += s_inc.query(l-1, (l - 1) + ((r - l + 2) / 2));
sum += s_dec.query(n-r, (n - r) + ((r - l + 1) / 2));
//cerr << sum << "\n";
if (m == ((r - l + 1) / 2)) {
sum -= a[(l-1) + ((r - l + 1) / 2)];
} else if (m < ((r - l + 1) / 2)) {
sum -= s_inc.query(l+m-1, (l - 1) + ((r - l + 2) / 2));
sum -= s_dec.query(n-(r-m), (n - r) + ((r - l + 1) / 2));
}
}
cout << sum << "\n";
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |