Submission #500358

# Submission time Handle Problem Language Result Execution time Memory
500358 2021-12-30T19:21:10 Z RedBoy Birthday gift (IZhO18_treearray) C++11
0 / 100
17 ms 23900 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(pos);
    b[val].insert(pos);

    v[pos] = val;
}

pair<int, int> query(int l, int r, int val)
{
    //cout << "Q: " << val << " - ";

    auto x = b[val].lower_bound(l);
    //cout << *x << '\n';

    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 13 ms 23756 KB n=5
2 Correct 12 ms 23756 KB n=100
3 Correct 12 ms 23824 KB n=100
4 Correct 16 ms 23832 KB n=100
5 Correct 13 ms 23840 KB n=100
6 Correct 12 ms 23756 KB n=100
7 Correct 13 ms 23832 KB n=100
8 Correct 12 ms 23756 KB n=100
9 Correct 12 ms 23756 KB n=100
10 Correct 13 ms 23728 KB n=100
11 Correct 16 ms 23900 KB n=100
12 Incorrect 17 ms 23704 KB Wrong output format.
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23756 KB n=5
2 Correct 12 ms 23756 KB n=100
3 Correct 12 ms 23824 KB n=100
4 Correct 16 ms 23832 KB n=100
5 Correct 13 ms 23840 KB n=100
6 Correct 12 ms 23756 KB n=100
7 Correct 13 ms 23832 KB n=100
8 Correct 12 ms 23756 KB n=100
9 Correct 12 ms 23756 KB n=100
10 Correct 13 ms 23728 KB n=100
11 Correct 16 ms 23900 KB n=100
12 Incorrect 17 ms 23704 KB Wrong output format.
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23756 KB n=5
2 Correct 12 ms 23756 KB n=100
3 Correct 12 ms 23824 KB n=100
4 Correct 16 ms 23832 KB n=100
5 Correct 13 ms 23840 KB n=100
6 Correct 12 ms 23756 KB n=100
7 Correct 13 ms 23832 KB n=100
8 Correct 12 ms 23756 KB n=100
9 Correct 12 ms 23756 KB n=100
10 Correct 13 ms 23728 KB n=100
11 Correct 16 ms 23900 KB n=100
12 Incorrect 17 ms 23704 KB Wrong output format.
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23756 KB n=5
2 Correct 12 ms 23756 KB n=100
3 Correct 12 ms 23824 KB n=100
4 Correct 16 ms 23832 KB n=100
5 Correct 13 ms 23840 KB n=100
6 Correct 12 ms 23756 KB n=100
7 Correct 13 ms 23832 KB n=100
8 Correct 12 ms 23756 KB n=100
9 Correct 12 ms 23756 KB n=100
10 Correct 13 ms 23728 KB n=100
11 Correct 16 ms 23900 KB n=100
12 Incorrect 17 ms 23704 KB Wrong output format.
13 Halted 0 ms 0 KB -