Submission #500327

# Submission time Handle Problem Language Result Execution time Memory
500327 2021-12-30T18:20:00 Z RedBoy Birthday gift (IZhO18_treearray) C++11
0 / 100
14 ms 23760 KB
#include <iostream>
#include <vector>
#include <set>
#include <bitset>

using namespace std;

const int N_MAX = 2e5 + 5;
const int LOG = 20;

int n, m, q;
int v[N_MAX], depth[N_MAX], p[N_MAX][LOG + 5];
vector<int> adj[N_MAX];
set<int> a[N_MAX], b[N_MAX];

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

    for(int i = 1; i < n; i++)
    {
        int x, y;
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

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

void dfs(int node, int father = 1)
{
    depth[node] = depth[father] + 1;
    p[node][0] = father;
    for(int j = 1; j <= LOG; j++)
        p[node][j] = p[p[node][j - 1]][j - 1];

    for(auto it: adj[node])
    {
        if(it == father)
            continue;
        dfs(it, node);
    }
}

int lca(int x, int y)
{
    if(depth[x] < depth[y])
        swap(x, y);

    int k = depth[x] - depth[y];
    for(int j = LOG; j >= 0; j--)
        if(k & (1 << j))
            x = p[x][j];

    if(x == y)
        return x;

    for(int j = LOG; j >= 0; j--)
    {
        if(p[x][j] != p[y][j])
        {
            x = p[x][j];
            y = p[y][j];
        }
    }

    return p[x][0];
}

void update(int pos, int val)
{
    if(pos > 1)
    {
        a[lca(v[pos - 1], v[pos])].erase(pos - 1);
        a[lca(v[pos - 1], val)].insert(pos - 1);
    }
    if(pos < m)
    {
        a[lca(v[pos], v[pos + 1])].erase(pos);
        a[lca(val, v[pos + 1])].insert(pos);
    }

    b[v[pos]].erase(val);
    b[val].insert(pos);

    v[pos] = val;
}

pair<int, int> query(int l, int r, int val)
{
    auto x = b[val].lower_bound(l);
    if(x != b[val].end() && *x <= r)
        return {*x, *x};

    x = a[val].lower_bound(l);
    if(x != a[val].end() && *x <= r)
        return {*x, *x + 1};

    return {-1, -1};
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    scan();
    dfs(1);

    for(int i = 1; i <= m; i++)
    {
        if(i < m)
            a[lca(v[i], v[i + 1])].insert(i);
        b[v[i]].insert(i);
    }

    while(q--)
    {
        int op;
        cin >> op;
        if(op == 1)
        {
            int pos, val;
            cin >> pos >> val;
            update(pos, val);
        }
        else
        {
            int l, r, val;
            cin >> l >> r >> val;
            pair<int, int> ans = query(l, r, val);
            cout << ans.first << ' ' << ans.second << '\n';
        }
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23756 KB n=5
2 Incorrect 11 ms 23760 KB Wrong range.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23756 KB n=5
2 Incorrect 11 ms 23760 KB Wrong range.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23756 KB n=5
2 Incorrect 11 ms 23760 KB Wrong range.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23756 KB n=5
2 Incorrect 11 ms 23760 KB Wrong range.
3 Halted 0 ms 0 KB -