結果

問題 No.3167 [Cherry 7th Tune C] Cut in Queue
ユーザー mihhiael
提出日時 2025-05-30 23:49:58
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
RE  
実行時間 -
コード長 12,741 bytes
コンパイル時間 3,893 ms
コンパイル使用メモリ 302,912 KB
実行使用メモリ 36,776 KB
最終ジャッジ日時 2025-05-30 23:50:23
合計ジャッジ時間 23,519 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 1 RE * 1
other AC * 2 RE * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#if __has_include("all.h")
#include "all.h"
#else
// necessary for snippet
#include <algorithm>
#include <concepts>
#include <cstdint>
#include <ios>
#include <iostream>
#include <istream>
#include <limits>
#include <ostream>
#include <queue>
#include <random>
#include <ranges>
#include <set>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include <atcoder/modint.hpp>
// often use
#include <map>
#include <cassert>
#endif

inline namespace {

using namespace std;
using ll = long long;
using pall = pair<ll, ll>;
template<class T> using vec = vector<T>;
template<class T> using veve = vec<vec<T>>;
using vell = vec<ll>;
using vest = vec<string>;
using vebo = basic_string<bool>;
using vevell = veve<ll>;
template<class T> using mset = multiset<T>;
template<class T> using priority_queue_ascend = priority_queue<T, vec<T>, decltype([](const T a, const T b) { return a > b; })>;
const ll inf = numeric_limits<ll>::max();
const string sp = " ";
const string lf = "\n";
const auto &npos = string::npos;
const vec<pall> grid_move4 = {
	{0, 1},
	{-1, 0},
	{0, -1},
	{1, 0}
};
const vec<pall> grid_move8 = [] {
	auto ret = grid_move4;
	ret.insert(ret.end(), {
		{-1, 1},
		{-1, -1},
		{1, -1},
		{1, 1}
	});
	return ret;
}();
constexpr ll MOD = 998244353;
// constexpr ll MOD = 1e9 + 7;
#define cont continue
#define br break
#define auot auto
#define whlie while
#define foR for
#define cosnt const
auto &ciN = cin;
auto &icn = cin;
auto &icN = cin;
constexpr bool ture = true;

using namespace atcoder;
using mint = static_modint<MOD>;

#define times(N) static_assert(is_integral_v<decltype((N) + 0)>, "times(): N must be integral"); for(typedef decltype((N) + 0) _int; [[maybe_unused]] const _int _i: views::iota((_int)0, (N)))
#define tiems times
#define itmes times

template<int M>
istream &operator>>(istream &in, static_modint<M> &i) {
	intmax_t tmp;
	in >> tmp;
	i = tmp;
	return in;
}
template<int M>
ostream &operator<<(ostream &out, const static_modint<M> &i) {
	return out << i.val();
}

template<class T, class U>
istream &operator>>(istream &in, pair<T, U> &p) {
	return in >> p.first >> p.second;
}
template<class T, class U>
ostream &operator<<(ostream &out, const pair<T, U> &p) {
	return out << p.first << sp << p.second;
}

template<class T>
istream &operator>>(istream &in, vec<T> &v) {
	for(auto &&e: v) {
		in >> e;
	}
	return in;
}

namespace myinput {
	template<class... Ts>
	istream &in(Ts&... vecs) {
		static_assert(sizeof...(vecs) != 0, "myfunc::in(): At least one vector must be provided");
		const set sizes = { vecs.size()... };
		if(sizes.size() > 1) {
			throw invalid_argument("myfunc::in(): All vectors must have the same size");
		}
		times(*sizes.begin()) {
			((cin >> vecs[_i]), ...);
		}
		return cin;
	}
}
using myinput::in;

ostream &out(const ranges::range auto &v, const string &delim, ostream &out = cout) {
	for(const auto &e: v) {
		out << e << delim;
	}
	return out;
}

[[nodiscard]] const string &yesno(const bool cond, const string &yes = "Yes", const string &no = "No") noexcept {
	if(cond) return yes;
	return no;
}

// [mi, ma)
[[nodiscard]] uint64_t randint(const uint64_t mi, const uint64_t ma) noexcept {
	static random_device seed;
	static mt19937_64 mt(seed());
	if(mi > ma) [[unlikely]] return randint(ma, mi);
	if(mi == ma) [[unlikely]] return mi;
	const uint64_t w = ma - mi;
	uint64_t r;
	do {
		r = mt();
	} while(mt.max() - mt.max() % w <= r);
	return r % w + mi;
}

template<class T, class U>
requires common_with<T, U>
[[nodiscard]] constexpr auto min(T &&a, U &&b) noexcept {
	return std::min<common_type_t<T, U>>(std::forward<T>(a), std::forward<U>(b));
}
template<class T, class U>
requires common_with<T, U>
[[nodiscard]] constexpr auto max(T &&a, U &&b) noexcept {
	return std::max<common_type_t<T, U>>(std::forward<T>(a), std::forward<U>(b));
}

template<class T>
[[nodiscard]] const T &min(const vec<T> &v) {
	return *ranges::min_element(v);
}
template<class T>
[[nodiscard]] const T &max(const vec<T> &v) {
	return *ranges::max_element(v);
}
template<class... Args>
[[nodiscard]] auto reduce(const ranges::range auto &v, Args... args) {
	return reduce(v.begin(), v.end(), args...);
}

[[nodiscard]] constexpr ll powll(ll a, ll b, const ll m = inf) {
	if(b < 0) [[unlikely]] throw invalid_argument("powll(): exponent less than zero");
	if(m < 1) [[unlikely]] throw invalid_argument("powll(): modulo less than one");
	a %= m;
	ll ret = 1;
	while(b) {
		if(b % 2) ret *= a, ret %= m;
		a *= a, a %= m;
		b /= 2;
	}
	return ret;
}

template<class T, class U>
requires assignable_from<T&, U> && totally_ordered_with<T, U>
bool mini(T &var, U &&val) noexcept {
	const bool cmp = var > val;
	if(cmp) var = val;
	return cmp;
}
template<class T, class U>
requires assignable_from<T&, U> && totally_ordered_with<T, U>
bool maxi(T &var, U &&val) noexcept {
	const bool cmp = var < val;
	if(cmp) var = val;
	return cmp;
}

namespace myclass {
	class [[nodiscard]] grid_base {
		public:
		grid_base(const ll h, const ll w) noexcept : height(h), width(w) {}

		[[nodiscard]] ll operator()(const ll i, const ll j) const noexcept {
			if(!isvalid(i, j)) return -1;
			return i * width + j;
		}

		[[nodiscard]] ll operator()(const pall &p) const noexcept {
			return (*this)(p.first, p.second);
		}

		protected:
		bool isvalid(const ll i, const ll j) const noexcept {
			return 0 <= i && 0 <= j && i < height && j < width;
		}
		const ll height, width;
	};

	class [[nodiscard]] grid_seen : public myclass::grid_base {
		public:
		grid_seen(const ll h, const ll w) : grid_base(h, w) {
			visited = vebo(h * w, false);
		}

		[[nodiscard]] bool &seen(const ll i, const ll j) & {
			if(!isvalid(i, j)) [[unlikely]] throw out_of_range("grid::seen(): out of range");
			return visited[i * width + j];
		}

		[[nodiscard]] bool &seen(const pall &p) & {
			return seen(p.first, p.second);
		}

		private:
		vebo visited;
	};
}

using grid_lite = myclass::grid_base;
using myclass::grid_seen;

template<class T>
auto erase_single(mset<T> &mset, T &&v) {
	const auto it = mset.find(v);
	if(it == mset.end()) [[unlikely]] throw invalid_argument("erase_single(): why v not in mset!?!?");
	return mset.erase(it);
}

}

void solve();

int main(void) {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	solve();
	return 0;
}

#include <bits/stdc++.h>

// using namespace std;
// using ull = unsigned long long;

// ull randint(ull, ull);

template<class T, T (*op)(T, T), T (*e)() = nullptr>
class treap {
	private:
	struct Node {
		T val;
		T sum;
		bool rev = false;
		unsigned pri = 0;
		int size = 0;
		int lchild = null;
		int rchild = null;
		static constexpr int null = 0;
	};

	public:
	treap() {
		if constexpr (e != nullptr) {
			nodes[Node::null].val = nodes[Node::null].sum = e();
		}
	}

	private:
	int merge(const int i, const int j) {
		if(isnull(i)) return j;
		if(isnull(j)) return i;

		if(nodes[i].pri > nodes[j].pri) {
			push(i);
			nodes[i].rchild = merge(nodes[i].rchild, j);
			return update(i);
		} else {
			push(j);
			nodes[j].lchild = merge(i, nodes[j].lchild);
			return update(j);
		}
	}

	// [0, k), [k, size)
	pair<int, int> split(const int i, const int k) {
		if(isnull(i)) return {Node::null, Node::null};

		push(i);
		auto &node = nodes[i];
		if(k <= nodes[node.lchild].size) {
			const auto [l, r] = split(node.lchild, k);
			node.lchild = r;
			return {l, update(i)};
		} else {
			const auto [l, r] = split(node.rchild, k - nodes[node.lchild].size - 1);
			node.rchild = l;
			return {update(i), r};
		}
	}

	public:
	void insert(const int k, const T &v) {
		assert(0 <= k && k <= size());
		const auto [l, r] = split(root, k);
		root = merge(merge(l, create_node(v)), r);
	}

	void erase(const int k) {
		assert(0 <= k && k < size());
		const auto [tmp, r] = split(root, k + 1);
		const auto [l, _] = split(tmp, k);
		root = merge(l, r);
	}

	public:
	T operator[](const int k) {
		assert(0 <= k && k < size());
		return prod(k, k + 1);
	}

	void set(const int k, const T &v) {
		assert(0 <= k && k < size());
		const auto [tmp, r] = split(root, k + 1);
		const auto [l, _] = split(tmp, k);
		root = merge(merge(l, create_node(v)), r);
	}

	void reverse(const int l, const int r) {
		assert(0 <= l && l <= r && r <= size());
		const auto [tmp, rt] = split(root, r);
		const auto [lt, mid] = split(tmp, l);
		nodes[mid].rev = !nodes[mid].rev;
		root = merge(merge(lt, mid), rt);
	}

	private:
	T _prod(const int i, const int l, const int r) {
		const auto [tmp, rt] = split(i, r);
		const auto [lt, midt] = split(tmp, l);
		const T res = nodes[midt].sum;
		root = merge(merge(lt, midt), rt);
		return res;
	}
	public:
	T prod(const int l, const int r) {
		assert(0 <= l && l < r && r <= size());
		return _prod(root, l, r);
	}

	T all_prod() const {
		assert(size() > 0);
		return nodes[root].sum;
	}

	int size() const noexcept {
		return nodes[root].size;
	}

	private:
	void _output_tree(const int i) noexcept {
		if(isnull(i)) return;
		push(i);
		if(const int child = nodes[i].lchild; !isnull(child)) cout << i << " " << child << "\n", _output_tree(child);
		if(const int child = nodes[i].rchild; !isnull(child)) cout << i << " " << child << "\n", _output_tree(child);
	}
	public:
	void _output_tree() noexcept {
		_output_tree(root);
	}

	private:
	int _max_right(const int i, const T &csum, const auto &f) {
		if(isnull(i)) return 0;

		push(i);
		const auto &node = nodes[i];
		const T &ltsum = nodes[node.lchild].sum;
		if(const T lsum = op(csum, ltsum); !f(lsum)) {
			return _max_right(node.lchild, csum, f);
		} else if(const T lmsum = op(lsum, node.val); !f(lmsum)) {
			return nodes[node.lchild].size;
		} else {
			return nodes[node.lchild].size + 1 + _max_right(node.rchild, lmsum, f);
		}
	}
	public:
	int max_right(const int l, const auto &f) {
		static_assert(e != nullptr, "e() must not be nullptr");
		assert(0 <= l && l <= size());
		assert(f(e()));

		const auto [lt, rt] = split(root, l);
		const int res = l + _max_right(rt, e(), f);
		root = merge(lt, rt);
		return res;
	}

	private:
	int _min_left(const int i, const T &csum, const auto &f) {
		if(isnull(i)) return 0;

		push(i);
		const auto &node = nodes[i];
		const T &rtsum = nodes[node.rchild].sum;
		if(const T rsum = op(rtsum, csum); !f(rsum)) {
			return _min_left(node.rchild, csum, f);
		} else if(const T rmsum = op(node.val, rsum); !f(rmsum)) {
			return nodes[node.rchild].size;
		} else {
			return nodes[node.rchild].size + 1 + _min_left(node.lchild, rmsum, f);
		}
	}
	public:
	int min_left(const int r, const auto &f) {
		static_assert(e != nullptr, "e() must not be nullptr");
		assert(0 <= r && r <= size());
		assert(f(e()));

		const auto [lt, rt] = split(root, r);
		const int res = r - _min_left(lt, e(), f);
		root = merge(lt, rt);
		return res;
	}

	private:
	bool isnull(const int i) const noexcept {
		return i == Node::null;
	}

	int create_node(const T &v) {
		nodes.push_back(Node {v, v, false, (unsigned)randint(0, 1U << 30), 1});
		return nodes.size() - 1;
	}

	int update(const int i) noexcept {
		auto &node = nodes[i];
		const int ls = nodes[node.lchild].size;
		const int rs = nodes[node.rchild].size;
		node.size = 1 + ls + rs;
		if(!isnull(node.lchild)) {
			node.sum = op(nodes[node.lchild].sum, node.val);
		} else {
			node.sum = node.val;
		}
		if(!isnull(node.rchild)) {
			node.sum = op(node.sum, nodes[node.rchild].sum);
		}
		return i;
	}

	void push(const int i) noexcept {
		auto &node = nodes[i];
		if(!node.rev) return;
		swap(node.lchild, node.rchild);
		if(!isnull(node.lchild)) nodes[node.lchild].rev ^= true;
		if(!isnull(node.rchild)) nodes[node.rchild].rev ^= true;
		node.rev = false;
	}

	int root = Node::null;
	vector<Node> nodes{1};
};

// template class treap<ull, [](ull a, ull b) { return a + b; }>;

#include <atcoder/lazysegtree.hpp>

void solve() {
	ll n;
	cin >> n;
	vell a(n);
	cin >> a;
	treap<ll, [](ll a, ll b) { return a + b; }> at;
	for(const auto &e: a) at.insert(at.size(), e);
	for(auto &e: a) e--;
	vell idxs(n);
	for(intmax_t i=0;i<intmax_t(n);i++) {
		idxs[a[i]] = i;
	}
	lazy_segtree<ll, [](ll a, ll b) { return a + b; }, [] { return 0ll; }, ll, [](ll f, ll s) { return f + s; }, [](ll f, ll g) { return f + g; }, [] { return 0ll; }> t(idxs);
	ll q;
	cin >> q;
	vell b(q);
	iota(b.begin(), b.end(), n);
	ranges::reverse(b);
	times(q) {
		ll op;
		cin >> op;
		if(op == 1) {
			ll aq;
			cin >> aq;
			const ll bi = b.back();
			b.pop_back();
			if(aq == 0) {
				at.insert(at.size(), bi);
				cont;
			}
			aq--;
			const ll idx = t.get(idxs[aq]);
			t.apply(idxs[aq], n, 1);
			at.insert(idx, bi);
		} else if(op == 2) {
			at.erase(0);
			t.apply(0, n, -1);
		} else if(op == 3) {
			ll k;
			cin >> k;
			k--;
			cout << at[k] <<lf;
		}

	}
}
0