Submission #72693275


Source Code Expand

#include <iostream>
#include <vector>
using namespace std;
struct FenwickTree {
    int n;
    vector<long long> tree;
    FenwickTree(int size) : n(size), tree(size + 1, 0) {}
    void update(int idx, long long delta) {
        while (idx <= n) {
            tree[idx] += delta;
            idx += idx & -idx;
        }
    }
    long long query(int idx) {
        long long res = 0;
        while (idx > 0) {
            res += tree[idx];
            idx -= idx & -idx;
        }
        return res;
    }
    long long get_single(int idx) {
        return query(idx) - query(idx - 1);
    }
};
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, Q;
    cin >> N >> Q;
    vector<long long> A(N + 1);
    FenwickTree ft(N);
    for (int i = 1; i <= N; ++i) {
        cin >> A[i];
        ft.update(i, A[i]);
    }
    while (Q--) {
        int op;
        cin >> op;
        if (op == 1) {
            int x;
            cin >> x;
            long long val_x = ft.get_single(x);
            long long val_x1 = ft.get_single(x + 1);
            ft.update(x, -val_x + val_x1);
            ft.update(x + 1, -val_x1 + val_x);
        } else if (op == 2) {
            int l, r;
            cin >> l >> r;
            long long sum = ft.query(r) - ft.query(l - 1);
            cout << sum << '\n';
        }
    }
    return 0;
}

Submission Info

Submission Time
Task D - Swap and Range Sum
User xuhanjin
Language C++23 (GCC 15.2.0)
Score 400
Code Size 1407 Byte
Status AC
Exec Time 76 ms
Memory 9136 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 3428 KiB
00_sample_01.txt AC 1 ms 3472 KiB
01_random_00.txt AC 74 ms 8208 KiB
01_random_01.txt AC 76 ms 8068 KiB
01_random_02.txt AC 72 ms 7172 KiB
01_random_03.txt AC 67 ms 6720 KiB
01_random_04.txt AC 68 ms 6328 KiB
01_random_05.txt AC 64 ms 6328 KiB
01_random_06.txt AC 62 ms 6328 KiB
01_random_07.txt AC 62 ms 6276 KiB
01_random_08.txt AC 59 ms 6276 KiB
01_random_09.txt AC 56 ms 6352 KiB
01_random_10.txt AC 50 ms 6092 KiB
01_random_11.txt AC 73 ms 8884 KiB
01_random_12.txt AC 71 ms 8368 KiB
01_random_13.txt AC 70 ms 7864 KiB
01_random_14.txt AC 68 ms 7092 KiB
01_random_15.txt AC 67 ms 6824 KiB
01_random_16.txt AC 65 ms 6388 KiB
01_random_17.txt AC 64 ms 6388 KiB
01_random_18.txt AC 62 ms 6428 KiB
01_random_19.txt AC 59 ms 6532 KiB
01_random_20.txt AC 56 ms 6464 KiB
01_random_21.txt AC 53 ms 6428 KiB
01_random_22.txt AC 63 ms 6600 KiB
01_random_23.txt AC 64 ms 6352 KiB
02_handmade_00.txt AC 2 ms 3476 KiB
02_handmade_01.txt AC 1 ms 3584 KiB
02_handmade_02.txt AC 58 ms 9136 KiB