이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define maxn 100010
#define pii pair<int, int>
int N, Q;
int par[18][maxn];
vector<int> ch[maxn];
vector<int> order;
int rt;
int minsub[maxn];
bool isin[maxn];
int myindo[maxn];
void getorder(int u) {
vector<pii> nch;
for (int v : ch[u]) {
nch.push_back(pii(minsub[v], v));
}
sort(nch.begin(), nch.end());
for (pii vp : nch) {
getorder(vp.second);
}
order.push_back(u);
}
void godown(int u) {
minsub[u] = u;
for (int v : ch[u]) {
godown(v);
minsub[u] = min(minsub[u], minsub[v]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> Q;
for (int i = 1; i <= N; i++) {
cin >> par[0][i];
ch[par[0][i]].push_back(i);
}
rt = ch[0][0];
godown(rt);
getorder(rt);
for (int i = 1; i < 18; i++) {
for (int j = 1; j <= N; j++) {
par[i][j] = par[i-1][par[i-1][j]];
}
}
int tp, x;
set<pii> open;
int ct = 0;
for (int v : order) {
open.insert(pii(ct++, v));
}
for (int i = 0; i < order.size(); i++) {
myindo[order[i]] = i;
}
while (Q--) {
cin >> tp >> x;
if (tp == 1) {
int ans = -1;
while (x--) {
pii cur = *(open.begin());
open.erase(open.begin());
isin[cur.second] = true;
ans = cur.second;
}
cout << ans << '\n';
}
else {
int tempo = x;
int numfall = 0;
for (int i = 17; i >= 0; i--) {
if (isin[par[i][tempo]]) {
numfall += (1 << i);
tempo = par[i][tempo];
}
}
cout << numfall << '\n';
isin[tempo] = false;
open.insert(pii(myindo[tempo], tempo));
}
}
cout.flush();
}
컴파일 시 표준 에러 (stderr) 메시지
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:60:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < order.size(); i++) {
~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |