제출 #155130

#제출 시각아이디문제언어결과실행 시간메모리
155130theboatmanBirthday gift (IZhO18_treearray)C++17
56 / 100
1445 ms81580 KiB
#include <bits/stdc++.h>

#define y1 theboatman
#define make_struct(args...) {args}

using namespace std;

struct Tree {
    multiset <int> s;

    void modify(int pos, int val) {
        if (val == 1) {
            s.insert(pos);
        }
        else {
            s.erase(s.find(pos));
        }
    }

    int get(int l, int r) {
        auto it = s.lower_bound(l);
        if (it == s.end()) {
            return int(1e9);
        }
        else {
            return *it;
        }
    }
};

struct Lca {
    int n, bit, timer;
    vector <int> tin, tout;
    vector <vector <int> > g, up;

    void dfs(int v, int from) {
        tin[v] = timer++;

        up[v][0] = from;
        for(int i = 1; i < bit; i++) {
            up[v][i] = up[up[v][i - 1]][i - 1];
        }

        for(auto to : g[v]) {
            if (to != from) {
                dfs(to, v);
            }
        }

        tout[v] = timer++;
    }

    void build(vector <vector <int> > &temp_graph) {
        g = temp_graph;
        n = g.size();

        bit = 0;
        while((1 << bit) <= n) {
            bit++;
        }
        bit--;

        tin.resize(n);
        tout.resize(n);
        up.resize(n, vector <int> (bit, 1));

        timer = 0;
        dfs(1, 1);
    }

    bool upper(int a, int b) {
        return tin[a] <= tin[b] && tout[a] >= tout[b];
    }

    int get(int a, int b) {
        if (upper(a, b)) {
            return a;
        }

        if (upper(b, a)) {
            return b;
        }

        for(int i = bit - 1; i > -1; i--) {
            if (!upper(up[a][i], b)) {
                a = up[a][i];
            }
        }

        return up[a][0];
    }
};

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

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

    vector <vector <int> > g(n + 1);
    for(int i = 0; i < n - 1; i++) {
        int x, y;
        cin >> x >> y;

        g[x].push_back(y);
        g[y].push_back(x);
    }

    vector <int> a(m + 1);
    for(int i = 1; i <= m; i++) {
        cin >> a[i];
    }

    Lca lca;
    lca.build(g);

    vector <Tree> tree(n + 1);

    for(int i = 2; i <= m; i++) {
        int c = lca.get(a[i], a[i - 1]);
        tree[c].modify(i - 1, 1);
    }

    for(int i = 1; i <= m; i++) {
        tree[a[i]].modify(i, 1);
    }

    for(int it = 0; it < q; it++) {
        int type;
        cin >> type;

        if (type == 1) {
            int pos, val;
            cin >> pos >> val;

            for(int i = max(pos, 2); i <= min(m, pos + 1); i++) {
                int c = lca.get(a[i], a[i - 1]);
                tree[c].modify(i - 1, -1);
            }

            tree[a[pos]].modify(pos, -1);
            a[pos] = val;
            tree[a[pos]].modify(pos, 1);

            for(int i = max(pos, 2); i <= min(m, pos + 1); i++) {
                int c = lca.get(a[i], a[i - 1]);
                tree[c].modify(i - 1, 1);
            }
        }
        else {
            int l, r, c;
            cin >> l >> r >> c;

            int pos = tree[c].get(l, r);

            if (pos > r || pos + (a[pos] != c) > r) {
                cout << "-1 -1\n";
            }
            else {
                cout << pos << " " << pos + (a[pos] != c) << "\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...