Submission #41554

#TimeUsernameProblemLanguageResultExecution timeMemory
41554PajarajaBirthday gift (IZhO18_treearray)C++14
100 / 100
1479 ms262144 KiB
#include <bits/stdc++.h> #define MAXN 200007 #define MAXL 22 using namespace std; vector<int> g[MAXN]; multiset<int> k[MAXN],km[MAXN]; multiset<int>::iterator it; int p[MAXL][MAXN],a[MAXN],b[MAXN],in[MAXN],out[MAXN],n,cnt; void dfs(int s,int f) { p[0][s]=f; in[s]=cnt++; for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfs(g[s][i],s); out[s]=cnt++; } bool insub(int x,int y) {return ((in[x]<=in[y])&&(out[x]>=out[y]));} void lcainit() { dfs(1,0); in[0]=-1; out[0]=2*MAXN; for(int i=1;i<MAXL;i++) for(int j=1;j<=n;j++) p[i][j]=p[i-1][p[i-1][j]]; } int lca(int x,int y) { if(insub(x,y)) return x; if(insub(y,x)) return y; for(int i=MAXL-1;i>=0;i--) if(!insub(p[i][x],y)) x=p[i][x]; return p[0][x]; } int main() { int m,q; scanf("%d%d%d",&n,&m,&q); for(int i=0;i<n-1;i++) { int t1,t2; scanf("%d%d",&t1,&t2); g[t1].push_back(t2); g[t2].push_back(t1); } lcainit(); for(int i=1;i<=m;i++) scanf("%d",&a[i]); for(int i=1;i<m;i++) b[i]=lca(a[i],a[i+1]); for(int i=1;i<=m;i++) k[a[i]].insert(i); for(int i=1;i<m;i++) km[b[i]].insert(i); while(q--) { int t; scanf("%d",&t); if(t==1) { int t1,t2; scanf("%d%d",&t1,&t2); if(t1>1) { int u=lca(t2,a[t1-1]); km[b[t1-1]].erase(t1-1); km[u].insert(t1-1); b[t1-1]=u; } if(t1<m) { int u=lca(t2,a[t1+1]); km[b[t1]].erase(t1); km[u].insert(t1); b[t1]=u; } k[a[t1]].erase(t1); a[t1]=t2; k[a[t1]].insert(t1); } else { int l,r,u; scanf("%d%d%d",&l,&r,&u); it=k[u].lower_bound(l); if(it!=k[u].end() && *it<=r) {printf("%d %d\n",*it,*it); continue;} it=km[u].lower_bound(l); if(it!=km[u].end() && *it<r) {printf("%d %d\n",*it,*it+1); continue;} printf("-1 -1\n"); } } }

Compilation message (stderr)

treearray.cpp: In function 'void dfs(int, int)':
treearray.cpp:13:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfs(g[s][i],s);
               ^
treearray.cpp: In function 'int main()':
treearray.cpp:34:26: 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:38:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&t1,&t2);
                        ^
treearray.cpp:43:41: 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:50:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&t);
                 ^
treearray.cpp:54:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d%d",&t1,&t2);
                         ^
treearray.cpp:76:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d%d%d",&l,&r,&u);
                            ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...