Submission #414858

# Submission time Handle Problem Language Result Execution time Memory
414858 2021-05-31T09:29:50 Z Lam_lai_cuoc_doi Birthday gift (IZhO18_treearray) C++17
100 / 100
1417 ms 85568 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

constexpr bool typetest = 0;
constexpr int N = 2e5 + 5;
int m, n, q, a[N], par[N][18], ranks[N];
vector<int> adj[N];
set<int> s[N], p[N];

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

    for (int i = 1; i < n; ++i)
    {
        int u, v;
        cin >> u >> v;
        adj[v].emplace_back(u);
        adj[u].emplace_back(v);
    }

    for (int i = 1; i <= m; ++i)
    {
        cin >> a[i];
        p[a[i]].insert(i);
    }
}

void dfs(int v)
{
    for (int i = 1; i < 18; ++i)
        par[v][i] = par[par[v][i - 1]][i - 1];

    for (auto i : adj[v])
        if (!ranks[i])
        {
            ranks[i] = ranks[v] + 1;
            par[i][0] = v;
            dfs(i);
        }
}

int LCA(int u, int v)
{
    if (ranks[u] < ranks[v])
        swap(u, v);

    for (int i = 17; ~i; --i)
        if (ranks[par[u][i]] >= ranks[v])
            u = par[u][i];

    for (int i = 17; ~i; --i)
        if (par[u][i] != par[v][i])
        {
            u = par[u][i];
            v = par[v][i];
        }

    return (u == v ? u : par[u][0]);
}

void Solve()
{
    ranks[1] = 1;
    dfs(1);

    //cerr << LCA(5, 2) << "\n";

    for (int i = 1; i < m; ++i)
        s[LCA(a[i], a[i + 1])].insert(i);

    while (q--)
    {
        int t;
        cin >> t;

        if (t == 1)
        {
            int pos, u;
            cin >> pos >> u;

            if (pos > 1)
            {
                s[LCA(a[pos], a[pos - 1])].erase(pos - 1);
                s[LCA(a[pos - 1], u)].insert(pos - 1);
            }
            if (pos < m)
            {
                s[LCA(a[pos], a[pos + 1])].erase(pos);
                s[LCA(a[pos + 1], u)].insert(pos);
            }
            p[a[pos]].erase(pos);
            a[pos] = u;
            p[a[pos]].insert(pos);
        }
        else
        {
            int l, r, v;
            cin >> l >> r >> v;

            auto x = s[v].lower_bound(l);

            if (x == s[v].end() || *x >= r)
            {
                x = p[v].lower_bound(l);
                //cerr << (x == s[v].end()) << '\n';

                if (x == p[v].end() || *x > r)
                    cout << "-1 -1\n";
                else
                    cout << *x << " " << *x << "\n";
            }
            else
                cout << *x << " " << *x + 1 << "\n";
        }
    }
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t(1);
    if (typetest)
        cin >> t;
    for (int _ = 1; _ <= t; ++_)
    {
        Read();
        Solve();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23760 KB n=5
2 Correct 16 ms 23816 KB n=100
3 Correct 16 ms 23828 KB n=100
4 Correct 16 ms 23768 KB n=100
5 Correct 13 ms 23768 KB n=100
6 Correct 15 ms 23816 KB n=100
7 Correct 17 ms 23812 KB n=100
8 Correct 17 ms 23900 KB n=100
9 Correct 16 ms 23756 KB n=100
10 Correct 16 ms 23812 KB n=100
11 Correct 13 ms 23844 KB n=100
12 Correct 16 ms 23796 KB n=100
13 Correct 14 ms 23756 KB n=100
14 Correct 15 ms 23828 KB n=100
15 Correct 17 ms 23808 KB n=100
16 Correct 15 ms 23808 KB n=100
17 Correct 14 ms 23800 KB n=100
18 Correct 16 ms 23768 KB n=100
19 Correct 17 ms 23732 KB n=100
20 Correct 16 ms 23768 KB n=100
21 Correct 16 ms 23744 KB n=100
22 Correct 13 ms 23768 KB n=100
23 Correct 15 ms 23820 KB n=100
24 Correct 17 ms 23768 KB n=100
25 Correct 16 ms 23764 KB n=100
26 Correct 16 ms 23768 KB n=12
27 Correct 15 ms 23812 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23760 KB n=5
2 Correct 16 ms 23816 KB n=100
3 Correct 16 ms 23828 KB n=100
4 Correct 16 ms 23768 KB n=100
5 Correct 13 ms 23768 KB n=100
6 Correct 15 ms 23816 KB n=100
7 Correct 17 ms 23812 KB n=100
8 Correct 17 ms 23900 KB n=100
9 Correct 16 ms 23756 KB n=100
10 Correct 16 ms 23812 KB n=100
11 Correct 13 ms 23844 KB n=100
12 Correct 16 ms 23796 KB n=100
13 Correct 14 ms 23756 KB n=100
14 Correct 15 ms 23828 KB n=100
15 Correct 17 ms 23808 KB n=100
16 Correct 15 ms 23808 KB n=100
17 Correct 14 ms 23800 KB n=100
18 Correct 16 ms 23768 KB n=100
19 Correct 17 ms 23732 KB n=100
20 Correct 16 ms 23768 KB n=100
21 Correct 16 ms 23744 KB n=100
22 Correct 13 ms 23768 KB n=100
23 Correct 15 ms 23820 KB n=100
24 Correct 17 ms 23768 KB n=100
25 Correct 16 ms 23764 KB n=100
26 Correct 16 ms 23768 KB n=12
27 Correct 15 ms 23812 KB n=100
28 Correct 15 ms 23944 KB n=500
29 Correct 15 ms 23868 KB n=500
30 Correct 18 ms 23884 KB n=500
31 Correct 15 ms 23884 KB n=500
32 Correct 15 ms 23924 KB n=500
33 Correct 16 ms 23956 KB n=500
34 Correct 15 ms 23864 KB n=500
35 Correct 17 ms 23884 KB n=500
36 Correct 17 ms 23916 KB n=500
37 Correct 16 ms 23812 KB n=500
38 Correct 15 ms 23804 KB n=500
39 Correct 16 ms 23944 KB n=500
40 Correct 14 ms 23808 KB n=500
41 Correct 14 ms 23884 KB n=500
42 Correct 16 ms 23812 KB n=500
43 Correct 15 ms 23936 KB n=500
44 Correct 18 ms 23884 KB n=500
45 Correct 16 ms 23816 KB n=500
46 Correct 16 ms 23968 KB n=500
47 Correct 15 ms 23940 KB n=500
48 Correct 16 ms 23836 KB n=500
49 Correct 14 ms 23944 KB n=500
50 Correct 17 ms 23936 KB n=500
51 Correct 17 ms 23916 KB n=500
52 Correct 16 ms 23948 KB n=500
53 Correct 17 ms 23884 KB n=500
54 Correct 16 ms 23840 KB n=500
55 Correct 18 ms 23880 KB n=278
56 Correct 16 ms 23860 KB n=500
57 Correct 18 ms 23976 KB n=500
58 Correct 17 ms 23896 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23760 KB n=5
2 Correct 16 ms 23816 KB n=100
3 Correct 16 ms 23828 KB n=100
4 Correct 16 ms 23768 KB n=100
5 Correct 13 ms 23768 KB n=100
6 Correct 15 ms 23816 KB n=100
7 Correct 17 ms 23812 KB n=100
8 Correct 17 ms 23900 KB n=100
9 Correct 16 ms 23756 KB n=100
10 Correct 16 ms 23812 KB n=100
11 Correct 13 ms 23844 KB n=100
12 Correct 16 ms 23796 KB n=100
13 Correct 14 ms 23756 KB n=100
14 Correct 15 ms 23828 KB n=100
15 Correct 17 ms 23808 KB n=100
16 Correct 15 ms 23808 KB n=100
17 Correct 14 ms 23800 KB n=100
18 Correct 16 ms 23768 KB n=100
19 Correct 17 ms 23732 KB n=100
20 Correct 16 ms 23768 KB n=100
21 Correct 16 ms 23744 KB n=100
22 Correct 13 ms 23768 KB n=100
23 Correct 15 ms 23820 KB n=100
24 Correct 17 ms 23768 KB n=100
25 Correct 16 ms 23764 KB n=100
26 Correct 16 ms 23768 KB n=12
27 Correct 15 ms 23812 KB n=100
28 Correct 15 ms 23944 KB n=500
29 Correct 15 ms 23868 KB n=500
30 Correct 18 ms 23884 KB n=500
31 Correct 15 ms 23884 KB n=500
32 Correct 15 ms 23924 KB n=500
33 Correct 16 ms 23956 KB n=500
34 Correct 15 ms 23864 KB n=500
35 Correct 17 ms 23884 KB n=500
36 Correct 17 ms 23916 KB n=500
37 Correct 16 ms 23812 KB n=500
38 Correct 15 ms 23804 KB n=500
39 Correct 16 ms 23944 KB n=500
40 Correct 14 ms 23808 KB n=500
41 Correct 14 ms 23884 KB n=500
42 Correct 16 ms 23812 KB n=500
43 Correct 15 ms 23936 KB n=500
44 Correct 18 ms 23884 KB n=500
45 Correct 16 ms 23816 KB n=500
46 Correct 16 ms 23968 KB n=500
47 Correct 15 ms 23940 KB n=500
48 Correct 16 ms 23836 KB n=500
49 Correct 14 ms 23944 KB n=500
50 Correct 17 ms 23936 KB n=500
51 Correct 17 ms 23916 KB n=500
52 Correct 16 ms 23948 KB n=500
53 Correct 17 ms 23884 KB n=500
54 Correct 16 ms 23840 KB n=500
55 Correct 18 ms 23880 KB n=278
56 Correct 16 ms 23860 KB n=500
57 Correct 18 ms 23976 KB n=500
58 Correct 17 ms 23896 KB n=500
59 Correct 17 ms 24280 KB n=2000
60 Correct 19 ms 24348 KB n=2000
61 Correct 21 ms 24280 KB n=2000
62 Correct 21 ms 24276 KB n=2000
63 Correct 21 ms 24220 KB n=2000
64 Correct 20 ms 24288 KB n=2000
65 Correct 19 ms 24280 KB n=2000
66 Correct 19 ms 24260 KB n=2000
67 Correct 20 ms 24260 KB n=2000
68 Correct 18 ms 24328 KB n=2000
69 Correct 19 ms 24268 KB n=2000
70 Correct 18 ms 24296 KB n=2000
71 Correct 19 ms 24188 KB n=2000
72 Correct 19 ms 24196 KB n=2000
73 Correct 19 ms 24268 KB n=2000
74 Correct 20 ms 24192 KB n=1844
75 Correct 20 ms 24192 KB n=2000
76 Correct 19 ms 24220 KB n=2000
77 Correct 19 ms 24268 KB n=2000
78 Correct 21 ms 24152 KB n=2000
79 Correct 19 ms 24196 KB n=2000
80 Correct 19 ms 24320 KB n=2000
81 Correct 20 ms 24272 KB n=2000
82 Correct 22 ms 24192 KB n=2000
83 Correct 19 ms 24328 KB n=2000
84 Correct 18 ms 24268 KB n=2000
85 Correct 20 ms 24260 KB n=2000
86 Correct 19 ms 24204 KB n=2000
87 Correct 21 ms 24184 KB n=2000
88 Correct 20 ms 24388 KB n=2000
89 Correct 20 ms 24380 KB n=2000
90 Correct 21 ms 24396 KB n=2000
91 Correct 18 ms 24176 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23760 KB n=5
2 Correct 16 ms 23816 KB n=100
3 Correct 16 ms 23828 KB n=100
4 Correct 16 ms 23768 KB n=100
5 Correct 13 ms 23768 KB n=100
6 Correct 15 ms 23816 KB n=100
7 Correct 17 ms 23812 KB n=100
8 Correct 17 ms 23900 KB n=100
9 Correct 16 ms 23756 KB n=100
10 Correct 16 ms 23812 KB n=100
11 Correct 13 ms 23844 KB n=100
12 Correct 16 ms 23796 KB n=100
13 Correct 14 ms 23756 KB n=100
14 Correct 15 ms 23828 KB n=100
15 Correct 17 ms 23808 KB n=100
16 Correct 15 ms 23808 KB n=100
17 Correct 14 ms 23800 KB n=100
18 Correct 16 ms 23768 KB n=100
19 Correct 17 ms 23732 KB n=100
20 Correct 16 ms 23768 KB n=100
21 Correct 16 ms 23744 KB n=100
22 Correct 13 ms 23768 KB n=100
23 Correct 15 ms 23820 KB n=100
24 Correct 17 ms 23768 KB n=100
25 Correct 16 ms 23764 KB n=100
26 Correct 16 ms 23768 KB n=12
27 Correct 15 ms 23812 KB n=100
28 Correct 15 ms 23944 KB n=500
29 Correct 15 ms 23868 KB n=500
30 Correct 18 ms 23884 KB n=500
31 Correct 15 ms 23884 KB n=500
32 Correct 15 ms 23924 KB n=500
33 Correct 16 ms 23956 KB n=500
34 Correct 15 ms 23864 KB n=500
35 Correct 17 ms 23884 KB n=500
36 Correct 17 ms 23916 KB n=500
37 Correct 16 ms 23812 KB n=500
38 Correct 15 ms 23804 KB n=500
39 Correct 16 ms 23944 KB n=500
40 Correct 14 ms 23808 KB n=500
41 Correct 14 ms 23884 KB n=500
42 Correct 16 ms 23812 KB n=500
43 Correct 15 ms 23936 KB n=500
44 Correct 18 ms 23884 KB n=500
45 Correct 16 ms 23816 KB n=500
46 Correct 16 ms 23968 KB n=500
47 Correct 15 ms 23940 KB n=500
48 Correct 16 ms 23836 KB n=500
49 Correct 14 ms 23944 KB n=500
50 Correct 17 ms 23936 KB n=500
51 Correct 17 ms 23916 KB n=500
52 Correct 16 ms 23948 KB n=500
53 Correct 17 ms 23884 KB n=500
54 Correct 16 ms 23840 KB n=500
55 Correct 18 ms 23880 KB n=278
56 Correct 16 ms 23860 KB n=500
57 Correct 18 ms 23976 KB n=500
58 Correct 17 ms 23896 KB n=500
59 Correct 17 ms 24280 KB n=2000
60 Correct 19 ms 24348 KB n=2000
61 Correct 21 ms 24280 KB n=2000
62 Correct 21 ms 24276 KB n=2000
63 Correct 21 ms 24220 KB n=2000
64 Correct 20 ms 24288 KB n=2000
65 Correct 19 ms 24280 KB n=2000
66 Correct 19 ms 24260 KB n=2000
67 Correct 20 ms 24260 KB n=2000
68 Correct 18 ms 24328 KB n=2000
69 Correct 19 ms 24268 KB n=2000
70 Correct 18 ms 24296 KB n=2000
71 Correct 19 ms 24188 KB n=2000
72 Correct 19 ms 24196 KB n=2000
73 Correct 19 ms 24268 KB n=2000
74 Correct 20 ms 24192 KB n=1844
75 Correct 20 ms 24192 KB n=2000
76 Correct 19 ms 24220 KB n=2000
77 Correct 19 ms 24268 KB n=2000
78 Correct 21 ms 24152 KB n=2000
79 Correct 19 ms 24196 KB n=2000
80 Correct 19 ms 24320 KB n=2000
81 Correct 20 ms 24272 KB n=2000
82 Correct 22 ms 24192 KB n=2000
83 Correct 19 ms 24328 KB n=2000
84 Correct 18 ms 24268 KB n=2000
85 Correct 20 ms 24260 KB n=2000
86 Correct 19 ms 24204 KB n=2000
87 Correct 21 ms 24184 KB n=2000
88 Correct 20 ms 24388 KB n=2000
89 Correct 20 ms 24380 KB n=2000
90 Correct 21 ms 24396 KB n=2000
91 Correct 18 ms 24176 KB n=2000
92 Correct 872 ms 73032 KB n=200000
93 Correct 1326 ms 79360 KB n=200000
94 Correct 1279 ms 83908 KB n=200000
95 Correct 843 ms 72876 KB n=200000
96 Correct 842 ms 72816 KB n=200000
97 Correct 1385 ms 78344 KB n=200000
98 Correct 831 ms 72856 KB n=200000
99 Correct 1076 ms 73056 KB n=200000
100 Correct 853 ms 72836 KB n=200000
101 Correct 1236 ms 85568 KB n=200000
102 Correct 500 ms 73968 KB n=200000
103 Correct 527 ms 74076 KB n=200000
104 Correct 521 ms 74012 KB n=200000
105 Correct 535 ms 74304 KB n=200000
106 Correct 538 ms 74496 KB n=200000
107 Correct 528 ms 74380 KB n=200000
108 Correct 1056 ms 72836 KB n=200000
109 Correct 1004 ms 72816 KB n=200000
110 Correct 1004 ms 73008 KB n=200000
111 Correct 917 ms 72300 KB n=200000
112 Correct 1286 ms 84328 KB n=200000
113 Correct 1417 ms 78228 KB n=200000
114 Correct 887 ms 72384 KB n=200000
115 Correct 1402 ms 75484 KB n=200000
116 Correct 832 ms 72960 KB n=200000
117 Correct 1237 ms 84736 KB n=200000
118 Correct 1339 ms 76860 KB n=200000
119 Correct 833 ms 72880 KB n=200000
120 Correct 1208 ms 84620 KB n=200000
121 Correct 1194 ms 84756 KB n=200000
122 Correct 1270 ms 84904 KB n=200000
123 Correct 564 ms 74372 KB n=200000
124 Correct 304 ms 38724 KB n=25264