답안 #750581

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
750581 2023-05-29T18:29:25 Z anaduguleanu Birthday gift (IZhO18_treearray) C++14
100 / 100
2454 ms 92528 KB
#include <iostream>
#include <vector>
#include <set>
#define LOG 20
#define MAX 200000
using namespace std;
///ifstream cin ("c.in");
///ofstream cout ("c.out");
long long a[MAX + 10], level[MAX + 10], ancestor[LOG + 10][MAX + 10];
vector <long long> graph[MAX + 10];
set <long long> posSequence[MAX + 10], posLca[MAX + 10];
void dfs(long long node, long long parent, long long depth)
{
    level[node] = depth;
    for (auto next : graph[node])
        if (next != parent)
        {
            ancestor[0][next] = node;
            dfs(next, node, depth + 1);
        }
}
long long lca(long long x, long long y)
{
    if (level[y] > level[x])
        swap(x, y);
    for (long long p = LOG; p >= 0; p--)
        if (level[ancestor[p][x]] >= level[y])
            x = ancestor[p][x];
    if (x == y)
        return x;
    for (long long p = LOG; p >= 0; p--)
        if (ancestor[p][x] != ancestor[p][y])
        {
            x = ancestor[p][x];
            y = ancestor[p][y];
        }
    return ancestor[0][x];
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    long long n, m, q;
    cin >> n >> m >> q;
    for (long long i = 1; i < n; i++)
    {
        long long x, y;
        cin >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    for (long long p = 0; p <= LOG; p++)
        ancestor[p][1] = 1;
    dfs(1, 0, 0);
    for (long long p = 1; p <= LOG; p++)
        for (long long node = 2; node <= n; node++)
            ancestor[p][node] = ancestor[p - 1][ancestor[p - 1][node]];
    for (long long i = 1; i <= m; i++)
    {
        cin >> a[i];
        posSequence[a[i]].insert(i);
    }
    for (long long i = 1; i < m; i++)
        posLca[lca(a[i], a[i + 1])].insert(i);
    for (long long qq = 1; qq <= q; qq++)
    {
        long long type;
        cin >> type;
        if (type == 1)
        {
            long long pos, val;
            cin >> pos >> val;
            posSequence[a[pos]].erase(pos);
            posSequence[val].insert(pos);
            if (pos != n)
            {
                posLca[lca(a[pos], a[pos + 1])].erase(pos);
                posLca[lca(val, a[pos + 1])].insert(pos);
            }
            if (pos != 1)
            {
                posLca[lca(a[pos - 1], a[pos])].erase(pos - 1);
                posLca[lca(a[pos - 1], val)].insert(pos - 1);
            }
            a[pos] = val;
        }
        else
        {
            long long l, r, valLca;
            cin >> l >> r >> valLca;
            auto it = posSequence[valLca].lower_bound(l);
            if (it != posSequence[valLca].end() && *it <= r)
                cout << *it << ' ' << *it << '\n';
            else
            {
                it = posLca[valLca].lower_bound(l);
                if (it != posLca[valLca].end() && *it < r)
                    cout << *it << ' ' << (*it) + 1 << '\n';
                else
                    cout << "-1 1\n";
            }
        }
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23968 KB n=5
2 Correct 14 ms 23892 KB n=100
3 Correct 12 ms 23984 KB n=100
4 Correct 12 ms 23944 KB n=100
5 Correct 12 ms 23892 KB n=100
6 Correct 12 ms 23892 KB n=100
7 Correct 12 ms 23892 KB n=100
8 Correct 12 ms 23940 KB n=100
9 Correct 13 ms 23940 KB n=100
10 Correct 13 ms 23940 KB n=100
11 Correct 12 ms 23892 KB n=100
12 Correct 12 ms 23944 KB n=100
13 Correct 12 ms 23892 KB n=100
14 Correct 12 ms 23892 KB n=100
15 Correct 12 ms 23892 KB n=100
16 Correct 11 ms 23940 KB n=100
17 Correct 12 ms 23944 KB n=100
18 Correct 12 ms 23944 KB n=100
19 Correct 12 ms 23984 KB n=100
20 Correct 12 ms 23944 KB n=100
21 Correct 12 ms 23924 KB n=100
22 Correct 12 ms 23988 KB n=100
23 Correct 15 ms 23892 KB n=100
24 Correct 14 ms 23892 KB n=100
25 Correct 13 ms 23948 KB n=100
26 Correct 12 ms 23892 KB n=12
27 Correct 12 ms 23892 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23968 KB n=5
2 Correct 14 ms 23892 KB n=100
3 Correct 12 ms 23984 KB n=100
4 Correct 12 ms 23944 KB n=100
5 Correct 12 ms 23892 KB n=100
6 Correct 12 ms 23892 KB n=100
7 Correct 12 ms 23892 KB n=100
8 Correct 12 ms 23940 KB n=100
9 Correct 13 ms 23940 KB n=100
10 Correct 13 ms 23940 KB n=100
11 Correct 12 ms 23892 KB n=100
12 Correct 12 ms 23944 KB n=100
13 Correct 12 ms 23892 KB n=100
14 Correct 12 ms 23892 KB n=100
15 Correct 12 ms 23892 KB n=100
16 Correct 11 ms 23940 KB n=100
17 Correct 12 ms 23944 KB n=100
18 Correct 12 ms 23944 KB n=100
19 Correct 12 ms 23984 KB n=100
20 Correct 12 ms 23944 KB n=100
21 Correct 12 ms 23924 KB n=100
22 Correct 12 ms 23988 KB n=100
23 Correct 15 ms 23892 KB n=100
24 Correct 14 ms 23892 KB n=100
25 Correct 13 ms 23948 KB n=100
26 Correct 12 ms 23892 KB n=12
27 Correct 12 ms 23892 KB n=100
28 Correct 17 ms 24020 KB n=500
29 Correct 13 ms 24080 KB n=500
30 Correct 14 ms 24084 KB n=500
31 Correct 13 ms 24040 KB n=500
32 Correct 13 ms 24020 KB n=500
33 Correct 12 ms 24020 KB n=500
34 Correct 13 ms 24112 KB n=500
35 Correct 16 ms 24148 KB n=500
36 Correct 15 ms 24080 KB n=500
37 Correct 12 ms 24020 KB n=500
38 Correct 12 ms 24104 KB n=500
39 Correct 15 ms 24088 KB n=500
40 Correct 13 ms 24136 KB n=500
41 Correct 12 ms 24084 KB n=500
42 Correct 16 ms 24032 KB n=500
43 Correct 13 ms 24044 KB n=500
44 Correct 15 ms 24020 KB n=500
45 Correct 13 ms 24080 KB n=500
46 Correct 13 ms 24020 KB n=500
47 Correct 12 ms 24020 KB n=500
48 Correct 13 ms 24108 KB n=500
49 Correct 13 ms 24148 KB n=500
50 Correct 17 ms 24052 KB n=500
51 Correct 14 ms 24020 KB n=500
52 Correct 15 ms 24148 KB n=500
53 Correct 16 ms 24020 KB n=500
54 Correct 17 ms 24028 KB n=500
55 Correct 15 ms 24020 KB n=278
56 Correct 13 ms 24148 KB n=500
57 Correct 13 ms 24148 KB n=500
58 Correct 14 ms 24080 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23968 KB n=5
2 Correct 14 ms 23892 KB n=100
3 Correct 12 ms 23984 KB n=100
4 Correct 12 ms 23944 KB n=100
5 Correct 12 ms 23892 KB n=100
6 Correct 12 ms 23892 KB n=100
7 Correct 12 ms 23892 KB n=100
8 Correct 12 ms 23940 KB n=100
9 Correct 13 ms 23940 KB n=100
10 Correct 13 ms 23940 KB n=100
11 Correct 12 ms 23892 KB n=100
12 Correct 12 ms 23944 KB n=100
13 Correct 12 ms 23892 KB n=100
14 Correct 12 ms 23892 KB n=100
15 Correct 12 ms 23892 KB n=100
16 Correct 11 ms 23940 KB n=100
17 Correct 12 ms 23944 KB n=100
18 Correct 12 ms 23944 KB n=100
19 Correct 12 ms 23984 KB n=100
20 Correct 12 ms 23944 KB n=100
21 Correct 12 ms 23924 KB n=100
22 Correct 12 ms 23988 KB n=100
23 Correct 15 ms 23892 KB n=100
24 Correct 14 ms 23892 KB n=100
25 Correct 13 ms 23948 KB n=100
26 Correct 12 ms 23892 KB n=12
27 Correct 12 ms 23892 KB n=100
28 Correct 17 ms 24020 KB n=500
29 Correct 13 ms 24080 KB n=500
30 Correct 14 ms 24084 KB n=500
31 Correct 13 ms 24040 KB n=500
32 Correct 13 ms 24020 KB n=500
33 Correct 12 ms 24020 KB n=500
34 Correct 13 ms 24112 KB n=500
35 Correct 16 ms 24148 KB n=500
36 Correct 15 ms 24080 KB n=500
37 Correct 12 ms 24020 KB n=500
38 Correct 12 ms 24104 KB n=500
39 Correct 15 ms 24088 KB n=500
40 Correct 13 ms 24136 KB n=500
41 Correct 12 ms 24084 KB n=500
42 Correct 16 ms 24032 KB n=500
43 Correct 13 ms 24044 KB n=500
44 Correct 15 ms 24020 KB n=500
45 Correct 13 ms 24080 KB n=500
46 Correct 13 ms 24020 KB n=500
47 Correct 12 ms 24020 KB n=500
48 Correct 13 ms 24108 KB n=500
49 Correct 13 ms 24148 KB n=500
50 Correct 17 ms 24052 KB n=500
51 Correct 14 ms 24020 KB n=500
52 Correct 15 ms 24148 KB n=500
53 Correct 16 ms 24020 KB n=500
54 Correct 17 ms 24028 KB n=500
55 Correct 15 ms 24020 KB n=278
56 Correct 13 ms 24148 KB n=500
57 Correct 13 ms 24148 KB n=500
58 Correct 14 ms 24080 KB n=500
59 Correct 16 ms 24576 KB n=2000
60 Correct 17 ms 24652 KB n=2000
61 Correct 17 ms 24660 KB n=2000
62 Correct 18 ms 24532 KB n=2000
63 Correct 16 ms 24660 KB n=2000
64 Correct 16 ms 24548 KB n=2000
65 Correct 18 ms 24600 KB n=2000
66 Correct 18 ms 24588 KB n=2000
67 Correct 16 ms 24636 KB n=2000
68 Correct 16 ms 24552 KB n=2000
69 Correct 15 ms 24592 KB n=2000
70 Correct 15 ms 24660 KB n=2000
71 Correct 15 ms 24556 KB n=2000
72 Correct 14 ms 24588 KB n=2000
73 Correct 14 ms 24584 KB n=2000
74 Correct 18 ms 24532 KB n=1844
75 Correct 14 ms 24624 KB n=2000
76 Correct 17 ms 24592 KB n=2000
77 Correct 19 ms 24596 KB n=2000
78 Correct 17 ms 24596 KB n=2000
79 Correct 16 ms 24532 KB n=2000
80 Correct 15 ms 24612 KB n=2000
81 Correct 17 ms 24580 KB n=2000
82 Correct 18 ms 24560 KB n=2000
83 Correct 16 ms 24676 KB n=2000
84 Correct 17 ms 24660 KB n=2000
85 Correct 16 ms 24580 KB n=2000
86 Correct 17 ms 24688 KB n=2000
87 Correct 17 ms 24556 KB n=2000
88 Correct 16 ms 24600 KB n=2000
89 Correct 15 ms 24660 KB n=2000
90 Correct 15 ms 24688 KB n=2000
91 Correct 15 ms 24632 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23968 KB n=5
2 Correct 14 ms 23892 KB n=100
3 Correct 12 ms 23984 KB n=100
4 Correct 12 ms 23944 KB n=100
5 Correct 12 ms 23892 KB n=100
6 Correct 12 ms 23892 KB n=100
7 Correct 12 ms 23892 KB n=100
8 Correct 12 ms 23940 KB n=100
9 Correct 13 ms 23940 KB n=100
10 Correct 13 ms 23940 KB n=100
11 Correct 12 ms 23892 KB n=100
12 Correct 12 ms 23944 KB n=100
13 Correct 12 ms 23892 KB n=100
14 Correct 12 ms 23892 KB n=100
15 Correct 12 ms 23892 KB n=100
16 Correct 11 ms 23940 KB n=100
17 Correct 12 ms 23944 KB n=100
18 Correct 12 ms 23944 KB n=100
19 Correct 12 ms 23984 KB n=100
20 Correct 12 ms 23944 KB n=100
21 Correct 12 ms 23924 KB n=100
22 Correct 12 ms 23988 KB n=100
23 Correct 15 ms 23892 KB n=100
24 Correct 14 ms 23892 KB n=100
25 Correct 13 ms 23948 KB n=100
26 Correct 12 ms 23892 KB n=12
27 Correct 12 ms 23892 KB n=100
28 Correct 17 ms 24020 KB n=500
29 Correct 13 ms 24080 KB n=500
30 Correct 14 ms 24084 KB n=500
31 Correct 13 ms 24040 KB n=500
32 Correct 13 ms 24020 KB n=500
33 Correct 12 ms 24020 KB n=500
34 Correct 13 ms 24112 KB n=500
35 Correct 16 ms 24148 KB n=500
36 Correct 15 ms 24080 KB n=500
37 Correct 12 ms 24020 KB n=500
38 Correct 12 ms 24104 KB n=500
39 Correct 15 ms 24088 KB n=500
40 Correct 13 ms 24136 KB n=500
41 Correct 12 ms 24084 KB n=500
42 Correct 16 ms 24032 KB n=500
43 Correct 13 ms 24044 KB n=500
44 Correct 15 ms 24020 KB n=500
45 Correct 13 ms 24080 KB n=500
46 Correct 13 ms 24020 KB n=500
47 Correct 12 ms 24020 KB n=500
48 Correct 13 ms 24108 KB n=500
49 Correct 13 ms 24148 KB n=500
50 Correct 17 ms 24052 KB n=500
51 Correct 14 ms 24020 KB n=500
52 Correct 15 ms 24148 KB n=500
53 Correct 16 ms 24020 KB n=500
54 Correct 17 ms 24028 KB n=500
55 Correct 15 ms 24020 KB n=278
56 Correct 13 ms 24148 KB n=500
57 Correct 13 ms 24148 KB n=500
58 Correct 14 ms 24080 KB n=500
59 Correct 16 ms 24576 KB n=2000
60 Correct 17 ms 24652 KB n=2000
61 Correct 17 ms 24660 KB n=2000
62 Correct 18 ms 24532 KB n=2000
63 Correct 16 ms 24660 KB n=2000
64 Correct 16 ms 24548 KB n=2000
65 Correct 18 ms 24600 KB n=2000
66 Correct 18 ms 24588 KB n=2000
67 Correct 16 ms 24636 KB n=2000
68 Correct 16 ms 24552 KB n=2000
69 Correct 15 ms 24592 KB n=2000
70 Correct 15 ms 24660 KB n=2000
71 Correct 15 ms 24556 KB n=2000
72 Correct 14 ms 24588 KB n=2000
73 Correct 14 ms 24584 KB n=2000
74 Correct 18 ms 24532 KB n=1844
75 Correct 14 ms 24624 KB n=2000
76 Correct 17 ms 24592 KB n=2000
77 Correct 19 ms 24596 KB n=2000
78 Correct 17 ms 24596 KB n=2000
79 Correct 16 ms 24532 KB n=2000
80 Correct 15 ms 24612 KB n=2000
81 Correct 17 ms 24580 KB n=2000
82 Correct 18 ms 24560 KB n=2000
83 Correct 16 ms 24676 KB n=2000
84 Correct 17 ms 24660 KB n=2000
85 Correct 16 ms 24580 KB n=2000
86 Correct 17 ms 24688 KB n=2000
87 Correct 17 ms 24556 KB n=2000
88 Correct 16 ms 24600 KB n=2000
89 Correct 15 ms 24660 KB n=2000
90 Correct 15 ms 24688 KB n=2000
91 Correct 15 ms 24632 KB n=2000
92 Correct 1887 ms 87996 KB n=200000
93 Correct 2089 ms 90304 KB n=200000
94 Correct 1839 ms 92008 KB n=200000
95 Correct 1852 ms 87872 KB n=200000
96 Correct 1851 ms 87916 KB n=200000
97 Correct 2183 ms 89900 KB n=200000
98 Correct 2037 ms 87908 KB n=200000
99 Correct 2337 ms 87628 KB n=200000
100 Correct 1944 ms 87960 KB n=200000
101 Correct 1900 ms 92448 KB n=200000
102 Correct 904 ms 89096 KB n=200000
103 Correct 881 ms 88972 KB n=200000
104 Correct 889 ms 89152 KB n=200000
105 Correct 883 ms 88760 KB n=200000
106 Correct 857 ms 88780 KB n=200000
107 Correct 876 ms 88716 KB n=200000
108 Correct 2246 ms 87792 KB n=200000
109 Correct 2283 ms 87704 KB n=200000
110 Correct 2238 ms 88008 KB n=200000
111 Correct 1589 ms 87896 KB n=200000
112 Correct 1926 ms 92056 KB n=200000
113 Correct 2106 ms 89676 KB n=200000
114 Correct 1588 ms 88008 KB n=200000
115 Correct 2454 ms 88504 KB n=200000
116 Correct 1873 ms 87576 KB n=200000
117 Correct 1814 ms 92148 KB n=200000
118 Correct 2277 ms 88864 KB n=200000
119 Correct 2001 ms 87440 KB n=200000
120 Correct 1750 ms 92160 KB n=200000
121 Correct 1677 ms 92080 KB n=200000
122 Correct 1773 ms 92528 KB n=200000
123 Correct 892 ms 88328 KB n=200000
124 Correct 307 ms 39556 KB n=25264