Submission #48552

#TimeUsernameProblemLanguageResultExecution timeMemory
48552aleksamiBirthday gift (IZhO18_treearray)C++14
100 / 100
1326 ms262144 KiB
#include <bits/stdc++.h> using namespace std; #define MAXN 200005 #define LOG 20 vector <int> g[MAXN]; int a[MAXN]; int in[MAXN]; int out[MAXN]; int rmq[MAXN][LOG]; int T=0; void dfs(int v,int p) { in[v]=++T; rmq[v][0]=p; for(auto x:g[v]) { if(x==p)continue; dfs(x,v); } out[v]=T; } int insubtree(int u,int v) { return (in[u]<=in[v]&&out[u]>=in[v]); } int lca(int u,int v) { if(insubtree(u,v))return u; if(insubtree(v,u))return v; for(int j = LOG-1; j >= 0; j--) { if(!insubtree(rmq[u][j],v))u=rmq[u][j]; } return rmq[u][0]; } set <int> st[MAXN]; set <int> stz[MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n,m,q; cin >> n >> m >> q; for(int i = 0; i < n-1; i++) { int u,v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1,0); rmq[1][0]=1; for(int d = 1; d < LOG; d++) { for(int i = 1; i <= n; i++)rmq[i][d]=rmq[rmq[i][d-1]][d-1]; } for(int i = 1; i <= m; i++)cin >> a[i]; for(int i = 1; i <= m-1; i++) { st[lca(a[i],a[i+1])].insert(i); } for(int i = 1; i <= m; i++) { stz[a[i]].insert(i); } while(q--) { int t; cin >> t; if(t==1) { int idx,val; cin >> idx >> val; if(idx!=m) { st[lca(a[idx],a[idx+1])].erase(idx); st[lca(val,a[idx+1])].insert(idx); } if(idx != 1) { st[lca(a[idx-1],a[idx])].erase(idx-1); st[lca(a[idx-1],val)].insert(idx-1); } stz[a[idx]].erase(idx); stz[val].insert(idx); a[idx]=val; } else { int l,r,v; cin >> l >> r >> v; auto it = st[v].lower_bound(l); auto itt = stz[v].lower_bound(l); if(itt!=stz[v].end() && *itt >= l && *itt <= r) { cout << *itt << " " << *itt << "\n"; continue; } if(it==st[v].end()) { cout << "-1 -1\n"; continue; } if(*it <= r && *it >= l && (*it+1)<=r) { cout << *it << " " << *it+1 << "\n"; } else { cout << "-1 -1\n"; } } } return 0; } /* 5 4 4 1 2 3 1 3 4 5 3 4 5 2 3 2 1 3 1 1 3 5 2 3 4 5 2 1 3 1 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...