Submission #89624

#TimeUsernameProblemLanguageResultExecution timeMemory
89624VardanyanBirthday gift (IZhO18_treearray)C++14
56 / 100
4011 ms45460 KiB
#include <bits/stdc++.h> using namespace std; const int N = 2*1000*100+5; int a[N]; vector<int> g[N]; int colour[N]; int par[N]; int dp[N][25]; int depth[N]; void kaxel(int v,int dep = 0,int p = -1){ par[v] = p; dp[v][0] = p; depth[v] = dep; for(int i = 0;i<g[v].size();i++){ int to = g[v][i]; if(to == p) continue; kaxel(to,dep+1,v); } } int lca(int a,int b){ if(depth[a]<depth[b]) swap(a,b); int dif = depth[a]-depth[b]; for(int i = 24;i>=0;i--){ if((dif>>i)&1) a = dp[a][i]; } // cout<<a<<" "<<b<<endl; if(a == b) return a; for(int i = 24;i>=0;i--){ if(dp[a][i] == -1 || dp[b][i] == -1) continue; if(dp[a][i] == dp[b][i]) continue; a = dp[a][i]; b = dp[b][i]; } a = dp[a][0]; return a; } int main(){ int n,m,q; scanf("%d%d%d",&n,&m,&q); for(int i = 1;i<=n-1;i++){ int x,y; scanf("%d%d",&x,&y); g[x].push_back(y); g[y].push_back(x); } memset(dp,-1,sizeof dp); kaxel(1); for(int j = 1;j<=24;j++){ for(int i = 1;i<=n;i++){ if(dp[i][j-1] == -1) dp[i][j] = -1; else dp[i][j] = dp[dp[i][j-1]][j-1]; } } /*int x,y; while(cin>>x>>y){ cout<<lca(x,y)<<endl; }*/ /*for(int i = 1;i<=n;i++){ for(int j = 0;j<=4;j++){ cout<<dp[i][j]<<" "; } cout<<endl; }*/ for(int i = 1;i<=m;i++) scanf("%d",&a[i]); while(q--){ int tp; scanf("%d",&tp); if(tp == 1){ int pos,val; scanf("%d%d",&pos,&val); a[pos] = val; } else{ int l,r,v; scanf("%d%d%d",&l,&r,&v); bool f = false; for(int i = l;i<=r;i++){ if(v == a[i]){ printf("%d %d\n",i,i); f = true; break; } if(i == r) continue; if(lca(a[i],a[i+1]) == v){ printf("%d %d\n",i,i+1); f = true; break; } } if(f) continue; printf("-1 -1\n"); } } return 0; }

Compilation message (stderr)

treearray.cpp: In function 'void kaxel(int, int, int)':
treearray.cpp:14:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0;i<g[v].size();i++){
                   ~^~~~~~~~~~~~
treearray.cpp: In function 'int main()':
treearray.cpp:39:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d",&n,&m,&q);
     ~~~~~^~~~~~~~~~~~~~~~~~~
treearray.cpp:42:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&x,&y);
         ~~~~~^~~~~~~~~~~~~~
treearray.cpp:64:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i = 1;i<=m;i++) scanf("%d",&a[i]);
                             ~~~~~^~~~~~~~~~~~
treearray.cpp:67:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&tp);
         ~~~~~^~~~~~~~~~
treearray.cpp:70:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d%d",&pos,&val);
             ~~~~~^~~~~~~~~~~~~~~~~~
treearray.cpp:75:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d%d%d",&l,&r,&v);
             ~~~~~^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...