Submission #72711518


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vec vector
#define pll pair<ll,ll>
const ll N = 2e5 + 50;
ll bit[N];

void add(int x,int v){
	for(int i = x ; i < N ; i += (i & (-i))){
		bit[i] += v;
	}
}

ll get(ll x){
	ll sum = 0;
	for(int i = x ; i > 0 ; i -= (i & (-i))){
		sum += bit[i];
	}
	return sum;
}

ll sum(ll l,ll r){
	return get(r) - get(l - 1);
}

void solve(){
	ll n,q;cin >> n >> q;
	vec<ll> a(n + 1);
	for(int i = 1 ; i <= n ; i++){
		cin >> a[i];
		add(i,a[i]);
	}
	while(q--){
		ll op;cin >> op;
		if(op == 1){
			ll x;cin >> x;
			add(x,a[x + 1] - a[x]);
			add(x + 1,a[x] - a[x + 1]);
			swap(a[x],a[x + 1]);
		}else if(op == 2){
			ll l,r;cin >> l >> r;
			cout << sum(l,r) << "\n";
		}
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);cout.tie(nullptr);
	int T = 1;
//	cin >> T;
	while(T--){
		solve();
	}
    return 0;
}

Submission Info

Submission Time
Task D - Swap and Range Sum
User Xiao_Zhang
Language C++23 (Clang 21.1.0)
Score 400
Code Size 946 Byte
Status AC
Exec Time 442 ms
Memory 9196 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 2864 KiB
00_sample_01.txt AC 1 ms 3036 KiB
01_random_00.txt AC 439 ms 8116 KiB
01_random_01.txt AC 442 ms 7944 KiB
01_random_02.txt AC 417 ms 7296 KiB
01_random_03.txt AC 377 ms 6600 KiB
01_random_04.txt AC 375 ms 6280 KiB
01_random_05.txt AC 352 ms 5824 KiB
01_random_06.txt AC 333 ms 5896 KiB
01_random_07.txt AC 333 ms 5636 KiB
01_random_08.txt AC 310 ms 5668 KiB
01_random_09.txt AC 291 ms 5916 KiB
01_random_10.txt AC 253 ms 5580 KiB
01_random_11.txt AC 415 ms 8876 KiB
01_random_12.txt AC 407 ms 8404 KiB
01_random_13.txt AC 392 ms 7908 KiB
01_random_14.txt AC 379 ms 7144 KiB
01_random_15.txt AC 364 ms 6644 KiB
01_random_16.txt AC 350 ms 6136 KiB
01_random_17.txt AC 336 ms 6108 KiB
01_random_18.txt AC 323 ms 5884 KiB
01_random_19.txt AC 309 ms 5904 KiB
01_random_20.txt AC 292 ms 6024 KiB
01_random_21.txt AC 271 ms 5900 KiB
01_random_22.txt AC 350 ms 6228 KiB
01_random_23.txt AC 357 ms 6356 KiB
02_handmade_00.txt AC 1 ms 2824 KiB
02_handmade_01.txt AC 1 ms 2800 KiB
02_handmade_02.txt AC 400 ms 9196 KiB