Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- // tiom4eg's precompiler options
- // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
- // IO settings
- #define fastIO ios_base::sync_with_stdio(false); cin.tie(0)
- // Quick types
- #define ll long long
- #define ld long double
- #define ull unsigned long long
- #define pii pair <int, int>
- #define vi vector <int>
- #define mi vector <vector <int>>
- // Quick functions
- #define endl "\n"
- #define F first
- #define S second
- #define all(a) a.begin(), a.end()
- #define sz(a) (int)(a.size())
- #define pb push_back
- #define mp make_pair
- // Quick fors
- #define FOR(i, a, b) for (int i = a; i < b; ++i)
- #define FORS(i, a, b, c) for (int i = a; i < b; i += c)
- #define RFOR(i, a, b) for (int i = a; i >= b; --i)
- #define EACH(e, a) for (auto& e : a)
- // Pragmas
- #ifndef TIOM4EG
- #pragma GCC optimize("O2") // let the chaos begin!
- //#pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt,tune=native")
- //#pragma GCC comment(linker, "/stack:200000000")
- #endif
- // PBDS
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #define ordered_set tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
- #define ook order_of_key
- #define fbo find_by_order
- using namespace __gnu_pbds;
- // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
- using namespace std;
- mt19937 rng(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
- //#define int long long
- const int INF = 1e9 + 7, MD = 998244353, MAX = 300005, R = 1 << 19, MOD1 = 1e9 + 7, MOD2 = 1e9 + 9, MOD12 = MOD1 * MOD1, MOD22 = MOD2 * MOD2, LG = 20, B1 = 103, B2 = 107, S = 10008;
- const ll INFLL = 1e18 + 7;
- int nx[MAX][LG], lv[MAX][LG];
- vector <pii> cur, o[LG];
- signed main() {
- fastIO;
- int n, m, k, b = 0, g = 0, r = 0; cin >> n >> m >> k, --k;
- cur.reserve(n);
- pair <int, char> t[n];
- FOR(i, 0, n) {
- cin >> t[i].F >> t[i].S, --t[i].F;
- if (t[i].S == 'B') ++b;
- else if (t[i].S == 'G') ++g;
- else ++r;
- }
- FOR(i, 0, n) nx[i][0] = t[i].F;
- FOR(i, 1, LG) FOR(j, 0, n) nx[j][i] = nx[nx[j][i - 1]][i - 1];
- FOR(i, 0, n) {
- if (t[i].S == 'B') lv[i][0] = 0;
- else if (t[i].S == 'G') lv[i][0] = b;
- else lv[i][0] = b + g;
- o[0].pb({lv[i][0], i});
- }
- sort(all(o[0]));
- FOR(i, 1, LG) {
- int p = 0;
- FOR(j, 1, n) if (o[i - 1][p].F < o[i - 1][j].F) {
- FOR(k, p, j) cur.pb({lv[nx[o[i - 1][k].S][i - 1]][i - 1], o[i - 1][k].S});
- sort(all(cur));
- lv[cur[0].S][i] = p, o[i].pb({p, cur[0].S});
- FOR(k, 1, j - p) lv[cur[k].S][i] = (cur[k].F == cur[k - 1].F ? lv[cur[k - 1].S][i] : p + k), o[i].pb({lv[cur[k].S][i], cur[k].S});
- cur.clear(), p = j;
- }
- vector <pii> cur;
- FOR(k, p, n) cur.pb({lv[nx[o[i - 1][k].S][i - 1]][i - 1], o[i - 1][k].S});
- sort(all(cur));
- lv[cur[0].S][i] = p, o[i].pb({p, cur[0].S});
- FOR(k, 1, n - p) lv[cur[k].S][i] = (cur[k].F == cur[k - 1].F ? lv[cur[k - 1].S][i] : p + k), o[i].pb({lv[cur[k].S][i], cur[k].S});
- cur.clear();
- }
- int st = o[LG - 1][n - 1].S;
- FOR(i, 0, n) if (o[LG - 1][i].F > k) {
- st = o[LG - 1][i - 1].S;
- break;
- }
- FOR(i, 0, m) {
- cout << t[st].S;
- st = t[st].F;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement