Submission #85406

#TimeUsernameProblemLanguageResultExecution timeMemory
85406farukkastamonudaBirthday gift (IZhO18_treearray)C++14
100 / 100
1583 ms176468 KiB
#include <bits/stdc++.h> #define fi first #define se second #define mp make_pair #define lo long long #define inf 1000000000 #define md 1000000007 #define pb push_back #define li 200005 #define mid (start+end)/2 using namespace std; int n,m,q,x,y,dep[li],baba[24][li],A[li],deg; vector<int> v[li]; set<int> s[li],t[li]; void dfs(int node,int ata,int d){ baba[0][node]=ata; dep[node]=d; for(int i=0;i<(int)v[node].size();i++){ int go=v[node][i]; if(go==ata) continue; dfs(go,node,d+1); } } void b_lca(){ for(int i=1;i<=20;i++){ for(int j=1;j<=n;j++){ baba[i][j]=baba[i-1][baba[i-1][j]]; } } } int f_lca(int x,int y){ if(x==0 || y==0) return x+y; if(dep[x]<dep[y]) swap(x,y); int dx=dep[x]; int dy=dep[y]; for(int i=20;i>=0;i--){ if(dx-(1<<i)>=dy){ dx-=(1<<i); x=baba[i][x]; } } if(x==y) return x; for(int i=20;i>=0;i--){ if(baba[i][x]!=baba[i][y]){ x=baba[i][x]; y=baba[i][y]; } } return baba[0][x]; } int main(){ scanf("%d %d %d",&n,&m,&q); for(int i=1;i<n;i++){ scanf("%d %d",&x,&y); v[x].pb(y); v[y].pb(x); s[i].insert(inf); t[i].insert(inf); } t[n].insert(inf); s[n].insert(inf); dfs(1,0,0); b_lca(); for(int i=1;i<=m;i++){ scanf("%d",&A[i]); t[A[i]].insert(i); } for(int i=1;i<m;i++){ s[f_lca(A[i],A[i+1])].insert(i); } for(int i=1;i<=q;i++){ int tt,ll; scanf("%d %d",&tt,&ll); if(tt==2){ int rr,vv; scanf("%d %d",&rr,&vv); int tut1=*t[vv].lower_bound(ll); int tut2=*s[vv].lower_bound(ll); if(tut1<=rr){ printf("%d %d\n",tut1,tut1); } else if(tut2<=rr-1){ printf("%d %d\n",tut2,tut2+1); } else{ printf("-1 -1\n"); } } else{ int vv; scanf("%d",&vv); t[A[ll]].erase(ll); t[vv].insert(ll); if(ll>1){ int x=f_lca(A[ll-1],A[ll]); s[x].erase(ll-1); x=f_lca(vv,A[ll-1]); s[x].insert(ll-1); } if(ll<m){ int x=f_lca(A[ll],A[ll+1]); s[x].erase(ll); x=f_lca(vv,A[ll+1]); s[x].insert(ll); } A[ll]=vv; } } return 0; }

Compilation message (stderr)

treearray.cpp: In function 'int main()':
treearray.cpp:52:7: 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:54:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&x,&y);
   ~~~~~^~~~~~~~~~~~~~~
treearray.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&A[i]);
   ~~~~~^~~~~~~~~~~~
treearray.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&tt,&ll);
   ~~~~~^~~~~~~~~~~~~~~~~
treearray.cpp:76:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d",&rr,&vv);
    ~~~~~^~~~~~~~~~~~~~~~~
treearray.cpp:91:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d",&vv);
    ~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...