Submission #155452

#TimeUsernameProblemLanguageResultExecution timeMemory
155452LawlietBall Machine (BOI13_ballmachine)C++14
0 / 100
1094 ms25208 KiB
#include <bits/stdc++.h> #define LOG 20 #define MAX 100010 using namespace std; int n, q; int root; int n1, n2; int curTime; int ord[MAX]; int mnSub[MAX]; int dp[LOG][MAX]; bool hasBall[MAX]; vector<int> grafo[MAX]; set<int> s; struct cmp { bool operator () (int a, int b) { return ord[a] < ord[b]; } }; set<int , cmp> nextNode; void DFSInit(int cur) { mnSub[ cur ] = cur; for(int i = 0 ; i < grafo[ cur ].size() ; i++) DFSInit( grafo[ cur ][ i ] ); sort( grafo[ cur ].begin() , grafo[ cur ].end() , [&] (int a, int b) { return mnSub[ a ] < mnSub[ b ]; }); if( !grafo[cur].empty() ) mnSub[ cur ] = min(cur , mnSub[ grafo[cur][0] ]); } void DFSTour(int cur) { for(int i = 0 ; i < grafo[ cur ].size() ; i++) DFSTour( grafo[cur][i] ); ord[ cur ] = ++curTime; } int add(int k) { int last; while(k-- > 0) { last = *nextNode.begin(); nextNode.erase( nextNode.begin() ); hasBall[ last ] = true; } return last; } int pop(int i) { int d = 0; for(int k = LOG - 1 ; k >= 0 ; k--) { int jump = dp[ k ][ i ]; if(hasBall[ jump ]) { i = jump; d += (1 << k); } } nextNode.insert( i ); hasBall[ i ] = false; return d; } int main() { scanf("%d %d",&n,&q); for(int i = 1 ; i <= n ; i++) { scanf("%d",&dp[0][i]); if(dp[0][i] == 0) root = i; grafo[ dp[0][i] ].push_back( i ); } for(int k = 1 ; k < LOG ; k++) { for(int i = 1 ; i <= n ; i++) { int jump = dp[k - 1][ i ]; dp[k][ i ] = dp[k - 1][ jump ]; } } DFSInit( 1 ); DFSTour( 1 ); for(int i = 1 ; i <= n ; i++) nextNode.insert( i ); for(int i = 1 ; i <= q ; i++) { scanf("%d %d",&n1,&n2); if(n1 == 1) printf("%d\n",add( n2 )); if(n1 == 2) printf("%d\n",pop( n2 )); } }

Compilation message (stderr)

ballmachine.cpp: In function 'void DFSInit(int)':
ballmachine.cpp:34:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0 ; i < grafo[ cur ].size() ; i++)
                  ~~^~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp: In function 'void DFSTour(int)':
ballmachine.cpp:48:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0 ; i < grafo[ cur ].size() ; i++)
                  ~~^~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:92:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&q);
  ~~~~~^~~~~~~~~~~~~~~
ballmachine.cpp:96:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&dp[0][i]);
   ~~~~~^~~~~~~~~~~~~~~~
ballmachine.cpp:121:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&n1,&n2);
   ~~~~~^~~~~~~~~~~~~~~~~
ballmachine.cpp: In function 'int add(int)':
ballmachine.cpp:66:9: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
  return last;
         ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...