제출 #1328342

#제출 시각아이디문제언어결과실행 시간메모리
1328342vahagngBall Machine (BOI13_ballmachine)C++20
100 / 100
150 ms47608 KiB
//----------vahagng----------//
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace __gnu_pbds;

// template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif

void print(long long t) { cerr << t; }
void print(int t) { cerr << t; }
void print(string t) { cerr << t; }
void print(char t) { cerr << t; }
void print(double t) { cerr << t; }
void print(long double t) { cerr << t; }
void print(unsigned long long t) { cerr << t; }

template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[], V n) { cerr << "["; for (int i = 0; i < n; i++) { cerr << v[i] << " "; } cerr << "]"; }
template <class T, class V> void print(pair <T, V> p) { cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}"; }
template <class T> void print(vector <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T> void print(set <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T> void print(unordered_set <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T> void print(multiset <T> v) { cerr << "[ "; for (T i : v) { print(i); cerr << " "; } cerr << "]"; }
template <class T, class V> void print(map <T, V> v) { cerr << "[ "; for (auto i : v) { print(i); cerr << " "; } cerr << "]"; }

#define ll long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define ld long double
#define sz(v) v.size()
#define endl '\n'

const ll inf = 2e18, mod = 1e9 + 7, mod2 = 998244353;

void SetIO(string str = "") {
    if (str != "") {
        freopen((str + ".in").c_str(), "r", stdin);
        freopen((str + ".out").c_str(), "w", stdout);
    }
    else {
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    }
}

void FastIO() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

const int N = 3e5 + 10, M = 2e7 + 10;

mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());

ll binpow(ll a, ll b, ll m) {
    ll res = 1;
    while (b) {
        if (b & 1) {
            res = (res * a) % m;
        }
        b >>= 1;
        a = (a * a) % m;
    }
    return res;
}

ll n, q, mn[N], dp[N], up[N][20], ind[N];
vector<int>adj[N], ord[N], tout;
bool ka[N];

void dfs(int node, int parent) {
    mn[node] = node;
    for (auto i : adj[node]) {
        if (i == parent) continue;
        dfs(i, node);
        mn[node] = min(mn[i], mn[node]);
    }
    vector<pair<int, int>> cur;
    for(auto i : adj[node]){
        if (i == parent) continue;
        cur.push_back({ mn[i], i });
    }
    sort(all(cur));
    for (auto i : cur) {
        ord[node].push_back(i.second);
    }
}

void dfs0(int node, int parent) {
    dp[node] = dp[parent] + 1;
    up[node][0] = parent;
    for(int i = 1; i < 20; i++){
        up[node][i] = up[up[node][i - 1]][i - 1];
    }
    for (auto i : ord[node]) {
        if (i == parent) continue;
        dfs0(i, node);
    }
    tout.push_back(node);
}

void solve(int tc) {
    cin >> n >> q;
    int r = 0;
    for (int i = 1; i <= n; i++) {
        int p;
        cin >> p;
        if(!p){
            r = i;
        }
        else {
            adj[p].push_back(i);
            adj[i].push_back(p);
        }
    }
    dfs(r, 0);
    dfs0(r, 0);
    set<pair<int, int>> st1, st2;
    for (int i = 1; i <= n; i++) {
        ind[tout[i - 1]] = i;
        st1.insert({ i, tout[i - 1] });
    }
    // dbg(st1);
    while (q--) {
        int type, k;
        cin >> type >> k;
        if (type == 1) {
            int ans = -1;
            while (k--) {
                auto [id, node] = *st1.begin();
                ka[node] = 1;
                ans = node;
                st2.insert({ id, node });
                st1.erase(st1.begin());
            }
            cout << ans << endl;
        }
        else {
            int u = k;
            for (int i = 19; i >= 0; i--) {
                if (ka[up[u][i]]) {
                    u = up[u][i];
                }
            }
            cout << dp[k] - dp[u] << endl;
            ka[u] = 0;
            st1.insert({ ind[u], u });
            st2.erase({ ind[u], u });
        }
    }
}


void precalc() {

}

int main() {
    // SetIO("teamwork");
    FastIO();
    int test_case = 1;
    // cin >> test_case;
    precalc();
    int cnt = 1;
    while (test_case--) {
        solve(cnt++);
    }
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

ballmachine.cpp: In function 'void SetIO(std::string)':
ballmachine.cpp:48:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         freopen((str + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:49:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |         freopen((str + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:52:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |         freopen("input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:53:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |         freopen("output.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...