結果

問題 No.2809 Sort Query
ユーザー shobonvipshobonvip
提出日時 2024-07-12 21:57:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,674 ms / 2,000 ms
コード長 4,178 bytes
コンパイル時間 5,444 ms
コンパイル使用メモリ 283,028 KB
実行使用メモリ 37,888 KB
最終ジャッジ日時 2024-07-12 21:59:58
合計ジャッジ時間 103,638 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1,192 ms
37,888 KB
testcase_02 AC 1,192 ms
37,872 KB
testcase_03 AC 1,134 ms
37,888 KB
testcase_04 AC 1,193 ms
37,888 KB
testcase_05 AC 1,208 ms
37,888 KB
testcase_06 AC 1,235 ms
37,888 KB
testcase_07 AC 1,240 ms
37,888 KB
testcase_08 AC 1,541 ms
37,888 KB
testcase_09 AC 1,223 ms
37,760 KB
testcase_10 AC 1,224 ms
37,888 KB
testcase_11 AC 1,410 ms
37,888 KB
testcase_12 AC 1,448 ms
37,888 KB
testcase_13 AC 1,387 ms
37,760 KB
testcase_14 AC 1,402 ms
37,888 KB
testcase_15 AC 1,427 ms
37,888 KB
testcase_16 AC 1,491 ms
37,888 KB
testcase_17 AC 1,413 ms
37,760 KB
testcase_18 AC 1,400 ms
37,888 KB
testcase_19 AC 1,404 ms
37,888 KB
testcase_20 AC 1,360 ms
37,888 KB
testcase_21 AC 1,554 ms
37,888 KB
testcase_22 AC 1,497 ms
37,888 KB
testcase_23 AC 1,438 ms
37,888 KB
testcase_24 AC 1,507 ms
37,888 KB
testcase_25 AC 1,648 ms
37,888 KB
testcase_26 AC 1,486 ms
37,888 KB
testcase_27 AC 1,413 ms
37,888 KB
testcase_28 AC 1,387 ms
37,888 KB
testcase_29 AC 1,465 ms
37,888 KB
testcase_30 AC 1,448 ms
37,760 KB
testcase_31 AC 1,318 ms
37,888 KB
testcase_32 AC 1,335 ms
37,888 KB
testcase_33 AC 1,317 ms
37,888 KB
testcase_34 AC 1,314 ms
37,888 KB
testcase_35 AC 1,260 ms
37,888 KB
testcase_36 AC 1,291 ms
37,888 KB
testcase_37 AC 1,313 ms
37,888 KB
testcase_38 AC 1,346 ms
37,888 KB
testcase_39 AC 1,381 ms
37,888 KB
testcase_40 AC 1,336 ms
37,888 KB
testcase_41 AC 1,525 ms
37,888 KB
testcase_42 AC 1,554 ms
37,888 KB
testcase_43 AC 1,489 ms
37,760 KB
testcase_44 AC 1,500 ms
37,888 KB
testcase_45 AC 1,674 ms
37,888 KB
testcase_46 AC 1,198 ms
37,888 KB
testcase_47 AC 1,316 ms
37,888 KB
testcase_48 AC 1,207 ms
37,888 KB
testcase_49 AC 1,210 ms
37,760 KB
testcase_50 AC 1,238 ms
37,888 KB
testcase_51 AC 1,154 ms
33,792 KB
testcase_52 AC 1,240 ms
33,664 KB
testcase_53 AC 1,178 ms
33,792 KB
testcase_54 AC 1,251 ms
33,664 KB
testcase_55 AC 1,148 ms
33,792 KB
testcase_56 AC 985 ms
22,656 KB
testcase_57 AC 768 ms
16,896 KB
testcase_58 AC 726 ms
19,840 KB
testcase_59 AC 802 ms
14,592 KB
testcase_60 AC 901 ms
30,464 KB
testcase_61 AC 1,205 ms
37,504 KB
testcase_62 AC 787 ms
18,816 KB
testcase_63 AC 1,045 ms
24,192 KB
testcase_64 AC 1,356 ms
36,992 KB
testcase_65 AC 722 ms
25,856 KB
testcase_66 AC 2 ms
5,376 KB
testcase_67 AC 2 ms
5,376 KB
testcase_68 AC 2 ms
5,376 KB
testcase_69 AC 2 ms
5,376 KB
testcase_70 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}



random_device seed_gen;
mt19937 engine(seed_gen());
uniform_int_distribution<int> dist_treap(0, (1 << 30) - 1);

struct treap {
	struct node {
		node *lch, *rch;
		int par;
		ll val;
		int cnt;
		node(ll v, int p):
			lch(nullptr), rch(nullptr),
			par(p), val(v), cnt(1) {}
	};

	int calc_cnt(node* t){
		if (t == nullptr) return 0;
		return t->cnt;
	}

	node* update(node* t){
		t->cnt = calc_cnt(t->lch) + calc_cnt(t->rch) + 1;
		return t;
	}

	node* merge(node* a, node* b){
		if (a == nullptr || b == nullptr){
			if (a == nullptr) return b;
			return a;
		}

		if (a->par > b->par){
			a->rch = merge(a->rch, b);
			return update(a);
		}else{
			b->lch = merge(a, b->lch);
			return update(b);
		}
	}

	pair<node*, node*> split(node* t, int k){
		if (t == nullptr) return pair(nullptr, nullptr);
		assert(0 <= k && k <= calc_cnt(t));

		if (calc_cnt(t->lch) >= k){
			pair<node*, node*> ret = split(t->lch, k);
			t->lch = ret.second;
			return pair(ret.first, update(t));
		}else{
			pair<node*, node*> ret = split(t->rch, k - calc_cnt(t->lch) - 1);
			t->rch = ret.first;
			return pair(update(t), ret.second);
		}
	}

	node* insert(node* t, int k, ll v){
		return insert(t, k, v, dist_treap(engine));
	}

	node* insert(node* t, int k, ll v, int p){
		pair<node*, node*> tar = split(t, k);
		node* var = new node(v, p);
		return merge(merge(tar.first, var), tar.second);
	}

	node* erase(node* t, int k){
		assert(0 <= k && k < calc_cnt(t));
		pair<node*, node*> spl1 = split(t, k + 1);
		pair<node*, node*> spl2 = split(spl1.first, k);
		delete spl2.second;
		return merge(spl2.first, spl1.second);
	}

	// returns the number of elements less than v
	int lower_bound(node* t, ll v){
		if (t == nullptr) return 0;

		if (v <= t->val){
			return lower_bound(t->lch, v);
		}else{
			return calc_cnt(t->lch) + 1 + lower_bound(t->rch, v);
		}
	}

	node* insert(node* t, ll v){
		return insert(t, lower_bound(t, v), v);
	}

	// returns val
	ll get(node* t, int k){
		assert(t != nullptr);
		if (k < calc_cnt(t->lch)){
			return get(t->lch, k);
		}else if(calc_cnt(t->lch) == k){
			return t->val;
		}else{
			return get(t->rch, k - calc_cnt(t->lch) - 1);
		}
	}

	ll change(node* t, int k, ll x){
		assert(t != nullptr);
		if (k < calc_cnt(t->lch)){
			return change(t->lch, k, x);
		}else if(calc_cnt(t->lch) == k){
			t->val = x;
			return t->val;
		}else{
			return change(t->rch, k - calc_cnt(t->lch) - 1, x);
		}
	}

	
};

int main(){
	int n, q; cin >> n >> q;
	vector<ll> a(n);
	rep(i,0,n){
		cin >> a[i];
	}
	
	set<int> changes;
	rep(i,0,n){
		changes.insert(i);
	}
	
	treap T;
	treap::node* root = nullptr;
	rep(i,0,n){
		root = T.insert(root, i, a[i]);
	}
	
	while(q--){
		int t; cin >> t;
		if (t == 1){
			int k; cin >> k;
			k--;
			ll x; cin >> x;
			changes.insert(k);
			T.change(root, k, x);
		}else if(t == 2){
			vector<ll> vals;
			int piv = 0;
			for(int i: changes){
				vals.push_back(T.get(root, i-piv));
				root = T.erase(root, i-piv);
				piv++;
			}
			for(ll x: vals){
				root = T.insert(root, x);
			}
			changes.clear();
		}else{
			int k; cin >> k;
			k--;
			cout << T.get(root, k) << '\n';
		}
	}

}

0