Submission #879144

#TimeUsernameProblemLanguageResultExecution timeMemory
879144sysiaBirthday gift (IZhO18_treearray)C++17
100 / 100
1965 ms108884 KiB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 20;
const int mod = 998244353;

void solve(){
    int n, m, q; cin >> n >> m >> q;
    vector<vector<int>>g(n+1);
    for (int i = 1; i<n; i++){
        int a, b; cin >> a >> b;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    vector<int>depth(n+1);
    vector up(n+1, vector<int>(K));
    function<void(int, int)>dfs = [&](int v, int pa){
        up[v][0] = pa;
        for (int i = 1; i<K; i++) up[v][i] = up[up[v][i-1]][i-1];
        for (auto x: g[v]){
            if (x == pa) continue;
            depth[x] = depth[v]+1;
            dfs(x, v);
        }
    };
    dfs(1, 1);
    auto lca = [&](int a, int b){
        if (depth[a] < depth[b]) swap(a, b);
        for (int i = K-1; i>=0; i--){
            if (depth[a] - (1<<i) >= depth[b]){
                a = up[a][i];
            }
        }
        if (a == b) return a;
        for (int i = K-1; i>=0; i--){
            if (up[a][i] != up[b][i]){
                a = up[a][i];
                b = up[b][i];
            }
        }
        return up[a][0];
    };
    vector<int>a(m+1);
    vector<set<T>>s(2);

    for (int i = 1; i<=m; i++){
        cin >> a[i];
        s[0].insert({a[i], i});
        if (i > 1) s[1].insert({lca(a[i], a[i-1]), i-1}); 
    }
    while (q--){
        int t; cin >> t;
        if (t == 2){
            int l, r, v; cin >> l >> r >> v;
            bool f = 0;
            for (int rep = 0; rep < 2; rep++){
                auto it = s[rep].lower_bound({v, l});
                if (it != s[rep].end() && it->first == v && it->second+rep <= r){
                    f = 1;
                    cout << it->second << " " << it->second+rep << "\n";
                    break;
                }
            }
            if (!f) {
                cout << "-1 -1\n";
            }
        } else {
            int i, val; cin >> i >> val;
            s[0].erase({a[i], i});
            if (i > 1) s[1].erase({lca(a[i-1], a[i]), i-1});
            if (i+1 <= m) s[1].erase({lca(a[i], a[i+1]), i});

            a[i] = val;

            s[0].insert({a[i], i});
            if (i > 1) s[1].insert({lca(a[i-1], a[i]), i-1});
            if (i+1 <= m) s[1].insert({lca(a[i], a[i+1]), i});
        }
    }
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    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...