Submission #500779

#TimeUsernameProblemLanguageResultExecution timeMemory
500779pragmatistBirthday gift (IZhO18_treearray)C++17
30 / 100
139 ms17932 KiB
#include <bits/stdc++.h> #define pb push_back #define sz(v) (int)v.size() #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define x first #define y second #define int long long #define nl "\n" using namespace std; typedef long long ll; typedef pair<long long, long long> pll; typedef pair <ll, ll> pii; const int N = (int)2e5 + 7; const int M = (int)7e6 + 7; const ll MOD = (ll)1e9 + 7; const int inf = (int)1e9 + 7; const ll INF = (ll)3e18 + 7; pii dir[] = {{-1, -1}, {1, 1}, {-1, 1}, {1, -1}}; int n, m, q, a[N], val[501][501]; int timer, tin[N], tout[N], up[N][19]; vector<int> g[N]; void calc(int v, int pr) { up[v][0] = pr; tin[v] = ++timer; for(int i = 1; i <= 18; ++i) { up[v][i] = up[up[v][i-1]][i-1]; } for(auto to : g[v]) { if(to != pr) { calc(to, v); } } tout[v] = ++timer; } bool is_parent(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; } int lca(int u, int v) { if(is_parent(u, v)) return u; if(is_parent(v, u)) return v; for(int i = 18; i >= 0; --i) { if(!is_parent(up[u][i], v)) { u = up[u][i]; } } return up[u][0]; } void solve() { cin >> n >> m >> q; for(int i = 1; i < n; ++i) { int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } for(int i = 1; i <= m; ++i) cin >> a[i]; calc(1, 1); for(int i = 1; i <= m; ++i) { int x = a[i]; for(int j = i; j <= m; ++j) { x = lca(x, a[j]); val[i][j] = x; } } while(q--) { char tp; cin >> tp; int l, r, v; if(tp == '1') { cin >> l >> r; a[l] = r; for(int i = 1; i <= m; ++i) { int x = a[i]; for(int j = i; j <= m; ++j) { x = lca(x, a[j]); val[i][j] = x; } } } else { cin >> l >> r >> v; int a = -1, b = -1; for(int i = l; i <= r && a == -1; ++i) { for(int j = i; j <= r; ++j) { if(val[i][j] == v) { a = i; b = j; break; } } } cout << a << ' ' << b << nl; } } } signed main() { ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0); int test = 1; //cin >> test; for(int i = 1; i <= test; ++i) { //cout << "Case " << i << ": "; solve(); } return 0; } /* 3 3 1 2 3 1 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...