제출 #831485

#제출 시각아이디문제언어결과실행 시간메모리
831485CookieBirthday gift (IZhO18_treearray)C++14
100 / 100
939 ms81560 KiB
#include<bits/stdc++.h>
#include<fstream>
#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx2")
using namespace std;
//ifstream fin("FEEDING.INP");
//ofstream fout("FEEDING.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const int base = 74;
const int mxn = 2e5 + 5;
int n, m, q;
vt<int>adj[mxn + 1];
int dep[mxn + 1], up[mxn + 1][18], a[mxn + 1], tin[mxn + 1], tt = 0, tout[mxn + 1], inf = 1e9;
void dfs(int s, int pre){
    tin[s] = ++tt;
    for(auto i: adj[s]){
        if(i != pre){
            dep[i] = dep[s] + 1;
            dfs(i, s); up[i][0] = s; 
        }
    }
    tout[s] = tt;
}
int lca(int u, int v){
    if(dep[u] < dep[v])swap(u, v);
    int k = dep[u] - dep[v];
    forr(i, 0, 18){
        if(k & (1 << i)){
            u = up[u][i];
        }
    }
    if(u == v)return(u);
    dorr(i, 17, 0){
        if(up[u][i] != up[v][i]){
            u = up[u][i]; v = up[v][i];
        }
    }
    return(up[u][0]);
}
set<int>cand[mxn + 1], app[mxn + 1];
signed main()
{
     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m >> q;
    // if there exist answer, either its (i, i) or (i, i + 1)

    forr(i, 1, n){
        int u, v; cin >> u >> v;
        adj[u].pb(v); adj[v].pb(u);
    }
    
    dfs(1, -1);
    forr(i, 1, 18){
        for(int j = 1; j <= n; j++){
            up[j][i] = up[up[j][i - 1]][i - 1];
        }
    }
    for(int i = 1; i <= m; i++){
        cin >> a[i]; app[a[i]].insert(i);
    }
    for(int i = 2; i <= m; i++){
        cand[lca(a[i - 1], a[i])].insert(i - 1);
    
    }
    while(q--){
        int idq; cin >> idq;
        if(idq == 1){
            int id, x; cin >> id >> x;
            if(id > 1)cand[lca(a[id - 1], a[id])].erase(id - 1); 
            if(id < m)cand[lca(a[id], a[id + 1])].erase(id);
            app[a[id]].erase(id);
            a[id] = x;
            if(id > 1)cand[lca(a[id - 1], a[id])].insert(id - 1); 
            if(id < m)cand[lca(a[id], a[id + 1])].insert(id);
            app[a[id]].insert(id);
        }else{
           int l, r, v; cin >> l >> r >> v;
           auto it = app[v].lower_bound(l);
           if(it != app[v].end() && *it <= r){
               cout << *it << " " << *it << "\n";
           }else{
               auto it2 = cand[v].lower_bound(l);
               if(it2 != cand[v].end() && *it2 < r){
                   cout << *it2 << " " << *it2 + 1 << "\n";
               }else{
                   cout << -1 << " " << -1 << "\n";
               }
           }
        }
    }
    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...