제출 #414858

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

using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

constexpr bool typetest = 0;
constexpr int N = 2e5 + 5;
int m, n, q, a[N], par[N][18], ranks[N];
vector<int> adj[N];
set<int> s[N], p[N];

void Read()
{
    cin >> n >> m >> q;

    for (int i = 1; i < n; ++i)
    {
        int u, v;
        cin >> u >> v;
        adj[v].emplace_back(u);
        adj[u].emplace_back(v);
    }

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

void dfs(int v)
{
    for (int i = 1; i < 18; ++i)
        par[v][i] = par[par[v][i - 1]][i - 1];

    for (auto i : adj[v])
        if (!ranks[i])
        {
            ranks[i] = ranks[v] + 1;
            par[i][0] = v;
            dfs(i);
        }
}

int LCA(int u, int v)
{
    if (ranks[u] < ranks[v])
        swap(u, v);

    for (int i = 17; ~i; --i)
        if (ranks[par[u][i]] >= ranks[v])
            u = par[u][i];

    for (int i = 17; ~i; --i)
        if (par[u][i] != par[v][i])
        {
            u = par[u][i];
            v = par[v][i];
        }

    return (u == v ? u : par[u][0]);
}

void Solve()
{
    ranks[1] = 1;
    dfs(1);

    //cerr << LCA(5, 2) << "\n";

    for (int i = 1; i < m; ++i)
        s[LCA(a[i], a[i + 1])].insert(i);

    while (q--)
    {
        int t;
        cin >> t;

        if (t == 1)
        {
            int pos, u;
            cin >> pos >> u;

            if (pos > 1)
            {
                s[LCA(a[pos], a[pos - 1])].erase(pos - 1);
                s[LCA(a[pos - 1], u)].insert(pos - 1);
            }
            if (pos < m)
            {
                s[LCA(a[pos], a[pos + 1])].erase(pos);
                s[LCA(a[pos + 1], u)].insert(pos);
            }
            p[a[pos]].erase(pos);
            a[pos] = u;
            p[a[pos]].insert(pos);
        }
        else
        {
            int l, r, v;
            cin >> l >> r >> v;

            auto x = s[v].lower_bound(l);

            if (x == s[v].end() || *x >= r)
            {
                x = p[v].lower_bound(l);
                //cerr << (x == s[v].end()) << '\n';

                if (x == p[v].end() || *x > r)
                    cout << "-1 -1\n";
                else
                    cout << *x << " " << *x << "\n";
            }
            else
                cout << *x << " " << *x + 1 << "\n";
        }
    }
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t(1);
    if (typetest)
        cin >> t;
    for (int _ = 1; _ <= t; ++_)
    {
        Read();
        Solve();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...