제출 #196883

#제출 시각아이디문제언어결과실행 시간메모리
196883dantoh000Ball Machine (BOI13_ballmachine)C++14
100 / 100
338 ms26160 KiB
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n,q; vector<int> adjlist[N]; int ct = 1; int p[N][20]; int d[N], mn[N], l[N], r[N], pos[N]; int fw[N], id[N]; int root; bool cmp(int x, int y){ return mn[x] < mn[y]; } 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 print(){ for (int i = 1; i <= n; i++) printf("%d ",sum(i,i)); printf("\n");} void dfs(int u){ mn[u] = u; for (auto v : adjlist[u]){ d[v] = d[u]+1; for (int k = 1; k < 20; k++){ p[v][k] = p[p[v][k-1]][k-1]; if (p[v][k] == 0) break; } dfs(v); mn[u] = min(mn[u],mn[v]); } return; } void visit(int u){ l[u] = ct; sort(adjlist[u].begin(),adjlist[u].end(),cmp); for (auto v : adjlist[u]){ visit(v); } id[ct] = u; pos[u] = ct; r[u] = ct++; } int add(){ int lo = 0, hi = n; while (lo + 1 < hi){ int mid = (lo+hi)/2; if (sum(mid) != mid) hi = mid; else lo = mid; } up(lo+1,1); ///print(); return id[lo+1]; } int rem(int x){ int ans = 0; for (int k = 19; k >= 0; k--){ int P = p[x][k]; if (P && sum(l[P],r[P]) == r[P]-l[P]+1){ x = P; ans |= (1<<k); } } up(pos[x],-1); ///print(); 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; else adjlist[p[i][0]].push_back(i); } dfs(root); visit(root); for (int i = 0; i < q; i++){ int t,x; scanf("%d%d",&t,&x); if (t == 1){ int last; while (x--){ 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:80: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:82: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:90: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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...