Submission #667238

#TimeUsernameProblemLanguageResultExecution timeMemory
667238AmirHBall Machine (BOI13_ballmachine)C++14
100 / 100
562 ms49096 KiB
#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<bitset>
#include<queue>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<unordered_map>
#include<list>
#include<utility>
#include<stack>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
#define cl clear
#define F  first
#define S  second
#define pb push_back
#define Sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define faster ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
const int MAX_N = 2e5 + 25;
const ll INF = 1e18;
const int inf = 1e9;
void free() {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
}
vector<int> vtx[MAX_N];
set<pii> adj[MAX_N];
int mini[MAX_N];
vector<int> res;
int par[MAX_N];
set<int> notseen;
int ar[MAX_N];
int pr[MAX_N];
int height[MAX_N];
int lcm[MAX_N][20];
int listpo[MAX_N];

int get_height(int v, int h) {
    if(h == 0)
        return v;
    int max_z = listpo[h];
    return get_height(lcm[v][max_z], h - (1 << max_z));
}

int get_last_seen(int v) {
    int l = 0, r = height[v] + 1;
    while(r - l > 1) {
        int mid = (l + r) / 2;
        if(notseen.find(ar[get_height(v, mid)]) == notseen.end())
            l = mid;
        else
            r = mid;
    }
    return get_height(v, l);
}

void find_po() {
    int j = 0;
    for(int i  = 1; i <= 1e5; i++) {
        if(i >= (1 << (j + 1))) {
            j++;
            listpo[i] = j;
        }
        else
            listpo[i] = j;
    }
}

void rmq(int v) {
    queue<int> q;
    q.push(v);
    while(!q.empty()) {
        int x = q.front();
        q.pop();
        for(auto i : vtx[x]) {
            if(height[i] > height[x])
                q.push(i);
        }
        for(int i = 0; (1 << i) <= height[x]; i++) {
            if(i == 0)
                lcm[x][i] = par[x];
            else
                lcm[x][i] = lcm[lcm[x][i - 1]][i - 1];
        }
    }
}

void dfs2(int v) {
    for(auto i : adj[v])
        dfs2(i.second);
    res.pb(v);
}
void dfs(int v, int s) {
    mini[v] = v;
    for(auto i : vtx[v]) {
        if(i != s) {
            height[i] = height[v] + 1;
            dfs(i, v);
            mini[v] = min(mini[v], mini[i]);
            adj[v].insert({mini[i], i});
        }
    }
}
int main() {
    faster; //free();
    find_po();
    int n, q; cin >> n >> q;
    int st;
    for(int i = 1; i <= n; i++) {
        int x; cin >> x;
        par[i] = x;
        if(x == 0) {
            st = i;
        }
        else {
            vtx[x].pb(i);
            vtx[i].pb(x);
        }
    }
    height[st] = 0;
    dfs(st, 0);
    dfs2(st);
    rmq(st);
    for(int i = 0; i < res.size(); i++) {
        ar[res[i]] = i + 1;
        pr[i + 1] = res[i];
    }
    for(int i = 1; i <= n; i++)
        notseen.insert(i);
    while(q--) {
        int t, x; cin >> t >> x;
        if(t == 1) {
            int ft;
            while(x--) {
                ft = pr[*notseen.begin()];
                notseen.erase(notseen.begin());
            }
            cout << ft << '\n';
        }
        if(t == 2) {
            int sec = get_last_seen(x);
            notseen.insert(ar[sec]);
            cout << height[x] - height[sec] << '\n';
        }
    }
}

Compilation message (stderr)

ballmachine.cpp: In function 'int main()':
ballmachine.cpp:134:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  134 |     for(int i = 0; i < res.size(); i++) {
      |                    ~~^~~~~~~~~~~~
ballmachine.cpp: In function 'void free()':
ballmachine.cpp:32:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |         freopen("input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:33:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |         freopen("output.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:148:27: warning: 'ft' may be used uninitialized in this function [-Wmaybe-uninitialized]
  148 |             cout << ft << '\n';
      |                           ^~~~
ballmachine.cpp:133:8: warning: 'st' may be used uninitialized in this function [-Wmaybe-uninitialized]
  133 |     rmq(st);
      |     ~~~^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...