이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
// #pragma GCC target("avx2")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file(s) freopen(s".in", "r", stdin);freopen(s".out", "w", stdout);
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ft first
#define sd second
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
const int N = 1e5 + 10;
const int M = 1e6 + 5;
const int B = 316;
const ll msize = 2;
const ll mod1 = 1e9 + 7;
const ll mod2 = 998244353;
const long double Phi = acos(-1);
const long long inf = 2e18;
const vector <pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
ll binmul(ll x, ll ti, ll m);
ll binpow(ll x, ll ti, ll m);
struct node {
ll sum, pref, suf;
} t[N * 4];
ll n, k, a[N];
node merge(node a, node b) {
return {
a.sum + b.sum,
a.pref + b.pref,
a.suf + b.suf
};
}
void upd(int pos, ll val, int v = 1, int tl = 1, int tr = n) {
if (tl == tr) {
t[v] = {val, val * tl, val * (n - tl + 1)};
return;
}
int tm = (tl + tr) / 2;
if (pos <= tm) upd(pos, val, v + v, tl, tm);
else upd(pos, val, v + v + 1, tm + 1, tr);
t[v] = merge(t[v + v], t[v + v + 1]);
}
node get(int l, int r, int v = 1, int tl = 1, int tr = n) {
if (r < tl || tr < l) return {0, 0, 0};
if (l <= tl && tr <= r) return t[v];
int tm = (tl + tr) / 2;
return merge(
get(l, r, v + v, tl, tm),
get(l, r, v + v + 1, tm + 1, tr)
);
}
const void solve() {
cin >> n >> k;
for (int i = 1; i <= n; ++ i) {
cin >> a[i];
upd(i, a[i]);
}
int q; cin >> q;
vector <int> v(k);
for (int i = 1; i <= q; ++ i) {
int tp;
cin >> tp;
if (tp == 1) {
for (int i = 0; i < k; ++ i) {
cin >> v[i];
}
int tmp = v[0], tmpVal = a[v[0]];
for (int i = 0; i < k; ++ i) {
if (i < k - 1) {
upd(v[i], a[v[i + 1]]);
a[v[i]] = a[v[i + 1]];
v[i] = v[i + 1];
} else {
upd(v[i], tmpVal);
a[v[i]] = tmpVal;
v[i] = tmp;
}
}
} else {
int l, r, m;
cin >> l >> r >> m;
if (l == r) {
cout << a[l] << '\n';
return;
}
m = min(m, (r - l + 1) - m + 1);
int x = l + m - 1, y = r - m + 1;
if (x == y) ++ y;
node sum1 = get(l, x), sum2 = get(y, r), sum3 = get(x + 1, y - 1);
// cout << l << " - " << x << " - " << y << " - " << r << "\n";
// cout << sum1.pref << " " << sum1.sum << "\n";
// cout << sum2.suf << " " << sum2.sum << "\n";
// cout << sum3.sum << "\n";
cout << (sum1.pref - sum1.sum * (l - 1)) + (sum2.suf - sum2.sum * (n - r)) + (sum3.sum * m) << "\n";
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
srand(time(NULL));
// file("promote");
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tt = 1;
// cin >> tt;
for (int i = 1; i <= tt; ++ i) {
// cout << "Caso #" << i << "\n";
solve();
}
#ifndef ONLINE_JUDGE
cerr << "\n" << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
// Template functions
ll binmul(ll x, ll ti, ll m) { ll res = 0;while (ti){if(ti & 1)res += x;x += x;ti >>= 1; x %= m; res %= m;} return res;}
ll binpow(ll x, ll ti, ll m) { ll res = 1;while (ti){if(ti & 1)(res *= x)%=m;(x*=x)%=m;ti >>= 1; x %= m; res %= m;} return res;}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |