| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 667224 | joaof | Ball Machine (BOI13_ballmachine) | C++17 | 235 ms | 41720 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define bug(x) cerr << #x << ": " << x << endl
#define pb push_back
using namespace std;
using ll = long long;
const int MAX = 1e5 + 10;
int prof[MAX], bl[MAX][50], sub[MAX], minSub[MAX], pai[MAX], mark[MAX], pot[50], id[MAX];
vector<int> g[MAX], ord;
set<pair<int, int>> s;
int inserir(int x){
//bug(x);
while(x--){
if(x == 0){
mark[(*s.begin()).ss] = 1;
int ans = (*s.begin()).ss;
s.erase((*s.begin()));
return ans;
}
mark[(*s.begin()).ss] = 1;
s.erase((*s.begin()));
}
return 4;
}
int remover(int x){
int ans = 0;
for(int i = 20; i >= 0; i--){
if(bl[x][i] == 0 || mark[bl[x][i]] == 0) continue;
ans += pot[i];
// bug(pot[i]);
x = bl[x][i];
// bug(x);
}
mark[x] = 0;
s.insert({id[x], x});
return ans;
}
bool cmp(int a, int b){
return minSub[a] < minSub[b];
}
void calcLift(int x){
bl[x][0] = pai[x];
int t = 0;
while(bl[x][t] != 0){
t++;
bl[x][t] = bl[bl[x][t - 1]][t - 1];
}
}
void dfs(int x, int type){
if(type == 1){
sub[x] = 1;
minSub[x] = x;
for(auto v : g[x]){
prof[v] = prof[x] + 1;
calcLift(v);
dfs(v, 1);
minSub[x] = min(minSub[x], minSub[v]);
sub[x] += sub[v];
}
sort(all(g[x]), cmp);
}
if(type == 2){
for(auto v : g[x]){
dfs(v, 2);
}
ord.pb(x);
id[x] = ord.size() - 1;
}
}
int main(){
pot[0] = 1;
for(int i = 1; i <= 25; i++){
pot[i] = 2 * pot[i - 1];
}
int n, q, root;
scanf("%d %d", &n, &q);
for(int i = 1; i <= n; i++){
int a;
scanf("%d", &a);
if(a == 0){
root = i;
continue;
}
pai[i] = a;
g[a].pb(i);
}
dfs(root, 1);
dfs(root, 2);
for(int i = 0; i < ord.size(); i++){
s.insert({i, ord[i]});
// bug(ord[i]);
}
for(int i = 1; i <= q; i++){
int op, a;
scanf("%d %d", &op, &a);
if(op == 1){
printf("%d\n", inserir(a));
}
if(op == 2){
printf("%d\n", remover(a));
}
}
}
컴파일 시 표준 에러 (stderr) 메시지
| # | 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... | ||||
