# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1022423 | hasan2006 | Birthday gift (IZhO18_treearray) | C++17 | 720 ms | 147536 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define se second
#define fi first
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"
const int N = 5e5 + 9 , mod = 1e9 + 7;
ll a[N] , b[N] , dp[N] , c[N] , d[N] , p[N][25] , us[N] , us1[N] , timer = 0;
vector<int>v[N];
void dfs(int n , int par = 0){
p[n][0] = par;
us[n] = ++timer;
for(int i = 1; i <= 20; i++)
p[n][i] = p[p[n][i - 1]][i - 1];
for(auto to : v[n])
if(to != par)
dfs(to , n);
us1[n] = ++timer;
}
bool ok(int x , int y){
return (us[x] <= us[y] && us1[x] >= us1[y]);
}
ll lca(ll x , ll y){
if(ok(x , y))
return x;
if(ok(y , x))
return y;
for(int i = 20; i >= 0; i--)
if(p[x][i] && !ok(p[x][i] , y))
x = p[x][i];
return p[x][0];
}
set<int>st[N] , ss[N];
void solve()
{
ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
cin>>n>>m>>q;
for(i = 1; i < n; i++){
cin>>l>>r;
v[l].pb(r);
v[r].pb(l);
}
dfs(1);
for(i = 1; i <= m; i++){
cin>>a[i];
ss[a[i]].insert(i);
if(i != 1)
b[i] = lca(a[i] , a[i - 1]) , st[b[i]].insert(i);
}
while(q--){
cin>>k>>x>>y;
if(k == 1){
ss[a[x]].erase(x);
a[x] = y;
ss[a[x]].insert(x);
if(x != m){
st[b[x + 1]].erase(x + 1);
b[x + 1] = lca(a[x + 1], a[x]);
st[b[x + 1]].insert(x + 1);
}
if(x != 1){
st[b[x]].erase(x);
b[x] = lca(a[x - 1], a[x]);
st[b[x]].insert(x);
}
}else {
cin>>k;
auto it = st[k].upper_bound(x);
if(it != st[k].end() && *it <= y){
cout<<*it - 1 << " "<<*it<<"\n";
continue;
}
auto it1 = ss[k].upper_bound(x - 1);
if(it1 != ss[k].end() && *it1 <= y){
cout<<*it1 << " "<<*it1<<"\n";
continue;
}
cout<<-1<<" "<<-1<<"\n";
}
}
}
int main(){
TL;
/*#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif*/
int t = 1;
// cin>>t;
while(t--)
{
solve();
}
}
// Author : حسن
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |