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 <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std;
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair <int, int>
#define ull unsigned long long
// #define int long long
// #define int unsigned long long
const ll inf = 1e9 + 7;
const ll weirdMod = 998244353;
ll cur;
int pos[15];
struct node {
ll lwgt = 0, rwgt = 0, sum = 0;
} t[400040];
void update(int v, int tl, int tr, int pos, int val) {
if (tl == tr) {
t[v].lwgt = val;
t[v].rwgt = val;
t[v].sum = val;
return;
} int tm = (tl + tr) / 2;
if (pos <= tm)
update(2 * v, tl, tm, pos, val);
else update(2 * v + 1, tm + 1, tr, pos, val);
t[v].sum = t[2 * v].sum + t[2 * v + 1].sum;
t[v].lwgt = t[2 * v].lwgt + t[2 * v + 1].lwgt + (tm - tl + 1) * t[2 * v + 1].sum;
t[v].rwgt = t[2 * v].rwgt + t[2 * v + 1].rwgt + (tr - tm) * t[2 * v].sum; // ???
}
ll getsum(int v, int tl, int tr, int l, int r) {
if (l > r)
return 0;
if (tl == l && tr == r)
return t[v].sum;
int tm = (tl + tr) / 2;
return getsum(2 * v, tl, tm, l, min(r, tm)) +
getsum(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
}
ll getlwgt(int v, int tl, int tr, int l, int r) {
if (l > r)
return 0;
if (tl == l && tr == r) {
ll ans = cur * t[v].sum + t[v].lwgt;
cur += (tr - tl + 1); return ans;
} int tm = (tl + tr) / 2;
return getlwgt(2 * v, tl, tm, l, min(r, tm)) +
getlwgt(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
}
ll getrwgt(int v, int tl, int tr, int l, int r) {
if (l > r)
return 0;
if (tl == l && tr == r) {
ll ans = cur * t[v].sum + t[v].rwgt;
cur += (tr - tl + 1); return ans;
} int tm = (tl + tr) / 2;
return getrwgt(2 * v + 1, tm + 1, tr, max(l, tm + 1), r) +
getrwgt(2 * v, tl, tm, l, min(r, tm));
}
void solve() {
int n, k;
cin >> n >> k;
int arr[n + 1];
for (int i = 1; i <= n; i++) {
cin >> arr[i];
update(1, 1, n, i, arr[i]);
} int q;
cin >> q;
int tc, l, r, m;
while (q--) {
cin >> tc;
if (tc == 1) {
for (int i = 1; i <= k; i++)
cin >> pos[i];
int tmp = arr[pos[1]];
for (int i = 1; i < k; i++)
arr[pos[i]] = arr[pos[i + 1]];
arr[pos[k]] = tmp;
for (int i = 1; i <= k; i++)
update(1, 1, n, i, arr[pos[i]]);
} else {
cin >> l >> r >> m;
if (r - l + 1 >= 2 * (m - 1)) {
ll lsum = getlwgt(1, 1, n, l, l + m - 2); cur = 0;
ll rsum = getrwgt(1, 1, n, r - m + 2, r); cur = 0;
ll allsum = getsum(1, 1, n, l + m - 1, r - m + 1) * m;
cout << lsum + rsum + allsum << '\n';
} else {
ll val = (r - l + 1) - m + 1;
ll lsum = getlwgt(1, 1, n, l, l + val - 2); cur = 0;
ll rsum = getrwgt(1, 1, n, r - val + 1, r); cur = 0;
ll allsum = getsum(1, 1, n, l + val - 1, r - val) * val;
cout << lsum + rsum + allsum << '\n';
}
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << '\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... |