답안 #771008

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
771008 2023-07-02T10:33:23 Z GordonRemzi007 Addk (eJOI21_addk) C++17
92 / 100
250 ms 8108 KB
#include <iostream>
#include <vector>
#define ll long long
using namespace std;

class SegmentTree {
    ll n, i = 0;
    vector<ll> seggy;
public:
    SegmentTree(ll size):n(size) {
        seggy = vector<ll>(2*n);
    }
    void build() {
        for(ll i = n-1; i > 0; i--) seggy[i] = seggy[2*i]+seggy[2*i+1];
    }
    void push(ll x) {
        seggy[n+i] = x;
        i++;
    }
    void update(ll x, ll y) {
        x+=n;
        seggy[x] = y;
        while(x > 1) {
            x/=2;
            seggy[x] = seggy[2*x]+seggy[2*x+1];
        }
    }
    ll query(ll x, ll y) {
        ll res = 0;
        for(x+=n, y+=n+1; x<y; x/=2, y/=2) {
            if(x%2) res+=seggy[x++];
            if(y%2) res+=seggy[--y];
        }
        return res;
    }
};

int main() {
    ll n, k, q, l, r, m;
    cin >> n >> k;
    vector<ll> a(n), u(k);
    SegmentTree b(n), c(n);
    for(ll i = 0; i < n; i++) {
        cin >> a[i];
        b.push(a[i]);
        c.push((i+1)*a[i]);
    }
    b.build();
    c.build();
    cin >> q;
    while(q--) {
        cin >> m;
        if(m == 1) {
            for(ll i = 0; i < k; i++) {
                cin >> u[i];
                u[i]--;
            }
            l = a[u[k-1]];
            for(ll i = k-1; i > 0; i--) {
                b.update(u[i-1], l);
                c.update(u[i-1], (u[i-1]+1)*l);
                r = a[u[i-1]];
                a[u[i-1]] = l;
                l = r;
            }
            b.update(u[k-1], l);
            c.update(u[k-1], (u[k-1]+1)*l);
        } else {
            cin >> l >> r >> m;
            l--, r--;
            if(2*m <= r-l+1) cout << c.query(l, l+m-1)-l*b.query(l, l+m-1) + b.query(l+m, r-m)*m + (r+2)*b.query(r-m+1, r)-c.query(r-m+1, r) << "\n";
            else cout << c.query(l, r-m)-l*b.query(l, r-m) + b.query(r-m+1, l+m-1)*(r-l+2-m) + (r+2)*b.query(l+m, r)-c.query(l+m, r) << "\n";
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 5 ms 340 KB Output is correct
4 Correct 7 ms 340 KB Output is correct
5 Correct 11 ms 588 KB Output is correct
6 Correct 12 ms 644 KB Output is correct
7 Correct 14 ms 704 KB Output is correct
8 Correct 17 ms 812 KB Output is correct
9 Correct 23 ms 964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 1356 KB Output is correct
2 Correct 74 ms 2548 KB Output is correct
3 Correct 99 ms 3404 KB Output is correct
4 Correct 204 ms 5852 KB Output is correct
5 Correct 250 ms 8108 KB Output is correct
6 Correct 248 ms 7920 KB Output is correct
7 Correct 238 ms 7900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 132 ms 2740 KB Output isn't correct
2 Halted 0 ms 0 KB -