제출 #334841

#제출 시각아이디문제언어결과실행 시간메모리
334841amunduzbaevBirthday gift (IZhO18_treearray)C++14
12 / 100
4065 ms492 KiB
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define pb push_back #define mp make_pair #define ub upper_bound #define lb lower_bound #define ll long long #define ld long double #define pii pair<int, int> #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(),x.rend() #define prc(n) fixed << setprecision(n) #define fastios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define pi acos(-1); const int inf = 1e9+7; const int N = 505; int n, m, q, a[N], par[N], used[N], s, dis[N], fir[N]; vector<int>edges[N]; vector<int>order; vector<pii>tree; void build(int lx, int rx, int x){ if(lx == rx){ if(lx > sz(order)) return; tree[x].ff = dis[order[lx-1]]; tree[x].ss = order[lx-1];; return; } int m = (lx + rx)/2; build(lx, m, x*2); build(m+1, rx, x*2+1); if(tree[x*2].ff <= tree[x*2+1].ff) tree[x] = tree[x*2]; else tree[x] = tree[x*2+1]; } pii find(int l, int r, int lx, int rx, int x){ if(lx > r || rx < l) return {inf, inf}; if(lx >= l && rx <= r) return tree[x]; int m = (lx + rx)/2; pii res1 = find(l, r, lx, m, x*2); pii res2 = find(l, r, m+1, rx, x*2+1); return (res1.ff <= res2.ff ? res1 : res2); } void dfs(int u, int p, int d){ order.pb(u); dis[u] = d; if(fir[u] == -1) fir[u] = sz(order); for(auto x:edges[u]){ if(x == p) continue; dfs(x, u, d+1); order.pb(u); } } void solve(){ cin>>n>>m>>q; for(int i=1;i<n;i++){ int a, b; cin>>a>>b; edges[a].pb(b); edges[b].pb(a); } memset(fir, -1, sizeof(fir)); dfs(1, 0, 0); s = 2; while(s < sz(order)) s*=2; tree.assign(2*s, {inf, inf}); build(1, s, 1); for(int i=1;i<=m;i++) cin>>a[i]; while(q--){ int t; cin>>t; if(t == 1){ int pos, val; cin>>pos>>val; a[pos] = val; } else{ int l, r, val, rr; cin>>l>>r>>val; int la = -1, ra = -1; for(;l<= r; l++){ int prev = -1; for(rr = l; rr <= r; rr++){ int lx = min(fir[a[l]], fir[a[rr]]), rx = max(fir[a[l]], fir[a[rr]]); int res = find(lx, rx, 1, s, 1).ss; if(rr - l){ lx = min(fir[prev], fir[res]), rx = max(fir[prev], fir[res]); res = find(lx, rx, 1, s, 1).ss; } if(res == val) if(rr - l +1 >= ra - la +1) la = l, ra = rr; prev = res; } } cout<<la<<" "<<ra<<"\n"; } } return; } int main(){ fastios int t = 1; //cin>>t; while(t--) solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...