답안 #774898

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
774898 2023-07-06T05:10:33 Z vjudge1 Addk (eJOI21_addk) C++17
100 / 100
202 ms 8804 KB
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/

#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define isz(x) (int)x.size()
using namespace std;

const int mxN = 1e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;

struct node {
    int val, lazy;
    node() {val = lazy = 0;}
    node operator + (const node &o) {
        node res;
        res.val = val + o.val;
        return res;
    }
} segtree[4 * mxN];

int n, k, q, a[mxN];

void down(int idx, int l, int r) {
    if(segtree[idx].lazy == 0)
        return;
    int val = segtree[idx].lazy, mid = (l + r) >> 1;

    segtree[idx << 1].val += val * (mid - l + 1);
    segtree[idx << 1].lazy += val;

    segtree[idx << 1 | 1].val += val * (r - mid);
    segtree[idx << 1 | 1].lazy += val;

    segtree[idx].lazy = 0;
}

void update(int l, int r, int idx, int u, int v, int val) {
    if(r < u || v < l) return;
    if(u <= l && r <= v) {
        segtree[idx].val += (r - l + 1) * val;
        segtree[idx].lazy += val;
        return;
    }

    down(idx, l, r);

    int mid = (l + r) >> 1;
    update(l, mid, idx << 1, u, v, val);
    update(mid + 1, r, idx << 1 | 1, u, v, val);

    segtree[idx] = segtree[idx << 1] + segtree[idx << 1 | 1];
}

int get(int l, int r, int idx, int u, int v) {
    if(r < u || v < l) return 0;
    if(u <= l && r <= v)
        return segtree[idx].val;

    down(idx, l, r);

    int mid = (l + r) >> 1;
    return get(l, mid, idx << 1, u, v) + get(mid + 1, r, idx << 1 | 1, u, v);
}

void solve() {
    cin >> n >> k;
    for(int i = 1; i <= n; ++i) {
        cin >> a[i];
        update(0, n, 1, i, n, a[i]);
    }

    cin >> q;
    while(q--) {
        int type; cin >> type;
        if(type == 1) {
            vi pos(k);
            for(int &val : pos) cin >> val;
            for(int i = 0; i < k; ++i) {
                int diff = a[pos[(i + 1) % k]] - a[pos[i]];
                update(0, n, 1, pos[i], n, diff);
            }
            for(int i = 0; i < k - 1; ++i)
                swap(a[pos[i]], a[pos[i + 1]]);
        }
        else {
            int l, r, m; cin >> l >> r >> m;
            cout << get(0, n, 1, l + m - 1, r) - get(0, n, 1, l - 1, r - m) << "\n";
        }
    }
}

signed main() {

#ifndef CDuongg
    if(fopen(taskname".inp", "r"))
        assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
    freopen("bai3.inp", "r", stdin);
    freopen("bai3.out", "w", stdout);
    auto start = chrono::high_resolution_clock::now();
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1; //cin >> t;
    while(t--) solve();

#ifdef CDuongg
    auto end = chrono::high_resolution_clock::now();
    cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
    cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif

}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6484 KB Output is correct
2 Correct 3 ms 6612 KB Output is correct
3 Correct 4 ms 6612 KB Output is correct
4 Correct 5 ms 6612 KB Output is correct
5 Correct 6 ms 6612 KB Output is correct
6 Correct 7 ms 6688 KB Output is correct
7 Correct 8 ms 6648 KB Output is correct
8 Correct 9 ms 6740 KB Output is correct
9 Correct 12 ms 6740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 7008 KB Output is correct
2 Correct 32 ms 7208 KB Output is correct
3 Correct 44 ms 7416 KB Output is correct
4 Correct 78 ms 8144 KB Output is correct
5 Correct 118 ms 8804 KB Output is correct
6 Correct 106 ms 8652 KB Output is correct
7 Correct 107 ms 8632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 7424 KB Output is correct
2 Correct 119 ms 8192 KB Output is correct
3 Correct 202 ms 8060 KB Output is correct
4 Correct 131 ms 8768 KB Output is correct