Submission #93219

# Submission time Handle Problem Language Result Execution time Memory
93219 2019-01-07T09:58:03 Z SamAnd Birthday gift (IZhO18_treearray) C++17
100 / 100
1399 ms 86040 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;

int n, m, q;
vector<int> a[N];
int b[N];

int k = 20;
int p[N][22];
int tim;
int tin[N], tout[N];
void dfs(int x, int cp)
{
    tin[x] = tim++;
    p[x][0] = cp;
    for (int i = 1; i <= k; ++i)
    {
        p[x][i] = p[p[x][i - 1]][i - 1];
    }
    for (int i = 0; i < a[x].size(); ++i)
    {
        int h = a[x][i];
        if (h == cp)
            continue;
        dfs(h, x);
    }
    tout[x] = tim++;
}

bool par(int x, int y)
{
    return (tin[x] <= tin[y] && tout[x] >= tout[y]);
}

int lca(int x, int y)
{
    if (par(x, y))
        return x;
    if (par(y, x))
        return y;
    for (int i = k; i >= 0; --i)
    {
        if (!par(p[x][i], y))
            x = p[x][i];
    }
    return p[x][0];
}

set<int> s[N], t[N];
void solv()
{
    scanf("%d%d%d", &n, &m, &q);
    for (int i = 1; i <= n; ++i)
    {
        a[i].clear();
        s[i].clear();
        t[i].clear();
    }
    for (int i = 0; i < n - 1; ++i)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        a[x].push_back(y);
        a[y].push_back(x);
    }
    for (int i = 1; i <= m; ++i)
        scanf("%d", &b[i]);
    dfs(1, 1);
    for (int i = 1; i < m; ++i)
    {
        s[lca(b[i], b[i + 1])].insert(i);
    }
    for (int i = 1; i <= m; ++i)
    {
        t[b[i]].insert(i);
    }
    while (q--)
    {
        int ty;
        scanf("%d", &ty);
        if (ty == 1)
        {
            int i, x;
            scanf("%d%d", &i, &x);
            if (m == 1)
            {
                b[i] = x;
                continue;
            }
            if (i == 1)
            {
                s[lca(b[i], b[i + 1])].erase(i);
                t[b[i]].erase(i);
            }
            else if (i == m)
            {
                s[lca(b[i - 1], b[i])].erase(i - 1);
                t[b[i]].erase(i);
            }
            else
            {
                s[lca(b[i], b[i + 1])].erase(i);
                s[lca(b[i - 1], b[i])].erase(i - 1);
                t[b[i]].erase(i);
            }
            b[i] = x;
            if (i == 1)
            {
                s[lca(b[i], b[i + 1])].insert(i);
                t[b[i]].insert(i);
            }
            else if (i == m)
            {
                s[lca(b[i - 1], b[i])].insert(i - 1);
                t[b[i]].insert(i);
            }
            else
            {
                s[lca(b[i], b[i + 1])].insert(i);
                s[lca(b[i - 1], b[i])].insert(i - 1);
                t[b[i]].insert(i);
            }
        }
        else
        {
            int l, r, x;
            scanf("%d%d%d", &l, &r, &x);
            if (m == 1)
            {
                if (x == b[m])
                    printf("1 1\n");
                else
                    printf("-1 -1\n");
                continue;
            }
            set<int>::iterator it1 = t[x].lower_bound(l);
            set<int>::iterator it = s[x].lower_bound(l);
            if (it1 != t[x].end() && *it1 <= r)
                printf("%d %d\n", *it1, *it1);
            else if (it != s[x].end() && *it + 1 <= r)
                printf("%d %d\n", *it, *it + 1);
            else
                printf("-1 -1\n");
        }
    }
}

int main()
{
    //freopen("input2.txt", "r", stdin);
    int tt = 1;
    while (tt--)
    {
        solv();
    }
    return 0;
}

Compilation message

treearray.cpp: In function 'void dfs(int, int)':
treearray.cpp:21:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < a[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
treearray.cpp: In function 'void solv()':
treearray.cpp:53:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d", &n, &m, &q);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:63:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &x, &y);
         ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &b[i]);
         ~~~~~^~~~~~~~~~~~~
treearray.cpp:81:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &ty);
         ~~~~~^~~~~~~~~~~
treearray.cpp:85:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d%d", &i, &x);
             ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:128:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d%d%d", &l, &r, &x);
             ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23928 KB n=5
2 Correct 23 ms 23672 KB n=100
3 Correct 19 ms 23672 KB n=100
4 Correct 22 ms 23800 KB n=100
5 Correct 19 ms 23800 KB n=100
6 Correct 19 ms 23800 KB n=100
7 Correct 19 ms 23800 KB n=100
8 Correct 18 ms 23800 KB n=100
9 Correct 14 ms 23800 KB n=100
10 Correct 22 ms 23800 KB n=100
11 Correct 22 ms 23800 KB n=100
12 Correct 22 ms 23804 KB n=100
13 Correct 20 ms 24184 KB n=100
14 Correct 19 ms 23804 KB n=100
15 Correct 19 ms 23800 KB n=100
16 Correct 19 ms 23800 KB n=100
17 Correct 19 ms 23800 KB n=100
18 Correct 19 ms 23800 KB n=100
19 Correct 18 ms 23800 KB n=100
20 Correct 19 ms 23800 KB n=100
21 Correct 19 ms 23772 KB n=100
22 Correct 18 ms 23804 KB n=100
23 Correct 19 ms 23804 KB n=100
24 Correct 21 ms 23928 KB n=100
25 Correct 22 ms 23800 KB n=100
26 Correct 22 ms 23800 KB n=12
27 Correct 19 ms 23800 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23928 KB n=5
2 Correct 23 ms 23672 KB n=100
3 Correct 19 ms 23672 KB n=100
4 Correct 22 ms 23800 KB n=100
5 Correct 19 ms 23800 KB n=100
6 Correct 19 ms 23800 KB n=100
7 Correct 19 ms 23800 KB n=100
8 Correct 18 ms 23800 KB n=100
9 Correct 14 ms 23800 KB n=100
10 Correct 22 ms 23800 KB n=100
11 Correct 22 ms 23800 KB n=100
12 Correct 22 ms 23804 KB n=100
13 Correct 20 ms 24184 KB n=100
14 Correct 19 ms 23804 KB n=100
15 Correct 19 ms 23800 KB n=100
16 Correct 19 ms 23800 KB n=100
17 Correct 19 ms 23800 KB n=100
18 Correct 19 ms 23800 KB n=100
19 Correct 18 ms 23800 KB n=100
20 Correct 19 ms 23800 KB n=100
21 Correct 19 ms 23772 KB n=100
22 Correct 18 ms 23804 KB n=100
23 Correct 19 ms 23804 KB n=100
24 Correct 21 ms 23928 KB n=100
25 Correct 22 ms 23800 KB n=100
26 Correct 22 ms 23800 KB n=12
27 Correct 19 ms 23800 KB n=100
28 Correct 23 ms 23928 KB n=500
29 Correct 24 ms 23928 KB n=500
30 Correct 23 ms 23916 KB n=500
31 Correct 23 ms 23928 KB n=500
32 Correct 24 ms 23900 KB n=500
33 Correct 24 ms 23928 KB n=500
34 Correct 20 ms 23928 KB n=500
35 Correct 19 ms 23928 KB n=500
36 Correct 19 ms 23928 KB n=500
37 Correct 19 ms 23928 KB n=500
38 Correct 20 ms 23928 KB n=500
39 Correct 19 ms 23928 KB n=500
40 Correct 20 ms 23928 KB n=500
41 Correct 19 ms 23928 KB n=500
42 Correct 20 ms 23928 KB n=500
43 Correct 20 ms 23928 KB n=500
44 Correct 20 ms 23928 KB n=500
45 Correct 20 ms 23920 KB n=500
46 Correct 19 ms 23928 KB n=500
47 Correct 19 ms 23928 KB n=500
48 Correct 19 ms 23928 KB n=500
49 Correct 20 ms 24028 KB n=500
50 Correct 19 ms 23928 KB n=500
51 Correct 19 ms 23928 KB n=500
52 Correct 23 ms 23992 KB n=500
53 Correct 20 ms 23900 KB n=500
54 Correct 22 ms 23928 KB n=500
55 Correct 23 ms 23928 KB n=278
56 Correct 20 ms 23928 KB n=500
57 Correct 23 ms 24056 KB n=500
58 Correct 24 ms 23928 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23928 KB n=5
2 Correct 23 ms 23672 KB n=100
3 Correct 19 ms 23672 KB n=100
4 Correct 22 ms 23800 KB n=100
5 Correct 19 ms 23800 KB n=100
6 Correct 19 ms 23800 KB n=100
7 Correct 19 ms 23800 KB n=100
8 Correct 18 ms 23800 KB n=100
9 Correct 14 ms 23800 KB n=100
10 Correct 22 ms 23800 KB n=100
11 Correct 22 ms 23800 KB n=100
12 Correct 22 ms 23804 KB n=100
13 Correct 20 ms 24184 KB n=100
14 Correct 19 ms 23804 KB n=100
15 Correct 19 ms 23800 KB n=100
16 Correct 19 ms 23800 KB n=100
17 Correct 19 ms 23800 KB n=100
18 Correct 19 ms 23800 KB n=100
19 Correct 18 ms 23800 KB n=100
20 Correct 19 ms 23800 KB n=100
21 Correct 19 ms 23772 KB n=100
22 Correct 18 ms 23804 KB n=100
23 Correct 19 ms 23804 KB n=100
24 Correct 21 ms 23928 KB n=100
25 Correct 22 ms 23800 KB n=100
26 Correct 22 ms 23800 KB n=12
27 Correct 19 ms 23800 KB n=100
28 Correct 23 ms 23928 KB n=500
29 Correct 24 ms 23928 KB n=500
30 Correct 23 ms 23916 KB n=500
31 Correct 23 ms 23928 KB n=500
32 Correct 24 ms 23900 KB n=500
33 Correct 24 ms 23928 KB n=500
34 Correct 20 ms 23928 KB n=500
35 Correct 19 ms 23928 KB n=500
36 Correct 19 ms 23928 KB n=500
37 Correct 19 ms 23928 KB n=500
38 Correct 20 ms 23928 KB n=500
39 Correct 19 ms 23928 KB n=500
40 Correct 20 ms 23928 KB n=500
41 Correct 19 ms 23928 KB n=500
42 Correct 20 ms 23928 KB n=500
43 Correct 20 ms 23928 KB n=500
44 Correct 20 ms 23928 KB n=500
45 Correct 20 ms 23920 KB n=500
46 Correct 19 ms 23928 KB n=500
47 Correct 19 ms 23928 KB n=500
48 Correct 19 ms 23928 KB n=500
49 Correct 20 ms 24028 KB n=500
50 Correct 19 ms 23928 KB n=500
51 Correct 19 ms 23928 KB n=500
52 Correct 23 ms 23992 KB n=500
53 Correct 20 ms 23900 KB n=500
54 Correct 22 ms 23928 KB n=500
55 Correct 23 ms 23928 KB n=278
56 Correct 20 ms 23928 KB n=500
57 Correct 23 ms 24056 KB n=500
58 Correct 24 ms 23928 KB n=500
59 Correct 26 ms 24312 KB n=2000
60 Correct 25 ms 24440 KB n=2000
61 Correct 26 ms 24440 KB n=2000
62 Correct 26 ms 24312 KB n=2000
63 Correct 28 ms 24284 KB n=2000
64 Correct 27 ms 24312 KB n=2000
65 Correct 26 ms 24316 KB n=2000
66 Correct 27 ms 24568 KB n=2000
67 Correct 28 ms 24312 KB n=2000
68 Correct 27 ms 24312 KB n=2000
69 Correct 26 ms 24312 KB n=2000
70 Correct 25 ms 24312 KB n=2000
71 Correct 25 ms 24312 KB n=2000
72 Correct 25 ms 24368 KB n=2000
73 Correct 26 ms 24312 KB n=2000
74 Correct 25 ms 24312 KB n=1844
75 Correct 24 ms 24312 KB n=2000
76 Correct 27 ms 24312 KB n=2000
77 Correct 27 ms 24312 KB n=2000
78 Correct 27 ms 24312 KB n=2000
79 Correct 27 ms 24316 KB n=2000
80 Correct 26 ms 24312 KB n=2000
81 Correct 27 ms 24312 KB n=2000
82 Correct 27 ms 24312 KB n=2000
83 Correct 27 ms 24444 KB n=2000
84 Correct 27 ms 24312 KB n=2000
85 Correct 26 ms 24312 KB n=2000
86 Correct 27 ms 24312 KB n=2000
87 Correct 27 ms 24312 KB n=2000
88 Correct 26 ms 24388 KB n=2000
89 Correct 26 ms 24440 KB n=2000
90 Correct 26 ms 24440 KB n=2000
91 Correct 25 ms 24312 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23928 KB n=5
2 Correct 23 ms 23672 KB n=100
3 Correct 19 ms 23672 KB n=100
4 Correct 22 ms 23800 KB n=100
5 Correct 19 ms 23800 KB n=100
6 Correct 19 ms 23800 KB n=100
7 Correct 19 ms 23800 KB n=100
8 Correct 18 ms 23800 KB n=100
9 Correct 14 ms 23800 KB n=100
10 Correct 22 ms 23800 KB n=100
11 Correct 22 ms 23800 KB n=100
12 Correct 22 ms 23804 KB n=100
13 Correct 20 ms 24184 KB n=100
14 Correct 19 ms 23804 KB n=100
15 Correct 19 ms 23800 KB n=100
16 Correct 19 ms 23800 KB n=100
17 Correct 19 ms 23800 KB n=100
18 Correct 19 ms 23800 KB n=100
19 Correct 18 ms 23800 KB n=100
20 Correct 19 ms 23800 KB n=100
21 Correct 19 ms 23772 KB n=100
22 Correct 18 ms 23804 KB n=100
23 Correct 19 ms 23804 KB n=100
24 Correct 21 ms 23928 KB n=100
25 Correct 22 ms 23800 KB n=100
26 Correct 22 ms 23800 KB n=12
27 Correct 19 ms 23800 KB n=100
28 Correct 23 ms 23928 KB n=500
29 Correct 24 ms 23928 KB n=500
30 Correct 23 ms 23916 KB n=500
31 Correct 23 ms 23928 KB n=500
32 Correct 24 ms 23900 KB n=500
33 Correct 24 ms 23928 KB n=500
34 Correct 20 ms 23928 KB n=500
35 Correct 19 ms 23928 KB n=500
36 Correct 19 ms 23928 KB n=500
37 Correct 19 ms 23928 KB n=500
38 Correct 20 ms 23928 KB n=500
39 Correct 19 ms 23928 KB n=500
40 Correct 20 ms 23928 KB n=500
41 Correct 19 ms 23928 KB n=500
42 Correct 20 ms 23928 KB n=500
43 Correct 20 ms 23928 KB n=500
44 Correct 20 ms 23928 KB n=500
45 Correct 20 ms 23920 KB n=500
46 Correct 19 ms 23928 KB n=500
47 Correct 19 ms 23928 KB n=500
48 Correct 19 ms 23928 KB n=500
49 Correct 20 ms 24028 KB n=500
50 Correct 19 ms 23928 KB n=500
51 Correct 19 ms 23928 KB n=500
52 Correct 23 ms 23992 KB n=500
53 Correct 20 ms 23900 KB n=500
54 Correct 22 ms 23928 KB n=500
55 Correct 23 ms 23928 KB n=278
56 Correct 20 ms 23928 KB n=500
57 Correct 23 ms 24056 KB n=500
58 Correct 24 ms 23928 KB n=500
59 Correct 26 ms 24312 KB n=2000
60 Correct 25 ms 24440 KB n=2000
61 Correct 26 ms 24440 KB n=2000
62 Correct 26 ms 24312 KB n=2000
63 Correct 28 ms 24284 KB n=2000
64 Correct 27 ms 24312 KB n=2000
65 Correct 26 ms 24316 KB n=2000
66 Correct 27 ms 24568 KB n=2000
67 Correct 28 ms 24312 KB n=2000
68 Correct 27 ms 24312 KB n=2000
69 Correct 26 ms 24312 KB n=2000
70 Correct 25 ms 24312 KB n=2000
71 Correct 25 ms 24312 KB n=2000
72 Correct 25 ms 24368 KB n=2000
73 Correct 26 ms 24312 KB n=2000
74 Correct 25 ms 24312 KB n=1844
75 Correct 24 ms 24312 KB n=2000
76 Correct 27 ms 24312 KB n=2000
77 Correct 27 ms 24312 KB n=2000
78 Correct 27 ms 24312 KB n=2000
79 Correct 27 ms 24316 KB n=2000
80 Correct 26 ms 24312 KB n=2000
81 Correct 27 ms 24312 KB n=2000
82 Correct 27 ms 24312 KB n=2000
83 Correct 27 ms 24444 KB n=2000
84 Correct 27 ms 24312 KB n=2000
85 Correct 26 ms 24312 KB n=2000
86 Correct 27 ms 24312 KB n=2000
87 Correct 27 ms 24312 KB n=2000
88 Correct 26 ms 24388 KB n=2000
89 Correct 26 ms 24440 KB n=2000
90 Correct 26 ms 24440 KB n=2000
91 Correct 25 ms 24312 KB n=2000
92 Correct 1109 ms 76944 KB n=200000
93 Correct 1179 ms 81268 KB n=200000
94 Correct 677 ms 84572 KB n=200000
95 Correct 1057 ms 76620 KB n=200000
96 Correct 854 ms 76904 KB n=200000
97 Correct 1123 ms 80360 KB n=200000
98 Correct 1074 ms 77036 KB n=200000
99 Correct 1232 ms 77016 KB n=200000
100 Correct 1081 ms 76912 KB n=200000
101 Correct 813 ms 86040 KB n=200000
102 Correct 570 ms 78072 KB n=200000
103 Correct 572 ms 77844 KB n=200000
104 Correct 570 ms 77804 KB n=200000
105 Correct 548 ms 77424 KB n=200000
106 Correct 520 ms 77560 KB n=200000
107 Correct 542 ms 77744 KB n=200000
108 Correct 954 ms 76312 KB n=200000
109 Correct 989 ms 76408 KB n=200000
110 Correct 958 ms 76344 KB n=200000
111 Correct 827 ms 76268 KB n=200000
112 Correct 650 ms 83824 KB n=200000
113 Correct 1032 ms 79268 KB n=200000
114 Correct 1054 ms 76316 KB n=200000
115 Correct 1399 ms 77432 KB n=200000
116 Correct 961 ms 76520 KB n=200000
117 Correct 610 ms 84344 KB n=200000
118 Correct 1047 ms 78200 KB n=200000
119 Correct 842 ms 76400 KB n=200000
120 Correct 541 ms 84216 KB n=200000
121 Correct 727 ms 84188 KB n=200000
122 Correct 718 ms 85120 KB n=200000
123 Correct 482 ms 76664 KB n=200000
124 Correct 215 ms 39040 KB n=25264