제출 #1314597

#제출 시각아이디문제언어결과실행 시간메모리
1314597hrantsargsyanBirthday gift (IZhO18_treearray)C++20
56 / 100
4093 ms39032 KiB
#include <iostream>
#include <vector>

using namespace std;

vector<int> a;
vector<int> lcas;
const int N = 2e5 + 5;
const int LOG = 25;
int m;
vector<int> adj[N];
int up[N][LOG];
int dept[N];


void dfs(int node, int parent)
{
    for (auto i : adj[node])
    {
        if (i == parent)
            continue;
        up[i][0] = node;
        for (int j = 1;j < LOG;++j)
        {
            up[i][j] = up[up[i][j - 1]][j - 1];
        }
        dept[i] = dept[node] + 1;
        dfs(i, node);
    }
}

int lift(int a, int k)
{
    for (int j = LOG - 1;j >= 0;--j)
    {
        if (k & (1 << j))
        {
            a = up[a][j];
        }
    }
    return a;
}

int lca(int a, int b)
{
    if (a == b)
        return a;
    if (dept[a] < dept[b])
    {
        swap(a, b);
    }
    int dif = dept[a] - dept[b];
    a = lift(a, dif);
    if (a == b)
        return a;
    for (int j = LOG - 1;j >= 0;--j)
    {
        if (up[a][j] != up[b][j])
        {
            a = up[a][j];
            b = up[b][j];
        }
    }
    return up[a][0];
}


int main()
{
    int n, q;
    cin >> n >> m >> q;
    for (int i = 0;i < n - 1;++i)
    {
        int x, y;
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    dept[1] = 0;
    dfs(1, 0);
    for (int i = 0;i < m;++i)
    {
        int x;
        cin >> x;
        a.push_back(x);
    }
    for (int i = 0;i < q;++i)
    {
        int type;
        cin >> type;
        if (type == 1)
        {
            int ind, v;
            cin >> ind >> v;
            ind--;
            a[ind] = v;
        }
        else if (type == 2)
        {
            int l, r, v;
            cin >> l >> r >> v;
            --l;
            --r;
            bool flag = false;

            for (int j = l;j < r;++j)
            {
                if (a[j] == v)
                {
                    cout << j + 1 << " " << j + 1 << endl;
                    flag = true;
                    break;
                }
                if (lca(a[j], a[j + 1]) == v)
                {
                    cout << j + 1 << " " << j + 2 << endl;
                    flag = true;
                    break;
                }

            }
            if ((a[r] == v) && flag==false)
            {
                cout << r + 1 << " " << r + 1 << endl;
                flag = true;
            }
            if (!flag)
            {
                cout << -1 << " " << -1 << endl;
            }
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...