Submission #886316

# Submission time Handle Problem Language Result Execution time Memory
886316 2023-12-11T19:48:33 Z boris_mihov Birthday gift (IZhO18_treearray) C++17
100 / 100
1169 ms 84740 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <bitset>
#include <vector>
#include <stack>
#include <set>

typedef long long llong;
const int MAXN = 200000 + 10;
const int MAXLOG = 18;

int n, m, q;
struct Sparse
{
    int sparse[MAXLOG][MAXN];
    int depth[MAXN];
    int par[MAXN];

    void build(int _depth[], int _par[])
    {
        for (int i = 1 ; i <= n ; ++i)
        {
            par[i] = _par[i];
            depth[i] = _depth[i];
            sparse[0][i] = par[i];
        }

        for (int log = 1 ; (1 << log) <= n ; ++log)
        {
            for (int i = 1 ; i <= n ; ++i)
            {
                sparse[log][i] = sparse[log - 1][sparse[log - 1][i]];
            }
        }
    }

    int equalize(int x, int y)
    {
        for (int log = MAXLOG - 1 ; log >= 0 ; --log)
        {
            if (depth[sparse[log][x]] >= depth[y])
            {
                x = sparse[log][x];
            }
        }

        return x;
    }

    int calcLCA(int x, int y)
    {
        if (x == y)
        {
            return x;
        }

        for (int log = MAXLOG - 1 ; log >= 0 ; --log)
        {
            if (sparse[log][x] != sparse[log][y])
            {
                x = sparse[log][x];
                y = sparse[log][y];
            }
        }

        return sparse[0][x];
    }

    int findLCA(int x, int y)
    {
        if (depth[x] < depth[y])
        {
            std::swap(x, y);
        }

        return calcLCA(equalize(x, y), y);
    }
};

int a[MAXN];
Sparse sparse;
int par[MAXN];
int depth[MAXN];
std::set <int> byLCA[MAXN];
std::set <int> byLCA2[MAXN];
std::vector <int> g[MAXN];

void dfs(int node, int p)
{
    par[node] = p;
    depth[node] = depth[p] + 1;
    
    for (const int &u : g[node])
    {
        if (u == p)
        {
            continue;
        }

        dfs(u, node);
    }
}

void solve()
{   
    dfs(1, 0);
    sparse.build(depth, par);

    for (int i = 1 ; i <= m ; ++i)
    {
        byLCA[a[i]].insert(i);
        if (i != m)
        {
            byLCA2[sparse.findLCA(a[i], a[i + 1])].insert(i);
        }
    }

    for (int query = 1 ; query <= q ; ++query)
    {
        int qType;
        std::cin >> qType;

        if (qType == 1)
        {
            int pos, node;
            std::cin >> pos >> node;
            byLCA[a[pos]].erase(byLCA[a[pos]].find(pos));
            
            if (pos < m)
            {
                int lca = sparse.findLCA(a[pos], a[pos + 1]);
                byLCA2[lca].erase(byLCA2[lca].find(pos));
            }

            if (pos > 1)
            {
                int lca = sparse.findLCA(a[pos], a[pos - 1]);
                byLCA2[lca].erase(byLCA2[lca].find(pos - 1));
            }

            a[pos] = node;
            byLCA[a[pos]].insert(pos);

            if (pos < m)
            {
                int lca = sparse.findLCA(a[pos], a[pos + 1]);
                byLCA2[lca].insert(pos);
            }

            if (pos > 1)
            {
                int lca = sparse.findLCA(a[pos], a[pos - 1]);
                byLCA2[lca].insert(pos - 1);
            }

            continue;
        }

        int l, r, node;
        std::cin >> l >> r >> node;

        auto it = byLCA[node].lower_bound(l);
        if (it != byLCA[node].end() && (*it) <= r)
        {
            std::cout << (*it) << ' ' << (*it) << '\n';
            continue;
        }

        it = byLCA2[node].lower_bound(l);
        if (it != byLCA2[node].end() && (*it) < r)
        {
            std::cout << (*it) << ' ' << (*it) + 1 << '\n';
            continue;
        }

        std::cout << -1 << ' ' << -1 << '\n';
    } 
}

void input()
{
    std::cin >> n >> m >> q;
    for (int i = 2 ; i <= n ; ++i)
    {
        int u, v;
        std::cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    for (int i = 1 ; i <= m ; ++i)
    {
        std::cin >> a[i];
    }
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 31324 KB n=5
2 Correct 8 ms 35420 KB n=100
3 Correct 6 ms 35500 KB n=100
4 Correct 7 ms 35420 KB n=100
5 Correct 7 ms 35420 KB n=100
6 Correct 7 ms 35420 KB n=100
7 Correct 7 ms 35416 KB n=100
8 Correct 7 ms 35416 KB n=100
9 Correct 7 ms 35420 KB n=100
10 Correct 6 ms 35420 KB n=100
11 Correct 8 ms 35420 KB n=100
12 Correct 7 ms 35416 KB n=100
13 Correct 6 ms 35420 KB n=100
14 Correct 6 ms 35420 KB n=100
15 Correct 7 ms 35420 KB n=100
16 Correct 7 ms 35536 KB n=100
17 Correct 7 ms 35420 KB n=100
18 Correct 7 ms 35420 KB n=100
19 Correct 7 ms 35420 KB n=100
20 Correct 7 ms 35420 KB n=100
21 Correct 6 ms 35500 KB n=100
22 Correct 6 ms 35420 KB n=100
23 Correct 6 ms 35416 KB n=100
24 Correct 7 ms 35420 KB n=100
25 Correct 7 ms 35420 KB n=100
26 Correct 6 ms 33372 KB n=12
27 Correct 6 ms 35420 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 10 ms 31324 KB n=5
2 Correct 8 ms 35420 KB n=100
3 Correct 6 ms 35500 KB n=100
4 Correct 7 ms 35420 KB n=100
5 Correct 7 ms 35420 KB n=100
6 Correct 7 ms 35420 KB n=100
7 Correct 7 ms 35416 KB n=100
8 Correct 7 ms 35416 KB n=100
9 Correct 7 ms 35420 KB n=100
10 Correct 6 ms 35420 KB n=100
11 Correct 8 ms 35420 KB n=100
12 Correct 7 ms 35416 KB n=100
13 Correct 6 ms 35420 KB n=100
14 Correct 6 ms 35420 KB n=100
15 Correct 7 ms 35420 KB n=100
16 Correct 7 ms 35536 KB n=100
17 Correct 7 ms 35420 KB n=100
18 Correct 7 ms 35420 KB n=100
19 Correct 7 ms 35420 KB n=100
20 Correct 7 ms 35420 KB n=100
21 Correct 6 ms 35500 KB n=100
22 Correct 6 ms 35420 KB n=100
23 Correct 6 ms 35416 KB n=100
24 Correct 7 ms 35420 KB n=100
25 Correct 7 ms 35420 KB n=100
26 Correct 6 ms 33372 KB n=12
27 Correct 6 ms 35420 KB n=100
28 Correct 7 ms 37468 KB n=500
29 Correct 8 ms 37468 KB n=500
30 Correct 7 ms 37464 KB n=500
31 Correct 8 ms 37464 KB n=500
32 Correct 7 ms 37468 KB n=500
33 Correct 7 ms 37468 KB n=500
34 Correct 7 ms 37468 KB n=500
35 Correct 7 ms 37676 KB n=500
36 Correct 8 ms 37468 KB n=500
37 Correct 7 ms 37568 KB n=500
38 Correct 7 ms 37468 KB n=500
39 Correct 7 ms 37468 KB n=500
40 Correct 8 ms 37552 KB n=500
41 Correct 7 ms 37464 KB n=500
42 Correct 7 ms 37468 KB n=500
43 Correct 8 ms 37468 KB n=500
44 Correct 7 ms 37720 KB n=500
45 Correct 8 ms 37468 KB n=500
46 Correct 7 ms 37468 KB n=500
47 Correct 8 ms 37468 KB n=500
48 Correct 7 ms 37548 KB n=500
49 Correct 7 ms 37468 KB n=500
50 Correct 8 ms 37724 KB n=500
51 Correct 8 ms 37468 KB n=500
52 Correct 8 ms 37464 KB n=500
53 Correct 8 ms 37468 KB n=500
54 Correct 7 ms 37468 KB n=500
55 Correct 7 ms 37468 KB n=278
56 Correct 7 ms 37468 KB n=500
57 Correct 7 ms 37468 KB n=500
58 Correct 9 ms 37468 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 10 ms 31324 KB n=5
2 Correct 8 ms 35420 KB n=100
3 Correct 6 ms 35500 KB n=100
4 Correct 7 ms 35420 KB n=100
5 Correct 7 ms 35420 KB n=100
6 Correct 7 ms 35420 KB n=100
7 Correct 7 ms 35416 KB n=100
8 Correct 7 ms 35416 KB n=100
9 Correct 7 ms 35420 KB n=100
10 Correct 6 ms 35420 KB n=100
11 Correct 8 ms 35420 KB n=100
12 Correct 7 ms 35416 KB n=100
13 Correct 6 ms 35420 KB n=100
14 Correct 6 ms 35420 KB n=100
15 Correct 7 ms 35420 KB n=100
16 Correct 7 ms 35536 KB n=100
17 Correct 7 ms 35420 KB n=100
18 Correct 7 ms 35420 KB n=100
19 Correct 7 ms 35420 KB n=100
20 Correct 7 ms 35420 KB n=100
21 Correct 6 ms 35500 KB n=100
22 Correct 6 ms 35420 KB n=100
23 Correct 6 ms 35416 KB n=100
24 Correct 7 ms 35420 KB n=100
25 Correct 7 ms 35420 KB n=100
26 Correct 6 ms 33372 KB n=12
27 Correct 6 ms 35420 KB n=100
28 Correct 7 ms 37468 KB n=500
29 Correct 8 ms 37468 KB n=500
30 Correct 7 ms 37464 KB n=500
31 Correct 8 ms 37464 KB n=500
32 Correct 7 ms 37468 KB n=500
33 Correct 7 ms 37468 KB n=500
34 Correct 7 ms 37468 KB n=500
35 Correct 7 ms 37676 KB n=500
36 Correct 8 ms 37468 KB n=500
37 Correct 7 ms 37568 KB n=500
38 Correct 7 ms 37468 KB n=500
39 Correct 7 ms 37468 KB n=500
40 Correct 8 ms 37552 KB n=500
41 Correct 7 ms 37464 KB n=500
42 Correct 7 ms 37468 KB n=500
43 Correct 8 ms 37468 KB n=500
44 Correct 7 ms 37720 KB n=500
45 Correct 8 ms 37468 KB n=500
46 Correct 7 ms 37468 KB n=500
47 Correct 8 ms 37468 KB n=500
48 Correct 7 ms 37548 KB n=500
49 Correct 7 ms 37468 KB n=500
50 Correct 8 ms 37724 KB n=500
51 Correct 8 ms 37468 KB n=500
52 Correct 8 ms 37464 KB n=500
53 Correct 8 ms 37468 KB n=500
54 Correct 7 ms 37468 KB n=500
55 Correct 7 ms 37468 KB n=278
56 Correct 7 ms 37468 KB n=500
57 Correct 7 ms 37468 KB n=500
58 Correct 9 ms 37468 KB n=500
59 Correct 10 ms 37720 KB n=2000
60 Correct 10 ms 37980 KB n=2000
61 Correct 9 ms 37720 KB n=2000
62 Correct 10 ms 37912 KB n=2000
63 Correct 9 ms 37720 KB n=2000
64 Correct 10 ms 37724 KB n=2000
65 Correct 10 ms 37892 KB n=2000
66 Correct 10 ms 37724 KB n=2000
67 Correct 10 ms 37724 KB n=2000
68 Correct 10 ms 37720 KB n=2000
69 Correct 9 ms 37724 KB n=2000
70 Correct 9 ms 37724 KB n=2000
71 Correct 9 ms 37724 KB n=2000
72 Correct 9 ms 37724 KB n=2000
73 Correct 9 ms 37720 KB n=2000
74 Correct 9 ms 37724 KB n=1844
75 Correct 8 ms 37724 KB n=2000
76 Correct 9 ms 37720 KB n=2000
77 Correct 10 ms 37724 KB n=2000
78 Correct 10 ms 37724 KB n=2000
79 Correct 9 ms 37724 KB n=2000
80 Correct 10 ms 37724 KB n=2000
81 Correct 10 ms 37724 KB n=2000
82 Correct 9 ms 37724 KB n=2000
83 Correct 9 ms 37768 KB n=2000
84 Correct 9 ms 37976 KB n=2000
85 Correct 9 ms 37876 KB n=2000
86 Correct 9 ms 37724 KB n=2000
87 Correct 9 ms 37724 KB n=2000
88 Correct 9 ms 37724 KB n=2000
89 Correct 11 ms 38308 KB n=2000
90 Correct 9 ms 37976 KB n=2000
91 Correct 9 ms 37896 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 10 ms 31324 KB n=5
2 Correct 8 ms 35420 KB n=100
3 Correct 6 ms 35500 KB n=100
4 Correct 7 ms 35420 KB n=100
5 Correct 7 ms 35420 KB n=100
6 Correct 7 ms 35420 KB n=100
7 Correct 7 ms 35416 KB n=100
8 Correct 7 ms 35416 KB n=100
9 Correct 7 ms 35420 KB n=100
10 Correct 6 ms 35420 KB n=100
11 Correct 8 ms 35420 KB n=100
12 Correct 7 ms 35416 KB n=100
13 Correct 6 ms 35420 KB n=100
14 Correct 6 ms 35420 KB n=100
15 Correct 7 ms 35420 KB n=100
16 Correct 7 ms 35536 KB n=100
17 Correct 7 ms 35420 KB n=100
18 Correct 7 ms 35420 KB n=100
19 Correct 7 ms 35420 KB n=100
20 Correct 7 ms 35420 KB n=100
21 Correct 6 ms 35500 KB n=100
22 Correct 6 ms 35420 KB n=100
23 Correct 6 ms 35416 KB n=100
24 Correct 7 ms 35420 KB n=100
25 Correct 7 ms 35420 KB n=100
26 Correct 6 ms 33372 KB n=12
27 Correct 6 ms 35420 KB n=100
28 Correct 7 ms 37468 KB n=500
29 Correct 8 ms 37468 KB n=500
30 Correct 7 ms 37464 KB n=500
31 Correct 8 ms 37464 KB n=500
32 Correct 7 ms 37468 KB n=500
33 Correct 7 ms 37468 KB n=500
34 Correct 7 ms 37468 KB n=500
35 Correct 7 ms 37676 KB n=500
36 Correct 8 ms 37468 KB n=500
37 Correct 7 ms 37568 KB n=500
38 Correct 7 ms 37468 KB n=500
39 Correct 7 ms 37468 KB n=500
40 Correct 8 ms 37552 KB n=500
41 Correct 7 ms 37464 KB n=500
42 Correct 7 ms 37468 KB n=500
43 Correct 8 ms 37468 KB n=500
44 Correct 7 ms 37720 KB n=500
45 Correct 8 ms 37468 KB n=500
46 Correct 7 ms 37468 KB n=500
47 Correct 8 ms 37468 KB n=500
48 Correct 7 ms 37548 KB n=500
49 Correct 7 ms 37468 KB n=500
50 Correct 8 ms 37724 KB n=500
51 Correct 8 ms 37468 KB n=500
52 Correct 8 ms 37464 KB n=500
53 Correct 8 ms 37468 KB n=500
54 Correct 7 ms 37468 KB n=500
55 Correct 7 ms 37468 KB n=278
56 Correct 7 ms 37468 KB n=500
57 Correct 7 ms 37468 KB n=500
58 Correct 9 ms 37468 KB n=500
59 Correct 10 ms 37720 KB n=2000
60 Correct 10 ms 37980 KB n=2000
61 Correct 9 ms 37720 KB n=2000
62 Correct 10 ms 37912 KB n=2000
63 Correct 9 ms 37720 KB n=2000
64 Correct 10 ms 37724 KB n=2000
65 Correct 10 ms 37892 KB n=2000
66 Correct 10 ms 37724 KB n=2000
67 Correct 10 ms 37724 KB n=2000
68 Correct 10 ms 37720 KB n=2000
69 Correct 9 ms 37724 KB n=2000
70 Correct 9 ms 37724 KB n=2000
71 Correct 9 ms 37724 KB n=2000
72 Correct 9 ms 37724 KB n=2000
73 Correct 9 ms 37720 KB n=2000
74 Correct 9 ms 37724 KB n=1844
75 Correct 8 ms 37724 KB n=2000
76 Correct 9 ms 37720 KB n=2000
77 Correct 10 ms 37724 KB n=2000
78 Correct 10 ms 37724 KB n=2000
79 Correct 9 ms 37724 KB n=2000
80 Correct 10 ms 37724 KB n=2000
81 Correct 10 ms 37724 KB n=2000
82 Correct 9 ms 37724 KB n=2000
83 Correct 9 ms 37768 KB n=2000
84 Correct 9 ms 37976 KB n=2000
85 Correct 9 ms 37876 KB n=2000
86 Correct 9 ms 37724 KB n=2000
87 Correct 9 ms 37724 KB n=2000
88 Correct 9 ms 37724 KB n=2000
89 Correct 11 ms 38308 KB n=2000
90 Correct 9 ms 37976 KB n=2000
91 Correct 9 ms 37896 KB n=2000
92 Correct 877 ms 75608 KB n=200000
93 Correct 947 ms 80364 KB n=200000
94 Correct 852 ms 83540 KB n=200000
95 Correct 786 ms 75172 KB n=200000
96 Correct 755 ms 75200 KB n=200000
97 Correct 986 ms 79428 KB n=200000
98 Correct 758 ms 75208 KB n=200000
99 Correct 996 ms 75516 KB n=200000
100 Correct 809 ms 75224 KB n=200000
101 Correct 916 ms 84740 KB n=200000
102 Correct 416 ms 76248 KB n=200000
103 Correct 402 ms 76292 KB n=200000
104 Correct 393 ms 76368 KB n=200000
105 Correct 426 ms 76648 KB n=200000
106 Correct 378 ms 76756 KB n=200000
107 Correct 417 ms 76660 KB n=200000
108 Correct 1016 ms 75516 KB n=200000
109 Correct 981 ms 75360 KB n=200000
110 Correct 1010 ms 75604 KB n=200000
111 Correct 794 ms 74764 KB n=200000
112 Correct 857 ms 83928 KB n=200000
113 Correct 988 ms 79232 KB n=200000
114 Correct 715 ms 74888 KB n=200000
115 Correct 1169 ms 77368 KB n=200000
116 Correct 861 ms 75556 KB n=200000
117 Correct 927 ms 84300 KB n=200000
118 Correct 1098 ms 78520 KB n=200000
119 Correct 719 ms 75408 KB n=200000
120 Correct 868 ms 84052 KB n=200000
121 Correct 910 ms 83936 KB n=200000
122 Correct 880 ms 84148 KB n=200000
123 Correct 395 ms 76628 KB n=200000
124 Correct 170 ms 54260 KB n=25264