結果

問題 No.3167 [Cherry 7th Tune C] Cut in Queue
ユーザー shobonvip
提出日時 2025-05-30 22:20:03
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 192 ms / 2,500 ms
コード長 2,675 bytes
コンパイル時間 4,627 ms
コンパイル使用メモリ 252,120 KB
実行使用メモリ 26,140 KB
最終ジャッジ日時 2025-05-30 22:20:18
合計ジャッジ時間 13,390 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
	author:  shobonvip
	created: 2025.05.30 21:58:27
**/

#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--)
#define all(v) v.begin(), v.end()

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;
}

int op(int a, int b) {
	return a + b;
}
int e() {
	return 0;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n; cin >> n;
	vector<int> a(n);
	rep(i,0,n) {
		cin >> a[i];
		a[i]--;
	}

	int q; cin >> q;
	vector<int> nxt(n+q, -1);
	vector<int> prv(n+q, -1);
	int lst = a[n-1];
	rep(i,0,n-1) {
		nxt[a[i]] = a[i+1];
		prv[a[i+1]] = a[i];
	}

	int piv = 0;
	vector<int> typ(q), val(q);
	rep(i,0,q) {
		cin >> typ[i];
		if (typ[i] == 1) {
			cin >> val[i];
			val[i]--;
			if (val[i] == -1) {
				nxt[lst] = n+piv;
				prv[n+piv] = lst;
				lst = n+piv;
				piv++;
			}else{
				if (prv[val[i]] >= 0) {
					int lft = prv[val[i]];
					int rgt = val[i];
					nxt[lft] = n+piv;
					prv[rgt] = n+piv;
					nxt[n+piv] = rgt;
					prv[n+piv] = lft;
				}else {
					int rgt = val[i];
					prv[rgt] = n+piv;
					nxt[n+piv] = rgt;
				}
				piv++;
			}
		}else if (typ[i] == 3){
			cin >> val[i];
		}
	}

	segtree<int,op,e> seg(n+q);

	vector<int> order;
	int x = lst;
	order.push_back(x);
	while (prv[x] != -1) {
		x = prv[x];
		order.push_back(x);
	}
	reverse(all(order));

	vector<int> taio(n+q);
	rep(i,0,(int)order.size()) {
		taio[order[i]] = i;
	}

	rep(i,0,n) {
		seg.set(taio[i], +1);
	}

	int y;
	auto f = [&](int x) -> bool {
		return x < y;
	};

	piv = 0;
	rep(i,0,q) {
		if (typ[i] == 1) {
			seg.set(taio[n+piv], +1);
			piv++;
		}else if (typ[i] == 2){
			y = 1;
			int v = seg.max_right(0, f);
			seg.set(v, 0);
		}else{
			y = val[i];
			int v = seg.max_right(0, f);
			cout << order[v] + 1 << '\n';
		}
	}



}
0