Submission #121475

#TimeUsernameProblemLanguageResultExecution timeMemory
121475TadijaSebezRegions (IOI09_regions)C++11
90 / 100
8074 ms80260 KiB
#include <stdio.h> #include <vector> #include <algorithm> using namespace std; #define pb push_back #define mp make_pair #define ll long int const int N=200050; const int S=190; const int H=1060; const int M=25050; int sol[H][M]; int hid[M],reg[N],tsz; vector<int> E[N],My[M],ids[M]; int lid[N],rid[N],tid; void DFS(int u) { lid[u]=++tid; ids[reg[u]].pb(tid); for(int i=0;i<E[u].size();i++) DFS(E[u][i]); rid[u]=tid; } void Heavy(int u, int r, int c) { if(reg[u]==r) c++; else sol[hid[r]][reg[u]]+=c; for(int i=0;i<E[u].size();i++) Heavy(E[u][i],r,c); } int Get(int l, int r, int ty) { return upper_bound(ids[ty].begin(),ids[ty].end(),r)-lower_bound(ids[ty].begin(),ids[ty].end(),l); } int par[N]; int main() { int n,m,q,i; scanf("%i %i %i",&n,&m,&q); scanf("%i",&reg[1]); for(i=2;i<=n;i++) scanf("%i %i",&par[i],&reg[i]),E[par[i]].pb(i); for(i=1;i<=n;i++) My[reg[i]].pb(i); DFS(1); for(i=1;i<=m;i++) if(My[i].size()>=S) hid[i]=++tsz,Heavy(1,i,0); int x,y; while(q--) { scanf("%i %i",&x,&y); if(My[x].size()>=S) printf("%i\n",sol[hid[x]][y]); else { int ans=0; for(i=0;i<My[x].size();i++) { ans+=Get(lid[My[x][i]],rid[My[x][i]],y); } printf("%i\n",ans); } fflush(stdout); } return 0; }

Compilation message (stderr)

regions.cpp: In function 'void DFS(int)':
regions.cpp:20:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<E[u].size();i++) DFS(E[u][i]);
              ~^~~~~~~~~~~~
regions.cpp: In function 'void Heavy(int, int, int)':
regions.cpp:27:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<E[u].size();i++) Heavy(E[u][i],r,c);
              ~^~~~~~~~~~~~
regions.cpp: In function 'int main()':
regions.cpp:51:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(i=0;i<My[x].size();i++)
            ~^~~~~~~~~~~~~
regions.cpp:37:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i %i %i",&n,&m,&q);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~
regions.cpp:38:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i",&reg[1]);
  ~~~~~^~~~~~~~~~~~~~
regions.cpp:39:50: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=2;i<=n;i++) scanf("%i %i",&par[i],&reg[i]),E[par[i]].pb(i);
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
regions.cpp:46:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%i %i",&x,&y);
   ~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...