제출 #411021

#제출 시각아이디문제언어결과실행 시간메모리
411021LptN21Ball Machine (BOI13_ballmachine)C++14
79.89 / 100
270 ms23108 KiB
#include <bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define FF first
#define SS second
#define pb push_back
#define sz(x) (int)x.size()
#define oo 1e9
#define PI acos(-1.0)
#define lb lower_bound
#define ub upper_bound
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
const int N = 1e5+7, M=18;
const int MOD = 1e9+7;

int n, m, k, t;
int P[N][M], fen[N], h[N];
vector<int> adj[N];
int pos[N], idx[N];
int _dp[N], cnt=0;

bool cmp(const int &a, const int &b) {return _dp[a]<_dp[b];}
void dfs(int u) {
    _dp[u]=u;
    for(int v, i=0;i<sz(adj[u]);i++) {
        v=adj[u][i], h[v]=h[u]+1, P[v][0]=u;
        dfs(v), _dp[u]=min(_dp[u], _dp[v]);
    }
}
void dfs2(int u) {
    for(int i=0;i<sz(adj[u]);i++) dfs2(adj[u][i]);
    pos[u]=++cnt, idx[cnt]=u;
}

void process(int s) {
    h[s]=-1, dfs(s);
    for(int i=1;i<=n;i++) sort(adj[i].begin(), adj[i].end(), cmp);
    dfs2(s);
    for(int j=1;(1<<j)<=n;j++) for(int i=1;i<=n;i++)
        P[i][j]=P[P[i][j-1]][j-1];
}
void upd(int i, int v) {for(;i<=n;i+=i&-i) fen[i]+=v;}
int get(int i) {int res=0;for(;i>0;i-=i&-i) res+=fen[i];return res;}

signed main() {
    //freopen("test.inp", "r", stdin);
    //freopen("test.out", "w", stdout);
    //fastIO;
    scanf("%d%d", &n, &m);int u;
    for(int i=1;i<=n;adj[u].pb(i++)) scanf("%d", &u);
    process(0);
    for(int ans=0, i=1;i<=m;ans=0, i++) {
        scanf("%d%d", &t, &k);
        if(t&1) {
            for(int j=1;j<=k;j++) {
                int l=1, mid, r=n;ans=n;
                while(l<=r) {
                    mid=(l+r)/2;
                    if(get(mid)==mid) l=mid+1;
                    else ans=mid, r=mid-1;
                }
                upd(ans, 1), ans=idx[ans];
            }
        } else {
            for(int tmp, i=log2(h[k]);i>=0;i--) {
                tmp=get(pos[P[k][i]])-get(pos[P[k][i]]-1);
                if(tmp) k=P[k][i], ans+=(1<<i);
            }
            upd(pos[k], -1);
        }
        printf("%d\n", ans);
    }
    return 0;
}
/* stuff you should look for
    - int overflow, array bounds
    - special cases (n=1?)
    - do smth instead of do nothing and stay organized
    - WRITE STUFF DOWN
    - DONT JUST STICK ON ONE APPROACH
*/

컴파일 시 표준 에러 (stderr) 메시지

ballmachine.cpp: In function 'int main()':
ballmachine.cpp:51:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |     scanf("%d%d", &n, &m);int u;
      |     ~~~~~^~~~~~~~~~~~~~~~
ballmachine.cpp:52:43: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |     for(int i=1;i<=n;adj[u].pb(i++)) scanf("%d", &u);
      |                                      ~~~~~^~~~~~~~~~
ballmachine.cpp:55:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |         scanf("%d%d", &t, &k);
      |         ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...