Submission #72699023


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define endl '\n'
const int N=500100,INF=2e18,MOD=998244353;
int t,n,k,x,y,z,m,h,j,u,cnt,mi,tt,rr,mx,res,ans,ll,q,kk;
int a[N],b[N],c[N];
template <class T>
struct BIT {
    int n;
    vector<T> w;

    BIT(int n = 0) { init(n); }

    // 用原数组 in[1..n] 建树(O(n log n))
    BIT(int n, const vector<T> &in) {
        init(n);
        for (int i = 1; i <= n; i++) {
            add(i, in[i]);
        }
    }

    void init(int n_) {
        n = n_;
        w.assign(n + 1, 0);
    }

    // 单点加
    void add(int x, T v) {
        for (; x <= n; x += x & -x) {
            w[x] += v;
        }
    }

    // 前缀和 [1..x]
    T ask(int x) const {
        T ans = 0;
        for (; x > 0; x -= x & -x) {
            ans += w[x];
        }
        return ans;
    }

    // 区间和 [l..r]
    T ask(int l, int r) const {
        return ask(r) - ask(l - 1);
    }
};
signed main(){
    cin>>n>>q;
    vector<int>f(n+5);
    for(int i=1;i<=n;i++){
        cin>>f[i];
    }
    BIT<long long> bit(n,f);
    while(q--){
        cin>>x;
        if(x==1){
            cin>>y;
            z=y+1;
            tt=bit.ask(y,y);
            rr=bit.ask(z,z);
            bit.add(y,-tt);
            bit.add(y,rr);
            bit.add(z,-rr);
            bit.add(z,tt);
        }else{
            int l,r;
            cin>>l>>r;
            cout<<bit.ask(l,r)<<endl;


        }

    }

}

Submission Info

Submission Time
Task D - Swap and Range Sum
User mengqing
Language C++23 (GCC 15.2.0)
Score 400
Code Size 1568 Byte
Status AC
Exec Time 437 ms
Memory 9084 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 3588 KiB
00_sample_01.txt AC 1 ms 3540 KiB
01_random_00.txt AC 437 ms 8236 KiB
01_random_01.txt AC 425 ms 8044 KiB
01_random_02.txt AC 390 ms 7208 KiB
01_random_03.txt AC 345 ms 6728 KiB
01_random_04.txt AC 334 ms 6476 KiB
01_random_05.txt AC 304 ms 6288 KiB
01_random_06.txt AC 275 ms 6384 KiB
01_random_07.txt AC 262 ms 6204 KiB
01_random_08.txt AC 232 ms 6080 KiB
01_random_09.txt AC 204 ms 6320 KiB
01_random_10.txt AC 167 ms 6160 KiB
01_random_11.txt AC 421 ms 8824 KiB
01_random_12.txt AC 401 ms 8556 KiB
01_random_13.txt AC 377 ms 7796 KiB
01_random_14.txt AC 352 ms 7024 KiB
01_random_15.txt AC 330 ms 6764 KiB
01_random_16.txt AC 305 ms 6452 KiB
01_random_17.txt AC 280 ms 6460 KiB
01_random_18.txt AC 256 ms 6476 KiB
01_random_19.txt AC 232 ms 6516 KiB
01_random_20.txt AC 206 ms 6528 KiB
01_random_21.txt AC 180 ms 6416 KiB
01_random_22.txt AC 300 ms 6592 KiB
01_random_23.txt AC 305 ms 6308 KiB
02_handmade_00.txt AC 1 ms 3404 KiB
02_handmade_01.txt AC 1 ms 3616 KiB
02_handmade_02.txt AC 406 ms 9084 KiB