Submission #72696285


Source Code Expand

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Fenwick {
    int n;
    vector<long long> bit;
    Fenwick(int n) : n(n), bit(n + 1, 0) {}
    void add(int idx, long long val) {
        for (; idx <= n; idx += idx & -idx)
            bit[idx] += val;
    }
    long long sum(int idx) {
        long long s = 0;
        for (; idx > 0; idx -= idx & -idx)
            s += bit[idx];
        return s;
    }
    long long rangeSum(int l, int r) {
        return sum(r) - sum(l - 1);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, Q;
    cin >> N >> Q;
    vector<long long> A(N + 1);
    Fenwick fw(N);
    for (int i = 1; i <= N; i++) {
        cin >> A[i];
        fw.add(i, A[i]);
    }
    while (Q--) {
        int type;
        cin >> type;
        if (type == 1) {
            int x;
            cin >> x;
            long long a = A[x];
            long long b = A[x + 1];
            swap(A[x], A[x + 1]);
            fw.add(x, b - a);
            fw.add(x + 1, a - b);
        }
        else {
            int l, r;
            cin >> l >> r;
            cout << fw.rangeSum(l, r) << "\n";
        }
    }

    return 0;
}

Submission Info

Submission Time
Task D - Swap and Range Sum
User adityagrg023
Language C++23 (GCC 15.2.0)
Score 400
Code Size 1267 Byte
Status AC
Exec Time 87 ms
Memory 9140 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 2 ms 3476 KiB
00_sample_01.txt AC 1 ms 3532 KiB
01_random_00.txt AC 87 ms 8212 KiB
01_random_01.txt AC 87 ms 7844 KiB
01_random_02.txt AC 84 ms 7176 KiB
01_random_03.txt AC 77 ms 6724 KiB
01_random_04.txt AC 77 ms 6496 KiB
01_random_05.txt AC 73 ms 6188 KiB
01_random_06.txt AC 68 ms 6352 KiB
01_random_07.txt AC 68 ms 6268 KiB
01_random_08.txt AC 63 ms 6332 KiB
01_random_09.txt AC 59 ms 6460 KiB
01_random_10.txt AC 52 ms 6060 KiB
01_random_11.txt AC 84 ms 8884 KiB
01_random_12.txt AC 84 ms 8364 KiB
01_random_13.txt AC 82 ms 7844 KiB
01_random_14.txt AC 78 ms 7076 KiB
01_random_15.txt AC 77 ms 6820 KiB
01_random_16.txt AC 74 ms 6524 KiB
01_random_17.txt AC 70 ms 6412 KiB
01_random_18.txt AC 67 ms 6480 KiB
01_random_19.txt AC 64 ms 6440 KiB
01_random_20.txt AC 60 ms 6460 KiB
01_random_21.txt AC 56 ms 6300 KiB
01_random_22.txt AC 71 ms 6480 KiB
01_random_23.txt AC 72 ms 6588 KiB
02_handmade_00.txt AC 1 ms 3396 KiB
02_handmade_01.txt AC 1 ms 3476 KiB
02_handmade_02.txt AC 74 ms 9140 KiB