Submission #860474

#TimeUsernameProblemLanguageResultExecution timeMemory
860474aykhnBirthday gift (IZhO18_treearray)C++14
100 / 100
1330 ms101204 KiB
#include <bits/stdc++.h>
 
// author : aykhn
 
using namespace std;
typedef long long ll;
 
#define pb push_back
#define ins insert
#define mpr make_pair
#define all(v) v.begin(), v.end()
#define bpc __builtin_popcount
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define int ll
#define infll 0x3F3F3F3F3F3F3F3F
#define inf 0x3F3F3F3F

const int MXN = 2e5 + 5;
const int LOG = 19;

int n, sz = 1;
int a[MXN];
int par[LOG][MXN], in[MXN], out[MXN];
vector<int> adj[MXN];
set<int> idx[MXN], id[MXN];
int tim = -1;

void dfs(int a, int p)
{
    in[a] = ++tim;
    for (int v : adj[a])
    {
        if (v == p) continue;
        par[0][v] = a;
        dfs(v, a);
    }
    out[a] = ++tim;
}

bool anc(int u, int v)
{
    return in[u] <= in[v] && out[v] <= out[u];
}

int LCA(int u, int v)
{
    if (anc(u, v)) return u;
    if (anc(v, u)) return v;
    for (int i = LOG - 1; i >= 0; i--)
    {
        if (anc(par[i][u], v)) continue;
        u = par[i][u];
    }
    return par[0][u];
}


signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    par[0][1] = 1;
    dfs(1, 1);
    for (int i = 1; i < LOG; i++)
    {
        for (int j = 1; j <= n; j++) par[i][j] = par[i - 1][par[i - 1][j]];
    }
    for (int i = 0; i < m; i++) 
    {
        cin >> a[i];
        id[a[i]].ins(i);
    }
    for (int i = 0; i + 1 < m; i++)
    {
        int lca = LCA(a[i], a[i + 1]);
        idx[lca].ins(i);
    }
    while (q--)
    {
        int type;
        cin >> type;
        if (type == 1)
        {
            int pos, v;
            cin >> pos >> v;
            pos--;
            id[a[pos]].erase(pos);
            id[v].ins(pos);
            if (pos) 
            {
                int lca = LCA(a[pos - 1], a[pos]);
                idx[lca].erase(pos - 1);
            }
            if (pos + 1 < m)
            {
                int lca = LCA(a[pos], a[pos + 1]);
                idx[lca].erase(pos);
            }
            a[pos] = v;
            if (pos) 
            {
                int lca = LCA(a[pos - 1], a[pos]);
                idx[lca].ins(pos - 1);
            }
            if (pos + 1 < m)
            {
                int lca = LCA(a[pos], a[pos + 1]);
                idx[lca].ins(pos);
            }
        }
        else
        {
            int l, r, v;
            cin >> l >> r >> v;
            l--;
            r--;
            auto it = id[v].lower_bound(l);
            if (it != id[v].end() && *it <= r)
            {
                cout << *it + 1 << " " << *it + 1 << '\n';
                continue;
            }
            auto it1 = idx[v].lower_bound(l);
            if (it1 != idx[v].end() && *it1 < r)
            {
                cout << *it1 + 1 << " " << *it1 + 2 << '\n';
                continue;
            }
            cout << "-1 -1" << '\n';
        }
    }

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...