結果

問題 No.833 かっこいい電車
ユーザー furafura
提出日時 2020-07-31 09:44:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,581 bytes
コンパイル時間 2,357 ms
コンパイル使用メモリ 205,648 KB
実行使用メモリ 5,484 KB
最終ジャッジ日時 2023-09-19 09:20:03
合計ジャッジ時間 6,560 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 33 ms
4,380 KB
testcase_11 AC 42 ms
4,992 KB
testcase_12 AC 15 ms
4,380 KB
testcase_13 AC 11 ms
4,376 KB
testcase_14 AC 33 ms
4,884 KB
testcase_15 AC 18 ms
4,376 KB
testcase_16 AC 14 ms
4,616 KB
testcase_17 AC 14 ms
4,380 KB
testcase_18 AC 39 ms
4,400 KB
testcase_19 AC 14 ms
4,380 KB
testcase_20 AC 5 ms
4,376 KB
testcase_21 AC 34 ms
4,376 KB
testcase_22 AC 23 ms
5,204 KB
testcase_23 AC 16 ms
4,380 KB
testcase_24 AC 22 ms
4,812 KB
testcase_25 AC 38 ms
4,380 KB
testcase_26 AC 16 ms
4,692 KB
testcase_27 AC 27 ms
4,396 KB
testcase_28 AC 21 ms
4,376 KB
testcase_29 AC 24 ms
4,376 KB
testcase_30 AC 30 ms
5,484 KB
testcase_31 AC 31 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

template<class G>
class Fenwick_tree{
	vector<G> a;
public:
	Fenwick_tree(){}
	Fenwick_tree(int n){ build(n); }
	Fenwick_tree(const vector<G>& a){ build(a); }
	void build(int n){
		a.assign(n,G{});
	}
	void build(const vector<G>& a){
		this->a=a;
		for(int i=1;i<a.size();i++) if(i+(i&-i)-1<a.size()) (this->a)[i+(i&-i)-1]+=(this->a)[i-1];
	}
	void add(int i,const G& val){
		for(i++;i<=a.size();i+=i&-i) a[i-1]+=val;
	}
	G sum(int l,int r)const{
		if(l==0){
			G res{};
			for(;r>0;r-=r&-r) res+=a[r-1];
			return res;
		}
		return sum(0,r)-sum(0,l);
	}
	int lower_bound(G val)const{
		if(val<=G{}) return 0;
		int x=0,k;
		for(k=1;k<=a.size();k<<=1);
		for(k>>=1;k>0;k>>=1) if(x+k<=a.size() && a[x+k-1]<val) val-=a[x+k-1], x+=k;
		return x;
	}
	int upper_bound(G val)const{
		if(val<G{}) return 0;
		int x=0,k;
		for(k=1;k<=a.size();k<<=1);
		for(k>>=1;k>0;k>>=1) if(x+k<=a.size() && a[x+k-1]<=val) val-=a[x+k-1], x+=k;
		return x;
	}
};

int main(){
	int n,q; scanf("%d%d",&n,&q);
	vector<lint> a(n);
	rep(i,n) scanf("%lld",&a[i]);

	Fenwick_tree<lint> F1=a;

	Fenwick_tree<int> F2(vector<int>(n,1));
	F2.add(0,-1);
	rep(_,q){
		int type,x; scanf("%d%d",&type,&x);
		if(type==1){
			if(F2.sum(x,x+1)==1) F2.add(x,-1);
		}
		else if(type==2){
			if(F2.sum(x,x+1)==0) F2.add(x,1);
		}
		else if(type==3){
			F1.add(x-1,1);
		}
		else{
			int k=F2.sum(0,x);
			int l=F2.lower_bound(k);
			int r=F2.lower_bound(k+1);
			printf("%lld\n",F1.sum(l,r));
		}
	}

	return 0;
}
0