Submission #786800

#TimeUsernameProblemLanguageResultExecution timeMemory
786800SalihSahinAddk (eJOI21_addk)C++14
0 / 100
141 ms6672 KiB
#include <bits/stdc++.h> #define pb push_back #define fastio cin.tie(0); ios_base::sync_with_stdio(false); #define int long long using namespace std; const int N = 1e5 + 5; const int mod = 1e9+7; const int inf = 3e18 + 5; struct SegmentTree{ vector<int> tree; void init(int n){ tree.resize(n*4); } void build(vector<int> &a, int v, int l, int r){ if(l == r) tree[v] = a[l]; else{ int m = (l + r)/2; build(a, v*2, l, m); build(a, v*2+1, m+1, r); tree[v] = tree[v*2] + tree[v*2+1]; } } int query(int v, int l, int r, int tl, int tr){ if(l > r || r < tl || l > tr) return 0; if(tl <= l && tr >= r) return tree[v]; int m = (l + r)/2; return query(v*2, l, m, tl, tr) + query(v*2+1, m+1, r, tl, tr); } void update(int v, int pos, int value, int l, int r){ if(l == r) tree[v] = value; else{ int m = (l+r)/2; if(pos <= m) update(v*2, pos, value, l, m); else update(v*2+1, pos, value, m+1, r); tree[v] = tree[v*2] + tree[v*2+1]; } } }; int32_t main(){ fastio; int n, k; cin>>n>>k; vector<int> a(n), la(n), ra(n); for(int i = 0; i < n; i++){ cin>>a[i]; la[i] = a[i] * (i + 1); ra[i] = a[i] * (n - i); } SegmentTree sum, pre, suf; sum.init(n); sum.build(a, 1, 0, n-1); pre.init(n); pre.build(la, 1, 0, n-1); suf.init(n); suf.build(ra, 1, 0, n-1); int q; cin>>q; while(q--){ int type; cin>>type; if(type == 1){ // left shift vector<int> ind(k); for(int i = 0; i < k; i++){ cin>>ind[i]; ind[i]--; } int temp = a[ind[0]]; for(int i = 0; i < k-1; i++){ a[ind[i]] = a[ind[i+1]]; } a[ind[k-1]] = temp; for(int i = 0; i < k; i++){ la[ind[i]] = a[ind[i]] * (ind[i] + 1); ra[ind[i]] = a[ind[i]] * (n - ind[i]); } for(int i = 0; i < k; i++){ sum.update(1, ind[i], a[ind[i]], 0, n-1); pre.update(1, ind[i], la[ind[i]], 0, n-1); suf.update(1, ind[i], ra[ind[i]], 0, n-1); } } else{ // sum query int l, r, m; cin>>l>>r>>m; l--, r--; if(r - l + 1 < m){ cout<<0<<endl; } else{ int mid_len = (r - l + 1) - 2*(m-1); int left_len = ((r - l + 1) - mid_len)/2; int right_len = ((r - l + 1) - mid_len)/2; int mid_sum = sum.query(1, 0, n-1, l + left_len, r - right_len) * m; int left_sum = pre.query(1, 0, n-1, l, l + left_len - 1) - sum.query(1, 0, n-1, l, l + left_len - 1) * (l + 1 - 1); int right_sum = suf.query(1, 0, n-1, r - right_len + 1, r) - sum.query(1, 0, n-1, r - right_len + 1, r) * (n - r - 1); cout<<left_sum + mid_sum + right_sum<<endl; } } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...