이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const bool debug = 0;
int n, q, p[N][20], op, x, fen[N], L, pos[N], mi[N];
vector<int> adj[N], ord;
bool mark[N];
void add(int i, int val) {
for (; i<=n; i+=i&(-i)) fen[i] += val;
return;
}
int sum(int i) {
int ret = 0;
for (; i>0; i-=i&(-i)) ret += fen[i];
return ret;
}
int find_first(int l, int r) {
if (l == r) return l;
int m = (l+r)/2;
if (sum(m) == 0) return find_first(m+1, r);
else return find_first(l, m);
}
void find_min(int v) {
for (int i=1; i<=L; i++) p[v][i] = p[p[v][i-1]][i-1];
mi[v] = v;
for (auto to : adj[v]) {
find_min(to);
mi[v] = min(mi[v], mi[to]);
}
return;
}
void find_ord(int v) {
for (auto to : adj[v]) {
find_ord(to);
}
ord.push_back(v);
return;
}
pair<int, int> find_lift(int v) {
int ti = 0;
for (int i=L; i>=0; i--) {
if (!mark[p[v][i]]) v = p[v][i], ti += pow(2, i);
}
return make_pair(v, ti);
}
bool cmp(int i, int j) {
return mi[i] < mi[j];
}
int main() {
scanf(" %d %d", &n, &q);
L = ceil(log2(n + 1));
for (int i=1; i<=n; i++) {
scanf(" %d", &p[i][0]);
adj[p[i][0]].push_back(i);
}
find_min(adj[0][0]);
for (int i=1; i<=n; i++) sort(adj[i].begin(), adj[i].end(), cmp);
ord.push_back(0);
find_ord(adj[0][0]);
for (int i=1; i<=n; i++) pos[ord[i]] = i;
if (debug) {
printf("ord : ");
for (auto it : ord) printf("%d ", it);
printf("\npos : ");
for (int i=0; i<=n; i++) printf("%d ", pos[i]);
printf("\n");
for (int i=1; i<=n; i++) {
printf("lift %d : ", i);
for (int j=0; j<=L; j++) printf("%d ", p[i][j]);
printf("\n");
}
}
mark[0] = true;
for (int i=1; i<=n; i++) add(i, 1), mark[i] = true;
while (q--) {
scanf(" %d %d", &op, &x);
if (op == 1) {
int idx;
for (int i=1; i<=x; i++) {
idx = find_first(1, n);
add(idx, -1);
mark[ord[idx]] = false;
}
printf("%d\n", ord[idx]);
}
else {
pair<int, int> out = find_lift(x);
add(pos[out.first], 1);
mark[out.first] = true;
printf("%d\n", out.second);
}
if (debug) {
printf("sum : ");
for (int i=1; i<=n; i++) printf("%d ", sum(i));
printf("\n");
}
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf(" %d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~
ballmachine.cpp:62:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf(" %d", &p[i][0]);
| ~~~~~^~~~~~~~~~~~~~~~~
ballmachine.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | scanf(" %d %d", &op, &x);
| ~~~~~^~~~~~~~~~~~~~~~~~~
ballmachine.cpp:97:35: warning: 'idx' may be used uninitialized in this function [-Wmaybe-uninitialized]
97 | printf("%d\n", ord[idx]);
| ^
# | 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... |