Submission #72694520


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#define int long long
template <typename Info>
struct Segment_Tree {
    Info *node;

    Segment_Tree(int _n = 0) {
        node = new Info[4 << std::__lg(_n + 1)]();
    }

    void push_up(int p) {
        node[p] = node[p << 1] + node[p << 1 | 1];
    }

    void build(int p, int l, int r, const std::vector<Info> &info) {
        if (l == r)
            return (void)(node[p] = info[l]);
        const int m = (l + r) >> 1;
        build(p << 1, l, m, info);
        build(p << 1 | 1, m + 1, r, info);
        push_up(p);
    }

    void build(int p, int l, int r) {
        if (l == r)
            return (void)(node[p] = Info());
        const int m = (l + r) >> 1;
        build(p << 1, l, m);
        build(p << 1 | 1, m + 1, r);
        push_up(p);
    }

    void update(int p, int l, int r, int u, Info x) {
        if (u < l || r < u)
            return;
        if (l == r)
            return (void)(node[p].update(x));
        const int m = (l + r) >> 1;
        update(p << 1, l, m, u, x);
        update(p << 1 | 1, m + 1, r, u, x);
        push_up(p);
    }

    Info query(int p, int l, int r, int L, int R) {
        if (R < l || r < L)
            return Info();
        if (L <= l && r <= R)
            return node[p];
        const int m = (l + r) >> 1;
        return query(p << 1, l, m, L, R) + query(p << 1 | 1, m + 1, r, L, R);
    }

    template <typename Check>
    std::pair<int, Info> search(int p, int l, int r, int L, int R, Info pre, Check check) {
        if (r < L || R < l) return std::make_pair(-1, Info());
        if (!check(pre + node[p])) return std::make_pair(-1, node[p]);
        if (l == r) return std::make_pair(l, node[p]);
        int mid = (l + r) >> 1;
        auto left_res = search(p << 1, l, mid, L, R, pre, check);
        if (left_res.first == -1) {
            return search(p << 1 | 1, mid + 1, r, L, R, pre + left_res.second, check);
        }
        return left_res;
    }

};
struct info {
    int sum;
    info(int _sum = 0) {
        sum = _sum;

    }
    friend info operator + (info l,info r) {
        info res;
        res.sum = l.sum + r.sum;
        return res;
    }
    void update(info x) {
        *this = x;
    }
};
signed main() {
    int n,q;
    cin >> n >> q;
    vector <info> a(n + 1);
    Segment_Tree<info> seg(n + 1);
    for(int i=1;i<=n;i++) cin >> a[i].sum;
    seg.build(1,1,n,a);
    for(int i=1;i<=q;i++) {
        int op;
        cin >> op;
        if(op == 1) {
            int x;
            cin >> x;
            int t1 = seg.query(1,1,n,x,x).sum;
            int t2 = seg.query(1,1,n,x + 1,x + 1).sum;
            swap(t1,t2);
            seg.update(1,1,n,x,info(t1));
            seg.update(1,1,n,x + 1,info(t2));
        }
        else {
            int l,r;
            cin >> l >> r;
            cout << seg.query(1,1,n,l,r).sum << "\n";
        }
    }
}

Submission Info

Submission Time
Task D - Swap and Range Sum
User zhishengie
Language C++23 (GCC 15.2.0)
Score 400
Code Size 3014 Byte
Status AC
Exec Time 604 ms
Memory 11656 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 2
AC × 29
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 02_handmade_00.txt, 02_handmade_01.txt, 02_handmade_02.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3608 KiB
00_sample_01.txt AC 1 ms 3492 KiB
01_random_00.txt AC 597 ms 10796 KiB
01_random_01.txt AC 604 ms 10632 KiB
01_random_02.txt AC 574 ms 9768 KiB
01_random_03.txt AC 524 ms 9292 KiB
01_random_04.txt AC 540 ms 9052 KiB
01_random_05.txt AC 500 ms 9016 KiB
01_random_06.txt AC 472 ms 8764 KiB
01_random_07.txt AC 476 ms 8904 KiB
01_random_08.txt AC 442 ms 8960 KiB
01_random_09.txt AC 417 ms 8796 KiB
01_random_10.txt AC 367 ms 8856 KiB
01_random_11.txt AC 529 ms 11400 KiB
01_random_12.txt AC 525 ms 10880 KiB
01_random_13.txt AC 517 ms 10380 KiB
01_random_14.txt AC 499 ms 9592 KiB
01_random_15.txt AC 492 ms 9264 KiB
01_random_16.txt AC 476 ms 8956 KiB
01_random_17.txt AC 462 ms 9144 KiB
01_random_18.txt AC 451 ms 8892 KiB
01_random_19.txt AC 435 ms 8984 KiB
01_random_20.txt AC 415 ms 9084 KiB
01_random_21.txt AC 395 ms 9020 KiB
01_random_22.txt AC 481 ms 9048 KiB
01_random_23.txt AC 488 ms 9148 KiB
02_handmade_00.txt AC 2 ms 3556 KiB
02_handmade_01.txt AC 1 ms 3580 KiB
02_handmade_02.txt AC 447 ms 11656 KiB