이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n,q;
vector<int> adjlist[N];
int ct = 1;
int p[N][17];
int d[N];
int sz[N], mn[N], l[N], r[N],pos[N];
int fw[N];
vector<int> id;
int root;
void up(int x, int v){
while (x <= N){
fw[x] += v;
x += x&(-x);
}
}
int sum(int x){
int res = 0;
while (x){
res += fw[x];
x -= x&(-x);
}
return res;
}
int sum(int s, int e){
return sum(e)-sum(s-1);
}
void dfs(int u){
sz[u] = 1;
mn[u] = u;
for (auto v : adjlist[u]){
d[v] = d[u]+1;
for (int k = 1; k < 17; k++){
p[v][k] = p[p[v][k-1]][k-1];
if (p[v][k] == 0) break;
}
dfs(v);
sz[u] += sz[v];
mn[u] = min(mn[u],mn[v]);
}
return;
}
void visit(int u){
l[u] = ct;
for (auto v : adjlist[u]){
if (mn[v] == mn[u]) visit(v);
}
for (auto v : adjlist[u]){
if (mn[v] != mn[u]) visit(v);
}
id.push_back(u);
r[u] = ct++;
}
bool isfull(int x) {
//printf("check %d, %d %d ,%d = %d?\n",x,l[x],r[x],sum(l[x],r[x]),sz[x]);
return sum(l[x],r[x]) == sz[x];
}
int add(){
int lo = 1, hi = n;
while (lo + 1 < hi){
int mid = (lo+hi)/2;
if (sum(mid) != mid) hi = mid;
else lo = mid;
}
if (lo + 1 == hi){
if (sum(lo) != lo) hi = lo;
else lo = hi;
}
up(lo,1);
/*for (int i = 1; i<= n; i++){
printf("%d ",sum(i,i));
}
printf("\n");*/
return id[lo];
}
int rem(int x){
int ans = 0;
for (int k = 16; k >= 0; k--){
if (p[x][k] && isfull(p[x][k])){
x = p[x][k];
ans ^= (1<<k);
}
}
//printf("top most %d\n",x);
up(pos[x],-1);
/*for (int i = 1; i<= n; i++){
printf("%d ",sum(i,i));
}
printf("\n");*/
return ans;
}
int main(){
scanf("%d%d",&n,&q);
for (int i = 1; i <= n; i++){
scanf("%d",&p[i][0]);
if (p[i][0] == 0) root = i;
adjlist[p[i][0]].push_back(i);
}
id.push_back(0);
dfs(root);
visit(root);
for (int i = 1; i <= n; i++) {
//printf("%d ",i);
pos[id[i]] = i;
}
for (int i = 0; i < q; i++){
int t,x;
scanf("%d%d",&t,&x);
if (t == 1){
int last;
for (int i = 0; i < x; i++){
last = add();
//printf("added %d %d\n",i,last);
}
printf("%d\n",last);
}
else {
printf("%d\n",rem(x));
}
}
}
컴파일 시 표준 에러 (stderr) 메시지
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:95:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&q);
~~~~~^~~~~~~~~~~~~~
ballmachine.cpp:97:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&p[i][0]);
~~~~~^~~~~~~~~~~~~~~
ballmachine.cpp:110:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&t,&x);
~~~~~^~~~~~~~~~~~~~
# | 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... |