Submission #1109453

# Submission time Handle Problem Language Result Execution time Memory
1109453 2024-11-06T18:08:17 Z Irate Addk (eJOI21_addk) C++17
100 / 100
151 ms 15432 KB
#include<bits/stdc++.h>
using namespace std;
int n, k;
struct Node{
    long long wpref1 = 0, wpref2 = 0, pref = 0;
};
struct SegmentTree{
    vector<Node>sTree;
    SegmentTree(int n){
        sTree.resize(4 * n);
    }
    Node Merge(Node &left, Node &right){
        Node res = {left.wpref1 + right.wpref1, left.wpref2 + right.wpref2, left.pref + right.pref};
        return res;
    }
    void Build(int node, int l, int r, vector<int>&v){
        if(l == r){
            sTree[node] = {1LL * l * v[l], (n - l + 1LL) * v[l], v[l]};
        }
        else{
            int mid = (l + r) >> 1;
            Build(node * 2, l, mid, v);
            Build(node * 2 + 1, mid + 1, r, v);
            sTree[node] = Merge(sTree[node * 2], sTree[node * 2 + 1]);
        }
    }
    void Update(int node, int l, int r, int pos, int val){
        if(l == r){
            sTree[node] = {1LL * l * val, (n - l + 1LL) * val, val};
        }
        else{
            int mid = (l + r) >> 1;
            if(pos <= mid){
                Update(node * 2, l, mid, pos, val);
            }
            else{
                Update(node * 2 + 1, mid + 1, r, pos, val);
            }
            sTree[node] = Merge(sTree[node * 2], sTree[node * 2 + 1]);
        }
    }
    Node Query(int node, int l, int r, int ql, int qr){
        if(ql <= l && r <= qr){
            return sTree[node];
        }
        if(ql > r || l > qr){
            Node ide;
            return ide;
        }
        int mid = (l + r) >> 1;
        Node lc = Query(node * 2, l, mid, ql, qr);
        Node rc = Query(node * 2 + 1, mid + 1, r, ql, qr);
        return Merge(lc, rc);
    }
};
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k;
    vector<int>v(n + 1);
    for(int i = 1;i <= n;++i){
        cin >> v[i];
    }
    SegmentTree tree(n);
    tree.Build(1, 1, n, v);
    int q;
    cin >> q;
    while(q--){
        int type;
        cin >> type;
        if(type == 1){
            vector<int>ind(k);
            for(int i = 0;i < k;++i){
                cin >> ind[i];
            }
            for(int i = 1;i < k;++i){
                tree.Update(1, 1, n, ind[i - 1], v[ind[i]]);
            }
            tree.Update(1, 1, n, ind[k - 1], v[ind[0]]);
            int num = v[ind[0]];
            for(int i = 1;i < k;++i){
                v[ind[i - 1]] = v[ind[i]];
            }
            v[ind[k - 1]] = num;
        }
        else{
            int l, r, m;
            cin >> l >> r >> m;
            int num = min(r - m + 1 - l, m - 1);
            Node res1 = tree.Query(1, 1, n, l, l + num - 1);
            Node res2 = tree.Query(1, 1, n, r - num + 1, r);
            Node res3 = tree.Query(1, 1, n, l + num, r - num);
            cout << 1LL * (num + 1) * res3.pref + res1.wpref1 - (l - 1LL) * res1.pref + res2.wpref2 - 1LL * (n - r) * res2.pref << '\n';
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 3 ms 592 KB Output is correct
4 Correct 5 ms 592 KB Output is correct
5 Correct 5 ms 848 KB Output is correct
6 Correct 5 ms 848 KB Output is correct
7 Correct 10 ms 1104 KB Output is correct
8 Correct 8 ms 1104 KB Output is correct
9 Correct 12 ms 1804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 2632 KB Output is correct
2 Correct 36 ms 4252 KB Output is correct
3 Correct 51 ms 4780 KB Output is correct
4 Correct 80 ms 8116 KB Output is correct
5 Correct 114 ms 11600 KB Output is correct
6 Correct 116 ms 11592 KB Output is correct
7 Correct 119 ms 13128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 6984 KB Output is correct
2 Correct 111 ms 11592 KB Output is correct
3 Correct 151 ms 15432 KB Output is correct
4 Correct 124 ms 14420 KB Output is correct