Submission #1097837

#TimeUsernameProblemLanguageResultExecution timeMemory
1097837TrinhKhanhDungBirthday gift (IZhO18_treearray)C++14
56 / 100
660 ms85584 KiB
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define sz(x) (int)x.size() #define ALL(v) v.begin(),v.end() #define MASK(k) (1LL << (k)) #define BIT(x, i) (((x) >> (i)) & 1) #define oo (ll)1e18 #define INF (ll)1e9 #define MOD (ll)(1e9 + 7) using namespace std; template<class T1, class T2> bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;} template<class T1, class T2> bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;} template<class T1, class T2> void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;} template<class T1, class T2> void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;} template<class T> void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());} const int MAX = 2e5 + 10, LOG = 18; int N, M, Q; vector<int> adj[MAX]; int h[MAX], d[MAX][LOG], a[MAX]; void dfs(int u, int p){ for(int v: adj[u]) if(v != p){ h[v] = h[u] + 1; d[v][0] = u; for(int i=1; i<LOG; i++){ d[v][i] = d[d[v][i - 1]][i - 1]; } dfs(v, u); } } int lca(int u, int v){ if(h[u] < h[v]) swap(u, v); int delta = h[u] - h[v]; for(int i=0; i<LOG; i++) if(BIT(delta, i)){ u = d[u][i]; } if(u == v) return u; for(int i=LOG-1; i>=0; i--) if(d[u][i] != d[v][i]){ u = d[u][i]; v = d[v][i]; } return d[u][0]; } set<int> st[2][MAX]; void solve(){ cin >> N >> M >> Q; for(int i=1; i<N; i++){ int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for(int i=1; i<=M; i++){ cin >> a[i]; } dfs(1, 0); for(int i=2; i<=M; i++){ st[1][lca(a[i - 1], a[i])].insert(i); } for(int i=1; i<=M; i++){ st[0][a[i]].insert(i); } while(Q--){ int t; cin >> t; if(t == 1){ int i, u; cin >> i >> u; st[0][a[i]].erase(i); if(i > 1) st[1][lca(a[i - 1], a[i])].erase(i); if(i < N) st[1][lca(a[i], a[i + 1])].erase(i + 1); a[i] = u; st[0][a[i]].insert(i); if(i > 1) st[1][lca(a[i - 1], a[i])].insert(i); if(i < N) st[1][lca(a[i], a[i + 1])].insert(i + 1); } if(t == 2){ int l, r, u; cin >> l >> r >> u; auto it = st[0][u].lower_bound(l); if(it != st[0][u].end() && *it <= r){ cout << *it << ' ' << *it << '\n'; continue; } it = st[1][u].upper_bound(l); if(it != st[1][u].end() && *it <= r){ cout << *it - 1 << ' ' << *it << '\n'; continue; } cout << -1 << ' ' << -1 << '\n'; } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); // freopen("deggraph.inp", "r", stdin); // freopen("deggraph.out", "w", stdout); 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...