Submission #1340218

#TimeUsernameProblemLanguageResultExecution timeMemory
1340218iamhereforfunBirthday gift (IZhO18_treearray)C++20
100 / 100
476 ms194140 KiB
// Starcraft 2 enjoyer //

#include <bits/stdc++.h>

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

#define LSOne(X) ((X) & -(X))

const int N = 1e6 + 5;
const int M = 2e5 + 5;
const int O = 1e5 + 5;
const int LG = 21;
const int INF = 1e9 + 5;
const int B = 1000;
const int MOD = 1e9 + 7;

struct Value
{
    int l, r, v;
    bool b;
    Value()
    {
        l = r = v = -1;
        b = 0;
    }
    Value(int l, int r, int v, bool b) : l(l), r(r), v(v), b(b) {}
};

int n, m, q, a[N], st[N], en[N], id, depth[N], ptr[N];
pair<int, int> sparseTable[LG][N];
vector<int> adj[N];
vector<pair<int, int>> tour;
set<pair<int, int>> gud[N];

void dfs1(int c, int p)
{
    st[c] = id++;
    ptr[st[c]] = c;
    tour.push_back({depth[c], c});
    for (int i : adj[c])
    {
        if (i == p)
            continue;
        dfs1(i, c);
        tour.push_back({depth[c], c});
        id++;
    }
    en[c] = id++;
    tour.push_back({depth[c], c});
}

void buildSparseTable()
{
    for (int x = 0; x < tour.size(); x++)
    {
        sparseTable[0][x] = tour[x];
    }
    for (int x = 1; x < LG; x++)
    {
        for (int y = 0; y + (1 << x) - 1 < (int)tour.size(); y++)
        {
            sparseTable[x][y] = min(sparseTable[x - 1][y], sparseTable[x - 1][y + (1 << (x - 1))]);
        }
    }
}

int getLCA(int l, int r)
{
    l = st[l], r = st[r];
    if (l > r)
        swap(l, r);
    // cout << l << " " << r << "\n";
    int lg = __lg(r - l + 1);
    // cout << min(sparseTable[lg][l], sparseTable[lg][r - (1 << lg) + 1]).first << " ";
    // cout << min(sparseTable[lg][l], sparseTable[lg][r - (1 << lg) + 1]).second << "v\n";
    return min(sparseTable[lg][l], sparseTable[lg][r - (1 << lg) + 1]).second;
}

void update(int l, int r, int i)
{
    if (r > m || l < 1)
        return;
    int j = getLCA(a[l], a[r]);
    if (l > r)
        swap(l, r);
    if (i == 0)
    {
        gud[j].erase({l, r});
    }
    else
    {
        gud[j].insert({l, r});
    }
}

inline void solve()
{
    cin >> n >> m >> q;
    for (int x = 0; x < n - 1; x++)
    {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    for (int x = 1; x <= m; x++)
    {
        cin >> a[x];
    }
    dfs1(1, 0);
    buildSparseTable();
    for (int x = 1; x < m; x++)
    {
        update(x, x + 1, 1);
        update(x, x, 1);
    }
    update(m, m, 1);
    for (int x = 0; x < q; x++)
    {
        int t;
        cin >> t;
        if (t == 1)
        {
            int p, v;
            cin >> p >> v;
            update(p, p + 1, 0);
            update(p, p, 0);
            update(p, p - 1, 0);
            a[p] = v;
            update(p, p + 1, 1);
            update(p, p, 1);
            update(p, p - 1, 1);
        }
        else
        {
            int l, r, v;
            cin >> l >> r >> v;
            auto it = gud[v].lower_bound({l, 0});
            if (it != gud[v].end() && it->second <= r)
                cout << it->first << " " << it->second << "\n";
            else
                cout << -1 << " " << -1 << "\n";
        }
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    for (int x = 1; x <= t; x++)
    {
        solve();
    }
    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...