結果

問題 No.3167 [Cherry 7th Tune C] Cut in Queue
ユーザー kwm_t
提出日時 2025-06-02 15:19:52
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 547 ms / 2,500 ms
コード長 2,026 bytes
コンパイル時間 7,065 ms
コンパイル使用メモリ 331,972 KB
実行使用メモリ 45,576 KB
最終ジャッジ日時 2025-06-02 15:20:13
合計ジャッジ時間 20,268 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r) - 1;i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
int op(int x, int  y) { return x + y; };
int e() { return 0; }
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n; cin >> n;
	vector<int>a(n + 1);
	rep(i, n) cin >> a[i + 1];
	int q; cin >> q;
	vector<P>qs(q);
	vector<vector<int>>to(n + 1);
	{
		int next = n + 1;
		for (auto &[t, x] : qs) {
			cin >> t;
			if (t == 1) {
				cin >> x;
				to[x].push_back(next++);
				to.emplace_back();
			}
			else if (t == 3) {
				cin >> x;
				x--;
			}
		}
	}
	vector<int>order;
	auto dfs = [&](auto &&self, int v)->void {
		for (auto nv : to[v])self(self, nv);
		order.emplace_back(v);
	};
	rep(i, n) dfs(dfs, a[i + 1]);
	dfs(dfs, 0);
	int sz = order.size();
	vector<int>pos(sz);
	rep(i, sz)pos[order[i]] = i;
	segtree<int, op, e> seg(sz);
	rep(i, n + 1)seg.set(pos[i], 1);
	int base = 0;
	{
		int next = n + 1;
		for (auto [t, x] : qs) {
			if (t == 1) {
				seg.set(pos[next++], 1);
			}
			else if (t == 2) {
				base++;
			}
			else if (t == 3) {
				{
					int p = seg.max_right(base, [&](int v) { return v <= x; });
					cout << order[p] << endl;
				}
				{
					//int p = seg.max_right(0, [&](int v) { return v <= x + base; });
					//cout << order[p] << endl;
				}
			}
		}
	}
	return 0;
}
0