結果
| 問題 |
No.3167 [Cherry 7th Tune C] Cut in Queue
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-05-30 23:24:40 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 30,954 bytes |
| コンパイル時間 | 4,608 ms |
| コンパイル使用メモリ | 318,376 KB |
| 実行使用メモリ | 99,424 KB |
| 最終ジャッジ日時 | 2025-05-30 23:25:03 |
| 合計ジャッジ時間 | 21,210 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 6 WA * 3 RE * 1 TLE * 2 -- * 31 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vl = vector<ll>;
using vul = vector<ull>;
using vs = vector<string>;
using vvs = vector<vs>;
using vd = vector<double>;
using vvl = vector<vl>;
using vvd = vector<vd>;
using xy = pair<ll, ll>;
using vxy = vector<xy>;
using vvxy = vector<vxy>;
using vvvl = vector<vvl>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define cnt(i, n, m) for (ll i = (n); i <= (m); i++)
#define each(ite, C) for (auto ite = (C).begin(); ite != (C).end(); ite++)
#define reach(ite, C) for (auto ite = (C).rbegin(); ite != (C).rend(); ite++)
#define yn(b) cout << (((b) > 0) ? "Yes" : "No") << endl;
#define square(a) ((a) * (a))
#define bfind(a, b) (((a) >> (b)) & 1ll)
#define bset(a, b, c) (((c) != 0) ? (a) | (1ll << (b)) : ~((~(a)) | (1 << (b))))
#define nth(a, b) (((a) >> (b)) & 1)
namespace zz_zout {
void out(const ll& showBit, const ll& Hier, const string& header,
const string& space, const string& endline) {};
template <typename T>
void out(const ll& showBit, const ll& Hier, const string& header,
const string& space, const string& endline, const T& x) {
if (showBit == 0) {
cout << header << x << space << endline;
} else {
cout << header;
bitset<64> bit(x);
rep(i, showBit) cout << bit[i];
cout << space << endline;
}
};
template <typename E1, typename E2>
void out(const ll& showBit, const ll& Hier, const string& header,
const string& space, const string& endline, const pair<E1, E2>& x) {
cout << header << "< ";
out(showBit, 0, "", " ", "", x.first);
out(showBit, 0, "", " ", "", x.second);
cout << ">" << space << endline;
};
template <typename E>
void out(const ll& showBit, const ll& Hier, const string& header,
const string& space, const string& endline, const vector<E>& x) {
cout << header << "[" << space;
if (Hier) cout << endline;
for (E xx : x) {
if (Hier) {
if (Hier > 0) {
out(showBit, Hier - 1, header + " ", space, endline, xx);
}
} else {
out(showBit, 0, "", space, "", xx);
}
}
if (Hier) cout << header;
cout << "]" << space << endline;
};
template <typename E>
void out(const ll& showBit, const ll& Hier, const string& header,
const string& space, const string& endline, const set<E>& x) {
cout << header << "{" << space;
if (Hier) cout << endline;
for (E xx : x) {
if (Hier) {
if (Hier > 0) {
out(showBit, Hier - 1, header + " ", space, endline, xx);
}
} else {
out(showBit, 0, "", space, "", xx);
}
}
if (Hier) cout << header;
cout << "}" << space;
if (Hier) cout << endline;
};
template <typename E>
void out(const ll& showBit, const ll& Hier, const string& header,
const string& space, const string& endline, const multiset<E>& x) {
cout << header << "{" << space;
if (Hier) cout << endline;
for (E xx : x) {
if (Hier) {
if (Hier > 0) {
out(showBit, Hier - 1, header + " ", space, endline, xx);
}
} else {
out(showBit, 0, "", space, "", xx);
}
}
if (Hier) cout << header;
cout << "}" << space;
if (Hier) cout << endline;
};
template <typename E>
void out(const ll& showBit, const ll& Hier, const string& header,
const string& space, const string& endline,
const unordered_set<E>& x) {
cout << header << "{" << space;
if (Hier) cout << endline;
for (E xx : x) {
if (Hier) {
if (Hier > 0) {
out(showBit, Hier - 1, header + " ", space, endline, xx);
}
} else {
out(showBit, 0, "", space, "", xx);
}
}
if (Hier) cout << header;
cout << "}" << space;
if (Hier) cout << endline;
};
template <typename E>
void out(const ll& showBit, const ll& Hier, const string& header,
const string& space, const string& endline,
const unordered_multiset<E>& x) {
cout << header << "{" << space;
if (Hier) cout << endline;
for (E xx : x) {
if (Hier) {
if (Hier > 0) {
out(showBit, Hier - 1, header + " ", space, endline, xx);
}
} else {
out(showBit, 0, "", space, "", xx);
}
}
if (Hier) cout << header;
cout << "}" << space;
if (Hier) cout << endline;
};
template <typename E1, typename E2>
void out(const ll& showBit, const ll& Hier, const string& header,
const string& space, const string& endline, const map<E1, E2>& x) {
cout << header << "{" << space;
if (Hier) cout << endline;
for (pair<E1, E2> xx : x) {
if (Hier) {
if (Hier > 0) {
out(showBit, Hier - 1, header + " ", space, endline, xx);
}
} else {
out(showBit, 0, "", space, "", xx);
}
}
if (Hier) cout << header;
cout << "}" << space;
if (Hier) cout << endline;
};
template <typename E1, typename E2>
void out(const ll& showBit, const ll& Hier, const string& header,
const string& space, const string& endline,
const multimap<E1, E2>& x) {
cout << header << "{" << space;
if (Hier) cout << endline;
for (pair<E1, E2> xx : x) {
if (Hier) {
if (Hier > 0) {
out(showBit, Hier - 1, header + " ", space, endline, xx);
}
} else {
out(showBit, 0, "", space, "", xx);
}
}
if (Hier) cout << header;
cout << "}" << space;
if (Hier) cout << endline;
};
template <typename E1, typename E2>
void out(const ll& showBit, const ll& Hier, const string& header,
const string& space, const string& endline,
const unordered_map<E1, E2>& x) {
cout << header << "{" << space;
if (Hier) cout << endline;
for (pair<E1, E2> xx : x) {
if (Hier) {
if (Hier > 0) {
out(showBit, Hier - 1, header + " ", space, endline, xx);
}
} else {
out(showBit, 0, "", space, "", xx);
}
}
if (Hier) cout << header;
cout << "}" << space;
if (Hier) cout << endline;
};
template <typename E1, typename E2>
void out(const ll& showBit, const ll& Hier, const string& header,
const string& space, const string& endline,
const unordered_multimap<E1, E2>& x) {
cout << header << "{" << space;
if (Hier) cout << endline;
for (pair<E1, E2> xx : x) {
if (Hier) {
if (Hier > 0) {
out(showBit, Hier - 1, header + " ", space, endline, xx);
}
} else {
out(showBit, 0, "", space, "", xx);
}
}
if (Hier) cout << header;
cout << "}" << space;
if (Hier) cout << endline;
};
template <typename E>
void out(const ll& showBit, const ll& Hier, const string& header,
const string& space, const string& endline, const stack<E>& x) {
cout << header << "(" << space;
if (Hier) cout << endline;
stack<E> sta = x;
while (!sta.empty()) {
E xx = sta.top();
sta.pop();
if (Hier) {
if (Hier > 0) {
out(showBit, Hier - 1, header + " ", space, endline, xx);
}
} else {
out(showBit, 0, "", space, "", xx);
}
}
if (Hier) cout << header;
cout << "]" << space;
if (Hier) cout << endline;
};
template <typename E>
void out(const ll& showBit, const ll& Hier, const string& header,
const string& space, const string& endline, const queue<E>& x) {
cout << header << "(" << space;
if (Hier) cout << endline;
queue<E> que = x;
while (!que.empty()) {
E xx = que.front();
que.pop();
if (Hier) {
if (Hier > 0) {
out(showBit, Hier - 1, header + " ", space, endline, xx);
}
} else {
out(showBit, 0, "", space, "", xx);
}
}
if (Hier) cout << header;
cout << "]" << space;
if (Hier) cout << endline;
};
template <typename E>
void out(const ll& showBit, const ll& Hier, const string& header,
const string& space, const string& endline, const deque<E>& x) {
cout << header << "(" << space;
if (Hier) cout << endline;
deque<E> que = x;
while (!que.empty()) {
E xx = que.front();
que.pop_front();
if (Hier) {
if (Hier > 0) {
out(showBit, Hier - 1, header + " ", space, endline, xx);
}
} else {
out(showBit, 0, "", space, "", xx);
}
}
if (Hier) cout << header;
cout << ")" << space;
if (Hier) cout << endline;
};
template <typename E>
void out(const ll& showBit, const ll& Hier, const string& header,
const string& space, const string& endline,
const priority_queue<E>& x) {
cout << header << "(" << space;
if (Hier) cout << endline;
priority_queue<E> que = x;
while (!que.empty()) {
E xx = que.top();
que.pop();
if (Hier) {
if (Hier > 0) {
out(showBit, Hier - 1, header + " ", space, endline, xx);
}
} else {
out(showBit, 0, "", space, "", xx);
}
}
if (Hier) cout << header;
cout << "]" << space;
if (Hier) cout << endline;
};
template <typename T, typename... U>
void out(const ll& showBit, const ll& Hier, const string& header,
const string& space, const string& endline, const T& x,
const U&... y) {
out(showBit, Hier, header, space, endline, x);
out(showBit, Hier, header, space, endline, y...);
};
void zout() { cout << endl; };
template <int Hier = 0, int showBit = 0, typename... T>
void zout(const T&... x) {
string endline = "\n";
if (Hier == 0) endline = "";
out(showBit, Hier, "", " ", endline, x...);
if (Hier == 0) cout << endl;
};
} // namespace zz_zout
using namespace zz_zout;
namespace zz_time {
std::chrono::_V2::system_clock::time_point START_TIME =
std::chrono::system_clock::now();
ll currentTime(std::chrono::_V2::system_clock::time_point st = START_TIME) {
std::chrono::_V2::system_clock::time_point now =
std::chrono::system_clock::now();
return (ll)(std::chrono::duration_cast<std::chrono::milliseconds>(now - st)
.count());
};
}; // namespace zz_time
using namespace zz_time;
namespace zz_random {
std::random_device RANDOM_DEVICE;
std::mt19937 RANDOM_MT;
void setSeed(const ll& seed = RANDOM_DEVICE()) {
RANDOM_MT = std::mt19937(seed);
};
std::uniform_int_distribution<int> createIntDistribution(const ll& L = 0,
const ll& R = 1) {
return std::uniform_int_distribution<int>(L, R);
};
std::uniform_real_distribution<double> createDoubleDistribution(
const double& L = 0.0, const double& R = 1.0) {
return std::uniform_real_distribution<double>(L, R);
};
std::normal_distribution<double> createNormalDistribution(
const double& average = 0.0, const double& standard_deviation = 1.0) {
return std::normal_distribution<double>(average, standard_deviation);
}
bool getProbability(const double& p = 0.5) {
return (createDoubleDistribution()(RANDOM_MT) <= 0.5);
}
}; // namespace zz_random
using namespace zz_random;
namespace zz_modulo {
class pp {
public:
ll e = 0;
ll mod = 2;
ll inv = 0;
pp& operator=(const pp& x);
pp operator+();
pp operator-();
template <typename T>
pp& operator+=(const T& y);
template <typename T>
pp& operator-=(const T& y);
template <typename T>
pp& operator*=(const T& y);
template <typename T>
pp& operator/=(T& y);
ll exp(ll x, ll I, ll modulo);
pp exp(pp& x, ll I);
pp exp(ll I);
void cal_inv();
pp inversion();
string str() const;
pp(const ll& elm = 0, const ll& modulo = 998244353, const ll& inversion = 0) {
mod = modulo;
e = elm;
if (e < 0) {
e = mod - ((-e) % mod);
} else {
e %= mod;
}
mod = modulo;
if (inversion < 0) {
cal_inv();
} else if (inversion != 0) {
inv = inversion;
}
};
static vector<pp> create_vp(ll N, const ll& modulo);
static pp chineseRemainderTheorem(vector<pp> vx);
static pp garner(vector<pp> vx, const ll& mod);
};
pp& pp::operator=(const pp& x) {
this->e = x.e;
this->mod = x.mod;
this->inv = x.inv;
return *this;
};
pp operator+(const pp& x, const pp& y) {
ll elm = x.e + y.e;
if (elm > x.mod) elm -= x.mod;
return pp(elm, x.mod);
};
template <typename T>
pp operator+(const pp& x, const T& y) {
pp z(y, x.mod);
return (x + z);
};
template <typename T>
pp operator+(const T& x, const pp& y) {
pp z(x, y.mod);
return (z + y);
};
template <typename T>
pp& pp::operator+=(const T& y) {
*this = (*this) + y;
return *this;
};
pp pp::operator+() { return *this; };
pp operator-(const pp& x, const pp& y) {
ll elm = x.e - y.e;
if (elm < 0) elm += x.mod;
return pp(elm, x.mod);
};
template <typename T>
pp operator-(const pp& x, const T& y) {
pp z(y, x.mod);
return (x - z);
};
template <typename T>
pp operator-(const T& x, const pp& y) {
pp z(x, y.mod);
return (z - y);
};
template <typename T>
pp& pp::operator-=(const T& y) {
*this = (*this) - y;
return *this;
};
pp pp::operator-() { return (0 - (*this)); };
pp operator*(const pp& x, const pp& y) {
return pp((x.e * y.e) % x.mod, x.mod, (x.inv * y.inv) % x.mod);
};
template <typename T>
pp operator*(const pp& x, const T& y) {
pp z(y, x.mod);
return (x * z);
};
template <typename T>
pp operator*(const T& x, const pp& y) {
pp z(x, y.mod);
return (z * x);
};
template <typename T>
pp& pp::operator*=(const T& y) {
*this = (*this) * y;
return *this;
};
pp operator/(const pp& x, pp& y) { return (x * y.inversion()); }
template <typename T>
pp operator/(const pp& x, T& y) {
pp z(y, x.mod);
return (x / z);
};
template <typename T>
pp operator/(const T& x, pp& y) {
pp z(x, y.mod);
return (z / y);
};
template <typename T>
pp& pp::operator/=(T& y) {
*this = (*this) / y;
return *this;
};
bool operator==(const pp& x, const pp& y) { return (x.e == y.e); }
template <typename T>
bool operator==(const T& x, const pp& y) {
return (x == y.e);
};
template <typename T>
bool operator==(const pp& x, const T& y) {
return (x.e == y);
};
bool operator!=(const pp& x, const pp& y) { return !(x == y); }
template <typename T>
bool operator!=(const T& x, const pp& y) {
return !(x == y);
};
template <typename T>
bool operator!=(const pp& x, const T& y) {
return !(x == y);
};
ll pp::exp(ll x, ll I, ll modulo) {
ll y = 1;
if (x < 0) {
x = (modulo - ((-x) % modulo));
} else {
x %= modulo;
}
while (I > 0) {
if (I & 1) {
y = (y * x) % modulo;
}
x = (x * x) % modulo;
I /= 2;
};
return y;
};
pp pp::exp(pp& x, ll I) {
x.cal_inv();
pp xx = x;
pp y(1, xx.mod, 1);
while (I > 0) {
if (I & 1) {
y = (y * xx);
}
xx = (xx * xx);
I /= 2;
};
return y;
}
pp pp::exp(ll I) { return exp(*this, I); }
void pp::cal_inv() {
if (this->inv <= 0) this->inv = exp(this->e, this->mod - 2, this->mod);
};
pp pp::inversion() {
cal_inv();
return pp(this->inv, this->mod, this->e);
}
string pp::str() const {
return ("<epi " + to_string(e) + " \t" + to_string(mod) + " \t" +
to_string(inv) + " >");
}
using vp = vector<zz_modulo::pp>;
vector<pp> pp::create_vp(ll N, const ll& modulo) {
// m=m%i+m/i * i
// 0- (m % i) = (m/i)*i
cout << "!!" << endl;
N = max<ll>(1, N);
vector<pp> vec(N + 1);
vec[0].e = 0;
vec[0].mod = modulo;
vec[0].inv = 1;
vec[1].e = 1;
vec[1].mod = modulo;
vec[1].inv = 1;
cnt(i, 2, N) {
vec[i] = pp(i, modulo);
cout << "@[" << i << "]=" << vec[i].str() << endl;
cout << "---" << vec[modulo / i].str() << flush;
cout << " " << vec[modulo % i].str() << endl;
vec[i].inv = (0 - ((modulo / i) * vec[modulo % i].inversion())).e;
cout << " [" << i << "]=" << vec[i].str() << endl;
}
return vec;
};
pp chineseRemainderTheorem(vector<pp>& vx) {
ll m = 1;
each(ite, vx) m *= ite->mod;
pp ans(0, m);
each(ite, vx) {
// ll Ai = R[i];
// ll Mi = MMM / MM[i];
// ll Ni = pp(Mi, MM[i]).inversion().e;
// ANS += Ai * Mi * Ni;
ll Ai = ite->e;
ll Mi = m / ite->mod;
ll Ni = pp(Mi, ite->mod, -1).inv;
ans += pp(Ai * Mi * Ni, m);
// zout(ans.str(), ite->str(), m, Ai, Mi, Ni);
}
return ans;
};
pp garner(vector<pp>& vx, const ll& mod) {
ll N = vx.size();
vector<vector<pp>> R(N, vector<pp>(N));
rep(i, N) {
cnt(j, i, N - 1) {
if (i == j) {
R[i][i] = vx[i].inversion();
} else {
R[i][j] = pp(vx[i].e, vx[j].mod, -1).inversion();
}
// zout("#ij=", i, j);
}
}
vector<pp> X(N);
rep(i, N) {
X[i] = vx[i];
rep(j, i) {
// zout("ij=", i, j);
X[i] = (X[i] - X[j].e) * R[j][i];
}
// cout << "x[" << i << "]=" << X[i].str() << endl;
}
// rep(i, N) zout(X[i].str());
pp ANS(0, mod), P(1, mod);
rep(i, N) {
ANS += P.e * X[i].e;
P *= vx[i].mod;
}
return ANS;
};
}; // namespace zz_modulo
using namespace zz_modulo;
namespace zz_prime {
class prime {
public:
static vl sieve;
static vl p;
static void set(ll N = 1000000) {
N = max<ll>(2, N) + 1;
sieve = vl(N, 0);
p.clear();
sieve[0] = 1;
sieve[1] = 1;
cnt(i, 2, N - 1) {
if (sieve[i] == 0) {
p.push_back(i);
ll j = i;
while (j < N) {
if (sieve[j] == 0) sieve[j] = i;
j += i;
}
}
}
};
static bool isprime(ll x) {
if (x <= 1) return false;
if (sieve.empty()) set();
if (x < sieve.size()) return (sieve[x] == x);
each(ite, p) {
if (x < (*ite) * (*ite)) return true;
if ((x % (*ite)) == 0) return false;
}
return true;
};
static vxy factor(ll x) {
x = max<ll>(1, x);
if (sieve.empty()) set();
map<ll, ll> MAP;
each(ite, p) {
if (x < sieve.size()) break;
while ((x % (*ite)) == 0) {
MAP[*ite]++;
x /= *ite;
}
}
if (x < sieve.size()) {
while (x > 1) {
MAP[sieve[x]]++;
x /= sieve[x];
}
} else {
MAP[x]++;
}
vxy fact;
each(ite, MAP) fact.push_back(*ite);
return fact;
};
static vl divisors(const vxy& fact) {
vl ans(1, 1);
function<void(const ll&, const ll&)> Func =
[&fact, &ans, &Func](const ll& I, const ll& Elm) -> void {
if (fact.empty()) return;
ll Add = 1;
if ((I + 1) < fact.size()) {
Func(I + 1, Elm);
} else {
if (Elm != 1) ans.emplace_back(Elm);
}
cnt(i, 1, fact[I].second) {
Add *= fact[I].first;
if ((I + 1) < fact.size()) {
Func(I + 1, Elm * Add);
} else {
ans.emplace_back(Elm * Add);
}
};
};
Func(0, 1);
sort(ans.begin(), ans.end());
return ans;
};
};
vl prime::p;
vl prime::sieve;
} // namespace zz_prime
using namespace zz_prime;
/////////////////////////////////////////////////
namespace lazeSegment {
template <class S, class F>
class lazySeg {
private:
public:
vector<S> A;
vector<F> B;
vxy Rng;
public:
const ll N;
const ll Size;
lazySeg(ll n = 300000)
: N([&n] {
ll x = 1;
while (x < n) x *= 2;
return x;
}()),
Size(2 * N - 1) {
A = vector<S>(Size, S::e());
B = vector<F>(Size, F::id());
Rng = vxy(Size);
Rng[0] = make_pair(0, N);
cnt(i, 1, Size - 1) {
if (i % 2 == 1) {
Rng[i] =
make_pair(Rng[(i - 1) / 2].first,
(Rng[(i - 1) / 2].first + Rng[(i - 1) / 2].second) / 2);
} else {
Rng[i] =
make_pair((Rng[(i - 1) / 2].first + Rng[(i - 1) / 2].second) / 2,
Rng[(i - 1) / 2].second);
}
}
};
template <typename... Arg>
S s(Arg... arg) {
return S(arg...);
};
template <typename... Arg>
F f(Arg... arg) {
return F(arg...);
};
S get(ll L, ll R, bool dbg = false) {
S s = S::e();
if (R <= L) return s;
L = max<ll>(0, L);
R = min<ll>(R, N);
if (N <= L) return s;
if (R <= 0) return s;
function<void(const ll&)> Func = [this, &L, &R, &dbg, &s,
&Func](const ll& at) {
if (at < (N - 1)) {
B[at * 2 + 1] = B[at] * B[at * 2 + 1];
B[(at + 1) * 2] = B[at] * B[(at + 1) * 2];
}
A[at] = B[at] * A[at];
B[at] = F::id();
if ((R <= Rng[at].first) || (Rng[at].second <= L)) {
if (dbg) cout << " 1 return" << endl;
return;
}
if ((L <= Rng[at].first) && (Rng[at].second <= R)) {
if (dbg) cout << " 2 in" << endl;
s = A[at] * s;
} else {
if (dbg) cout << " 3 cross" << endl;
Func((at + 1) * 2);
Func(at * 2 + 1);
A[at] = A[at * 2 + 1] * A[(at + 1) * 2];
}
};
Func(0);
return s;
};
void set(ll L, ll R, F Act, bool dbg = false) {
if (dbg)
cout << "set(" << L << " " << R << " " << Act.str() << " " << dbg << ")"
<< endl;
if (R <= L) return;
L = max<ll>(0, L);
R = min<ll>(R, N);
if (N <= L) return;
if (R <= 0) return;
function<void(const ll&)> Func = [this, &L, &R, &Act, &dbg,
&Func](const ll& at) {
if ((L <= Rng[at].first) && (Rng[at].second <= R)) {
B[at] = Act * B[at];
}
if (at < (N - 1)) {
B[at * 2 + 1] = B[at] * B[at * 2 + 1];
B[(at + 1) * 2] = B[at] * B[(at + 1) * 2];
}
A[at] = B[at] * A[at];
B[at] = F::id();
if (dbg)
cout << "@=" << at << " [" << L << "," << R << ") Rng=["
<< Rng[at].first << "," << Rng[at].second << ") S=" << A[at].str()
<< " F=" << B[at].str() << endl;
if ((R <= Rng[at].first) || (Rng[at].second <= L)) {
if (dbg) cout << " 1 return" << endl;
return;
}
if ((L <= Rng[at].first) && (Rng[at].second <= R)) {
if (dbg) cout << " 2 in" << endl;
} else {
if (dbg) cout << " 3 cross" << endl;
Func((at + 1) * 2);
Func(at * 2 + 1);
A[at] = A[at * 2 + 1] * A[(at + 1) * 2];
}
};
Func(0);
};
S operator[](const ll& at) { return get(at, at + 1); };
string str(const ll& at, const ll rngShow = true, const ll sShow = true,
const ll fShow = true, const ll length = 0) {
// S s = get(at, s + 1);
string T(1, '_');
if (rngShow)
T += "[" + to_string(Rng[at].first) + "," + to_string(Rng[at].second) +
")";
if (sShow) {
if (*T.rbegin() != '_') T.push_back(' ');
T += A[at].str();
}
if (fShow) {
if (*T.rbegin() != '_') T.push_back(' ');
T += B[at].str();
}
T.push_back('_');
if (length > 0) {
while (T.size() < length) T.push_back('_');
}
return T;
}
void show(const bool Hier = false, const ll rngShow = true,
const ll sShow = true, const ll fShow = true, const ll length = 0) {
if (Hier) {
rep(i, Size) {
cout << str(i, rngShow, sShow, fShow, length) << flush;
if (Rng[i].second == N) cout << endl;
}
} else {
rep(i, Size) cout << str(i, rngShow, sShow, fShow, length) << endl;
}
}
};
class S_sum {
public:
ll e_sum;
ll len;
S_sum(const ll& x, const ll& y) : e_sum(x), len(y) {};
static S_sum e() { return S_sum(0, 1); };
friend S_sum operator*(const S_sum& l, const S_sum& r) {
return S_sum(l.e_sum + r.e_sum, l.len + r.len);
}
string str() { return "<" + to_string(e_sum) + "," + to_string(len) + ">"; };
friend string str(S_sum x) { return x.str(); }
};
class F_sum {
public:
ll a;
ll b;
F_sum(const ll& x, const ll& y) : a(x), b(y) {};
static F_sum id() { return F_sum(1, 0); };
friend F_sum operator*(const F_sum& l, const F_sum& r) {
return F_sum(l.a * r.a, l.a * r.b + l.b);
};
friend S_sum operator*(const F_sum& l, const S_sum& r) {
return S_sum(l.a * r.e_sum + l.b * r.len, r.len);
};
string str() { return ("<" + to_string(a) + "," + to_string(b) + ">"); };
friend string str(F_sum x) { return x.str(); }
};
class S_sum998244353 {
public:
ll e_sum;
ll len;
S_sum998244353(const ll& x, const ll& y) : e_sum(x), len(y) {};
static S_sum998244353 e() { return S_sum998244353(0, 1); };
friend S_sum998244353 operator*(const S_sum998244353& l,
const S_sum998244353& r) {
return S_sum998244353((l.e_sum + r.e_sum) % 998244353,
(l.len + r.len) % 998244353);
}
string str() { return "<" + to_string(e_sum) + "," + to_string(len) + ">"; };
friend string str(S_sum998244353 x) { return x.str(); }
};
class F_sum998244353 {
public:
ll a;
ll b;
F_sum998244353(const ll& x, const ll& y) : a(x), b(y) {};
static F_sum998244353 id() { return F_sum998244353(1, 0); };
friend F_sum998244353 operator*(const F_sum998244353& l,
const F_sum998244353& r) {
return F_sum998244353((l.a * r.a) % 998244353,
(((l.a * r.b) % 998244353) + l.b) % 998244353);
};
friend S_sum998244353 operator*(const F_sum998244353& l,
const S_sum998244353& r) {
return S_sum998244353(
(((l.a * r.e_sum) % 998244353) + ((l.b * r.len) % 998244353)) %
998244353,
r.len);
};
string str() { return ("<" + to_string(a) + "," + to_string(b) + ">"); };
friend string str(F_sum998244353 x) { return x.str(); }
};
class S_func998244353 {
public:
ll a;
ll b;
S_func998244353(const ll& x, const ll& y) : a(x), b(y) {};
static S_func998244353 e() { return S_func998244353(1, 0); };
friend S_func998244353 operator*(const S_func998244353& l,
const S_func998244353& r) {
return S_func998244353((r.a * l.a) % 998244353,
(((r.a * l.b) % 998244353) + r.b) % 998244353);
}
string str() { return "<" + to_string(a) + "," + to_string(b) + ">"; };
friend string str(S_func998244353 x) { return x.str(); }
};
struct F_func998244353 {
public:
ll a;
ll b;
ll c;
ll d;
F_func998244353(const ll& x, const ll& y, const ll& z, const ll& w)
: a(x), b(y), c(z), d(w) {};
static F_func998244353 id() { return F_func998244353(1, 0, 1, 0); };
friend F_func998244353 operator*(const F_func998244353& l,
const F_func998244353& r) {
return F_func998244353(
(r.a * l.a) % 998244353, (((r.a * l.b) % 998244353) + r.b) % 998244353,
(r.c * l.c) % 998244353, (((r.c * l.d) % 998244353) + r.d) % 998244353);
};
friend S_func998244353 operator*(const F_func998244353& l,
const S_func998244353& r) {
return S_func998244353((((l.a * r.a) % 998244353) + l.b) % 998244353,
(((l.c * r.b) % 998244353) + l.d) % 998244353);
};
string str() {
return ("<" + to_string(a) + "," + to_string(b) + "," + to_string(c) + "," +
to_string(d) + ">");
};
friend string str(F_func998244353 x) { return x.str(); }
};
class S_bitflp {
public:
ll zero;
ll one;
ll zo;
ll oz;
S_bitflp(const ll& x, const ll& y, const ll& z, const ll& w)
: zero(x), one(y), zo(z), oz(w) {};
static S_bitflp e() { return S_bitflp(0, 0, 0, 0); };
friend S_bitflp operator*(const S_bitflp& l, const S_bitflp& r) {
return S_bitflp(l.zero + r.zero, l.one + r.one,
l.zo + r.zo + l.zero * r.one, l.oz + r.oz + l.one * r.zero);
}
string str() {
return "<" + to_string(zero) + "," + to_string(one) + "," + to_string(zo) +
"," + to_string(oz) + ">";
};
friend string str(S_bitflp x) { return x.str(); }
};
class F_bitflp {
public:
ll flp;
ll set_a;
ll set_b;
F_bitflp(const ll& x, const ll& y, const ll& z)
: flp(x), set_a(y), set_b(z) {};
static F_bitflp id() { return F_bitflp(0, 1, 0); };
friend F_bitflp operator*(const F_bitflp& l, const F_bitflp& r) {
return F_bitflp((l.flp + r.flp) % 2, l.set_a, l.set_b);
};
friend S_bitflp operator*(const F_bitflp& l, const S_bitflp& r) {
if (l.flp == 0) {
if (l.set_a != 1) {
return S_bitflp(l.set_b == 0, l.set_b == 1, 0, 0);
}
return r;
}
return S_bitflp(r.one, r.zero, r.oz, r.zo);
};
string str() { return ("<" + to_string(flp) + ">"); };
friend string str(F_bitflp x) { return x.str(); }
};
using sum = lazySeg<lazeSegment::S_sum, lazeSegment::F_sum>;
using sum998244353 =
lazySeg<lazeSegment::S_sum998244353, lazeSegment::F_sum998244353>;
using func998244353 =
lazySeg<lazeSegment::S_func998244353, lazeSegment::F_func998244353>;
using bitflip =
lazeSegment::lazySeg<lazeSegment::S_bitflp, lazeSegment::F_bitflp>;
}; // namespace lazeSegment
/////////////////////////////////////////////////
int main() {
ll N;
cin >> N;
vl A(N);
rep(i, N) cin >> A[i];
ll Q;
cin >> Q;
vxy Query(Q);
rep(i, Q) {
cin >> Query[i].first;
if (Query[i].first != 2) cin >> Query[i].second;
}
// return 0;
vvl PrevVec(N + Q);
ll I = 0;
list<ll> Vec;
rep(i, Q) {
if (Query[i].first == 1 && Query[i].second != 0) {
PrevVec[Query[i].second - 1].push_back(N + I);
I++;
}
if (Query[i].first == 1 && Query[i].second == 0) {
Vec.push_back(N + I);
I++;
}
}
// zout<0>(N, A);
function<void(const ll&)> Func = [&PrevVec, &Vec,
&Func](const ll& AT) -> void {
// zout("Func(", AT, ")");
Vec.push_front(AT);
reach(ite, PrevVec[AT]) { Func(*ite); }
// each(ite, Vec) cout << *ite << " ";
// cout << endl;
};
rep(q, N) {
ll i = N - 1 - q;
Func(A[i] - 1);
}
// zout("Vec=", Vec);
// cout << "Vec=";
// each(ite, Vec) cout << *ite << " ";
// cout << endl;
ll M = Vec.size();
lazeSegment::sum SEG(M);
vl Num2AT(M);
vl Data;
each(ite, Vec) {
Num2AT[*ite] = Data.size();
Data.push_back(*ite);
}
I = N;
ll L, R, D, K;
rep(i, N) SEG.set(Num2AT[i], Num2AT[i] + 1, SEG.f(1, 1));
rep(i, Q) {
// zout(i, Query[i]);
// rep(j, M) cout << SEG.get(j, j + 1).e_sum << " ";
// cout << endl;
// zout(Num2AT);
// zout(Data);
switch (Query[i].first) {
case 1:
SEG.set(Num2AT[I], Num2AT[I] + 1, SEG.f(1, 1));
I++;
break;
case 2:
// 0 [L R) 1
if (SEG.get(0, 1).e_sum == 0) {
L = 0;
R = M;
while ((L + 1) < R) {
D = (L + R) / 2;
if (SEG.get(0, D + 1).e_sum == 0) {
L = D;
} else {
R = D;
}
}
SEG.set(L + 1, L + 2, SEG.f(1, -1));
} else {
// SEG.set(0, 1, SEG.f(0, 0));
SEG.set(0, 1, SEG.f(1, -1));
}
break;
case 3:
// k-1 [L R) k
K = Query[i].second;
if (SEG.get(0, 1).e_sum == K) {
cout << Data[0] + 1 << endl;
} else {
L = 0;
R = M;
while ((L + 1) < R) {
D = (L + R) / 2;
if (SEG.get(0, D + 1).e_sum < K) {
L = D;
} else {
R = D;
}
}
cout << Data[L + 1] + 1 << endl;
}
break;
}
}
return 0;
}