Submission #951265

#TimeUsernameProblemLanguageResultExecution timeMemory
951265LOLOLOBirthday gift (IZhO18_treearray)C++17
100 / 100
920 ms138412 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())

const int N = 5e5 + 100;
vector <int> pos[N], ed[N];
int p[N][20], in[N], ou[N], timer = 1, a[N];

void dfs(int u, int v) {
    in[u] = timer++;
    p[u][0] = v;
    for (int i = 1; i < 20; i++)
        p[u][i] = p[p[u][i - 1]][i - 1];

    for (auto x : ed[u]) {
        if (x == v)
            continue;

        dfs(x, u);
    }
    ou[u] = timer;
}

bool is(int a, int b) {
    return in[a] <= in[b] && ou[a] >= ou[b];
}

int lca(int a, int b) {
    if (is(a, b))
        return a;

    if (is(b, a))
        return b;

    for (int i = 19; i >= 0; i--) {
        if (is(p[a][i], b) == 0)
            a = p[a][i];
    }

    return p[a][0];
}

set <int> adj[N], el[N];

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

    int n, m, q;
    cin >> n >> m >> q;

    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        ed[a].pb(b);
        ed[b].pb(a);
    }

    dfs(1, 1);
    for (int i = 1; i <= m; i++) {
        cin >> a[i];
        el[a[i]].insert(i);
    }

    for (int i = 1; i < m; i++) {
        adj[lca(a[i], a[i + 1])].insert(i);
    }

    for (int i = 1; i <= q; i++) {
        int t;
        cin >> t;

        if (t == 1) {
            int p, v;
            cin >> p >> v;
            el[a[p]].erase(p);
            el[v].insert(p);

            if (p < m) {
                adj[lca(a[p], a[p + 1])].erase(p);
                adj[lca(v, a[p + 1])].insert(p);
            }

            if (p > 1) {
                adj[lca(a[p - 1], a[p])].erase(p - 1);
                adj[lca(v, a[p - 1])].insert(p - 1);
            }
            a[p] = v;
        } else {
            int l, r, v;
            cin >> l >> r >> v;
            auto it = el[v].lower_bound(l);
            if (it != el[v].end() && *it <= r) {
                cout << *it << " " << *it << '\n';
                continue;
            }

            it = adj[v].lower_bound(l);
            if (it != adj[v].end() && *it < r) {
                cout << *it << " " << *it + 1 << '\n';
                continue;
            }

            cout << -1 << " " << -1 << '\n';
        }
    }

    return 0;
}

/*
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
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...