제출 #805594

#제출 시각아이디문제언어결과실행 시간메모리
805594aykhnAddk (eJOI21_addk)C++14
100 / 100
286 ms11260 KiB
#include <bits/stdc++.h> // author: aykhn using namespace std; typedef long long ll; #define TC int t; cin >> t; while (t--) _(); #define OPT ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define all(v) v.begin(), v.end() #define pii pair<int, int> #define mpr make_pair #define eb emplace_back #define pdd pair<double, double> #define pb push_back #define ts to_string #define fi first #define se second #define int ll #define ins insert #define inf 0x3F3F3F3F #define infll 0x3F3F3F3F3F3F3F3FLL #define bpc __builtin_popcount struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; int n; int a[2][100001]; struct SegTree { int sz; vector<int> st; void build(int l, int r, int x, int id) { if (l + 1 == r) { if (l < n) st[x] = a[id][l]; return; } int mid = (l + r) >> 1; build(l, mid, 2*x + 1, id); build(mid, r, 2*x + 2, id); st[x] = st[2*x + 1] + st[2*x + 2]; } void build(int id) { sz = 1; while (sz < n) sz <<= 1; st.assign(sz << 1, 0); build(0, sz, 0, id); } void make(int ind, int val, int l, int r, int x) { if (l + 1 == r) { st[x] = val; return; } int mid = (l + r) >> 1; if (ind < mid) make(ind, val, l, mid, 2*x + 1); else make(ind, val, mid, r, 2*x + 2); st[x] = st[2*x + 1] + st[2*x + 2]; } void make(int ind, int val) { make(ind - 1, val, 0, sz, 0); } int get(int lx, int rx, int l, int r, int x) { if (l >= rx || r <= lx) return 0; if (l >= lx && r <= rx) return st[x]; int mid = (l + r) >> 1; return get(lx, rx, l, mid, 2*x + 1) + get(lx, rx, mid, r, 2*x + 2); } int get(int l, int r) { if (l > r) return 0; return get(l - 1, r, 0, sz, 0); } }; SegTree st[2]; int solve1(int l, int r, int t) { if (l > r) return 0; int r1 = l + t; int len = min(r + 1, r1) - l; int s1 = st[1].get(l, l + len - 1) - st[0].get(l, l + len - 1) * (l - 1); int s2 = st[0].get(r1, r) * t; return s1 + s2; } int solve2(int l, int r, int t) { if (l > r) return 0; int l1 = r - t; int len = r - max(l - 1, l1); int s1 = st[0].get(r - len + 1, r) * (r + 1) - st[1].get(r - len + 1, r); int s2 = st[0].get(l, l1) * t; return s1 + s2; } signed main() { OPT; int k; cin >> n >> k; int b[k + 1]; for (int i = 0; i < n; i++) { cin >> a[0][i]; a[1][i] = a[0][i] * (i + 1); } st[0].build(0); st[1].build(1); int q; cin >> q; while (q--) { int type; cin >> type; if (type == 1) { for (int j = 1; j <= k; j++) { cin >> b[j]; b[j]--; if (j > 1) { swap(a[0][b[j - 1]], a[0][b[j]]); } } for (int j = 1; j <= k; j++) { a[1][b[j]] = a[0][b[j]] * (b[j] + 1); st[0].make(b[j] + 1, a[0][b[j]]); st[1].make(b[j] + 1, a[1][b[j]]); } } else { int l, r, m; cin >> l >> r >> m; int sz = r - l + 1; int t = sz - m + 1; int res = solve1(l, l + (sz + 1)/2 - 1, min(t, m)) + solve2(l + (sz + 1)/2, r, min(t, m)); cout << res << endl; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...