Submission #41675

#TimeUsernameProblemLanguageResultExecution timeMemory
41675IvanCBirthday gift (IZhO18_treearray)C++14
30 / 100
4048 ms6908 KiB
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> ii; const int MAXN = 2*1e5 + 10; vector<int> grafo[MAXN]; int ini[MAXN],dfsNum,leftmost[MAXN],rightmost[MAXN],treearray[MAXN],n,m,q; ii vetorzao[2*MAXN]; void dfs(int v,int p,int depth){ ini[v] = ++dfsNum; vetorzao[dfsNum] = ii(depth,v); for(int u : grafo[v]){ if(u == p) continue; dfs(u,v,depth+1); vetorzao[++dfsNum] = ii(depth,v); } } int main(){ scanf("%d %d %d",&n,&m,&q); for(int i = 1;i<n;i++){ int u,v; scanf("%d %d",&u,&v); grafo[u].push_back(v); grafo[v].push_back(u); } dfs(1,-1,0); for(int v = 1;v<=n;v++){ int ptr = ini[v]; while(ptr + 1 <= dfsNum && ((vetorzao[ptr+1].first > vetorzao[ini[v]].first) || (vetorzao[ptr+1] == vetorzao[ini[v]]))){ ptr++; } rightmost[v] = ptr; ptr = ini[v]; while(ptr - 1 >= 1 && ((vetorzao[ptr-1].first > vetorzao[ini[v]].first) || (vetorzao[ptr-1] == vetorzao[ini[v]]))){ ptr--; } leftmost[v] = ptr; } for(int i = 1;i<=m;i++){ scanf("%d",&treearray[i]); treearray[i] = ini[treearray[i]]; } for(int pergunta = 1;pergunta<=q;pergunta++){ int operacao; scanf("%d",&operacao); if(operacao == 1){ int idx,v; scanf("%d %d",&idx,&v); treearray[idx] = ini[v]; } else{ int l,r,v,flag = 0; scanf("%d %d %d",&l,&r,&v); for(int i = l;i<=r && !flag;i++){ int minimo = treearray[i],maximo = treearray[i],lo = treearray[i],hi = treearray[i]; ii ancestral = ii(vetorzao[minimo]); for(int j = i;j<=r && !flag;j++){ minimo = min(minimo,treearray[j]); maximo = max(maximo,treearray[j]); while(hi +1 <= maximo){ ancestral = min(ancestral,vetorzao[hi+1]); hi++; } while(lo - 1 >= minimo){ ancestral = min(ancestral,vetorzao[lo-1]); lo--; } if(ancestral.second == v){ printf("%d %d\n",i,j); flag = 1; } else if(ancestral.first < vetorzao[ini[v]].first){ break; } } } if(!flag) printf("-1 -1\n"); } } return 0; }

Compilation message (stderr)

treearray.cpp: In function 'int main()':
treearray.cpp:18:28: 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:21:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&u,&v);
                       ^
treearray.cpp:39:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&treearray[i]);
                            ^
treearray.cpp:44:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&operacao);
                        ^
treearray.cpp:47:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d",&idx,&v);
                          ^
treearray.cpp:52:30: 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...