Submission #865719

#TimeUsernameProblemLanguageResultExecution timeMemory
865719vjudge1Birthday gift (IZhO18_treearray)C++17
100 / 100
697 ms88800 KiB
/// tree bends in youth
/// 24  .10.2023
/// success is doing same thing in every single day!!!
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(x) x.begin(), x.end()
#define F first
#define S second
using namespace std;
const ll N =2e5+ 5;
const ll NN =2e6 + 5;
const ll INF = -1e1;
const ll MOD = 1e9 + 7;
const ll LG = 18;
const ll k = 316;
vector <int> g[N];
int cnt;
int tin[N],tout[N],used[N],p[N][20],a[N];
set <int> d[N],b[N];
void dfs(int v){
    cnt++;
    tin[v] = cnt;
    used[v] = 1;
    for(int to : g[v]){
        if(used[to] == 0){
            p[to][0] = v;
            dfs(to);
        }
    }
    tout[v] = cnt;
}
bool upper(int x,int y){
    if(tin[x] <= tin[y] && tout[x] >= tout[y])return 1;
    return 0;
}
int lca(int x,int y){
    if(upper(x,y) == 1){
        return x;
    }
    if(upper(y,x) == 1){
        return y;
    }
    for(int i = 18;i >= 0;i--){
        int h = p[x][i];
        if(upper(h,y) == 0){
            x = h;
        }
    }
    return p[x][0];
}
void solve(){
    int n,m,q;
    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);
    }
    p[1][0] = 1;
    dfs(1);
    for(int i = 1;i <= 18;i++){
        for(int j = 1;j <= n;j++){
            p[j][i] = p[p[j][i - 1]][i - 1];
        }
    }
    for(int i= 1;i <= m;i++){
        cin >> a[i];
        b[a[i]].insert(i);
        if(i > 1){
            d[lca(a[i],a[i - 1])].insert(i - 1);
        }
    }
    while(q--){
        int tp,l,r,v;cin >> tp;
        if(tp == 1){
            cin >> l >> r;
            b[a[l]].erase(l);
            if(l > 1)d[lca(a[l - 1],a[l])].erase(l - 1);
            if(l < m)d[lca(a[l + 1],a[l])].erase(l);
            a[l] = r;
            if(l > 1)d[lca(a[l - 1],a[l])].insert(l - 1);
            if(l < m)d[lca(a[l + 1],a[l])].insert(l);
            b[a[l]].insert(l);
        }
        else{
            cin >> l >>r  >> v;
            if(d[v].lower_bound(l) != d[v].end() && *d[v].lower_bound(l) < r){
                int x = *d[v].lower_bound(l);
                cout << x << ' ' << x + 1 << '\n';
            }
            else if(b[v].lower_bound(l) != b[v].end() && *b[v].lower_bound(l) <= r){
                int x = *b[v].lower_bound(l);
                cout << x << ' ' << x << '\n';
            }
            else{
                cout << "-1 -1\n";
            }
        }
    }
}
main (){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
    ll abd= 1;
//    cin >> abd;
    for(ll i = 1;i <= abd;i++){
//        cout << "Case " << i << ":\n";
        solve();
    }
}
//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


//5 4 1
//1 2
//3 1
//3 4
//5 3
//4 5 2 3
//2 1 3 1

Compilation message (stderr)

treearray.cpp:101:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  101 | main (){
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...