답안 #774953

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
774953 2023-07-06T06:15:27 Z huutuan Addk (eJOI21_addk) C++14
0 / 100
2000 ms 788424 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define sumof(x) accumulate(all(x), 0ll)

struct ST_node{
    int x;
    ST_node (int a=0): x(a){}
    friend ST_node sus(const ST_node& a, const ST_node& b){
        return ST_node(a.x+b.x);
    }
};

struct SegmentTree{
    int n; vector<ST_node> t;

    void init(int _n){
        n=_n;
        t.assign(4*n+1, ST_node());
    }

    void build(int k, int l, int r, int* a){
        if (l==r){
            t[k]=ST_node(a[l]);
            return;
        }
        int mid=(l+r)>>1;
        build(k<<1, l, mid, a);
        build(k<<1|1, mid+1, r, a);
        t[k]=sus(t[k<<1], t[k<<1|1]);
    }

    void init(int _n, int* a){
        init(_n);
        build(1, 1, n, a);
    }

    void update(int k, int l, int r, int pos, int val){
        if (l==r){
            t[k]=ST_node(val);
            return;
        }
        int mid=(l+r)>>1;
        if (pos<=mid) update(k<<1, l, mid, pos, val);
        else update(k<<1|1, mid+1, r, pos, val);
        t[k]=sus(t[k<<1], t[k<<1|1]);
    }

    ST_node get(int k, int l, int r, int L, int R){
        if (r<L || R<l) return t[0];
        if (L<=l && r<=R) return t[k];
        int mid=(l+r)>>1;
        return sus(get(k<<1, l, mid, L, R), get(k<<1|1, mid+1, r, L, R));
    }

    void update(int pos, int val){
        update(1, 1, n, pos, val);
    }

    int get(int l, int r){
        return get(1, 1, n, l, r).x;
    }
} st, stl, str;

const int N=1e5+1;
int n, k, q, a[N];

void solve(int tc){
    // cout << "Case #" << tc << ": ";
    cin >> n >> k;
    for (int i=1; i<=n; ++i) cin >> a[i];
    st.init(n);
    stl.init(n);
    str.init(n);
    cin >> q;
    function<int(int, int)> getl=[&](int l, int r) -> int {
        return stl.get(l, r)-st.get(l, r)*(l-1);
    };
    function<int(int, int)> getr=[&](int l, int r) -> int {
        return str.get(l, r)-st.get(l, r)*(n-r);
    };
    function<void(int, int)> update=[&](int i, int x) -> int {
        st.update(i, x);
        stl.update(i, x*i);
        str.update(i, x*(n-i+1));
    };
    for (int i=1; i<=n; ++i) update(i, a[i]);
    while (q--){
        int o; cin >> o;
        if (o==1){
            vector<int> v(k);
            for (int& i:v) cin >> i;
            for (int i=0; i<sz(v); ++i){
                int j=(i+1)%sz(v);
                update(v[i], a[v[j]]);
            }
            int tmp=a[v[0]];
            for (int i=0; i<sz(v)-1; ++i) a[v[i]]=a[v[i+1]];
            a[v.back()]=tmp;
        }else{
            int l, r, m;
            cin >> l >> r >> m;
            if (r-l+1<2*m-1){
                int i1=(l+r)>>1, i2=i1+1;
                cout << getl(l, i1)+getr(i2, r) << '\n';
            }else{
                int i1=l+m-2, i2=r-m+2;
                cout << getl(l, i1)+getr(i2, r)+st.get(i1+1, i2-1)*m << '\n';
            }
        }
    }
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int ntests=1;
    // cin >> ntests;
    for (int i=1; i<=ntests; ++i) solve(i);
    return 0;
}

Compilation message

Main.cpp: In lambda function:
Main.cpp:91:5: warning: no return statement in function returning non-void [-Wreturn-type]
   91 |     };
      |     ^
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2092 ms 788424 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2048 ms 7440 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2062 ms 18680 KB Time limit exceeded
2 Halted 0 ms 0 KB -