Submission #84474

# Submission time Handle Problem Language Result Execution time Memory
84474 2018-11-15T11:22:40 Z inom Birthday gift (IZhO18_treearray) C++14
56 / 100
1610 ms 108896 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int MX = 200 * 1000 + 7;
const int LG = 18;

vector<int> g[MX];
int tin[MX], tout[MX];
int timer = 1;
int p[MX][LG];
int a[MX];
set<int> x[MX], y[MX];

void dfs(int v = 1, int par = 1) {
    tin[v] = timer;
    timer++;
    p[v][0] = par;
    for (int i = 1; i < LG; i++) {
        p[v][i] = p[p[v][i - 1]][i - 1];
    }
    for (int to : g[v]) {
        if (to != par) {
            dfs(to, v);
        }
        
    }
    tout[v] = timer;
    timer++;
}

bool isAncestor(int u, int v) {
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}

int getLCA(int u, int v) {
    if (isAncestor(u, v)) {
        return u;
    } else if (isAncestor(v, u)) {
        return v;
    } else {
        for (int i = LG - 1; i >= 0; i--) {
            if (!isAncestor(p[u][i], v)) {
                u = p[u][i];
            }
        }
        return p[u][0];
    }
}

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    int n, k, q;
    scanf("%d %d %d", &n, &k, &q);
    for (int i = 1; i <= n - 1; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) {
        x[i].insert(k + 1);
        y[i].insert(k + 1);
    }
    dfs();
    for (int i = 1; i <= k; i++) {
        scanf("%d", &a[i]);
        x[a[i]].insert(i);
        if (i > 1) {
            y[getLCA(a[i - 1], a[i])].insert(i - 1);
        }
    }
    for (int i = 0; i < q; i++) {
        int t;
        scanf("%d", &t);
        if (t == 1) {
            int pos, v;
            scanf("%d %d", &pos, &v);
            x[a[pos]].erase(pos);
            if (pos > 1) {
                y[getLCA(a[pos - 1], a[pos])].erase(pos - 1);
            }
            if (pos + 1 <= n) {
                y[getLCA(a[pos], a[pos + 1])].erase(pos);
            }
            a[pos] = v;
            x[a[pos]].insert(pos);
            if (pos > 1) {
                y[getLCA(a[pos - 1], a[pos])].insert(pos - 1);
            }
            if (pos + 1 <= n) {
                y[getLCA(a[pos], a[pos + 1])].insert(pos);
            }
        } else {
            int l, r, v;
            scanf("%d %d %d", &l, &r, &v);
            int p1 = *x[v].lower_bound(l), p2 = *y[v].lower_bound(l);
            if (p1 <= r) {
                printf("%d %d\n", p1, p1);
            } else if (p2 + 1 <= r) {
                printf("%d %d\n", p2, p2 + 1);
            } else {
                printf("-1 -1\n");
            }
        }
    }
}


Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d", &n, &k, &q);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:62:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &u, &v);
         ~~~~~^~~~~~~~~~~~~~~~~
treearray.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &a[i]);
         ~~~~~^~~~~~~~~~~~~
treearray.cpp:80:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &t);
         ~~~~~^~~~~~~~~~
treearray.cpp:83:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d", &pos, &v);
             ~~~~~^~~~~~~~~~~~~~~~~~~
treearray.cpp:101:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d %d", &l, &r, &v);
             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 22 ms 23952 KB n=100
3 Correct 23 ms 23960 KB n=100
4 Correct 23 ms 23960 KB n=100
5 Correct 24 ms 24012 KB n=100
6 Correct 22 ms 24056 KB n=100
7 Correct 22 ms 24056 KB n=100
8 Correct 24 ms 24096 KB n=100
9 Correct 23 ms 24096 KB n=100
10 Correct 26 ms 24096 KB n=100
11 Correct 23 ms 24108 KB n=100
12 Correct 23 ms 24188 KB n=100
13 Correct 24 ms 24188 KB n=100
14 Correct 24 ms 24224 KB n=100
15 Correct 23 ms 24224 KB n=100
16 Correct 24 ms 24304 KB n=100
17 Correct 23 ms 24304 KB n=100
18 Correct 23 ms 24304 KB n=100
19 Correct 24 ms 24304 KB n=100
20 Correct 23 ms 24304 KB n=100
21 Correct 24 ms 24304 KB n=100
22 Correct 28 ms 24304 KB n=100
23 Correct 23 ms 24304 KB n=100
24 Correct 23 ms 24304 KB n=100
25 Correct 23 ms 24304 KB n=100
26 Correct 23 ms 24304 KB n=12
27 Correct 23 ms 24304 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 22 ms 23952 KB n=100
3 Correct 23 ms 23960 KB n=100
4 Correct 23 ms 23960 KB n=100
5 Correct 24 ms 24012 KB n=100
6 Correct 22 ms 24056 KB n=100
7 Correct 22 ms 24056 KB n=100
8 Correct 24 ms 24096 KB n=100
9 Correct 23 ms 24096 KB n=100
10 Correct 26 ms 24096 KB n=100
11 Correct 23 ms 24108 KB n=100
12 Correct 23 ms 24188 KB n=100
13 Correct 24 ms 24188 KB n=100
14 Correct 24 ms 24224 KB n=100
15 Correct 23 ms 24224 KB n=100
16 Correct 24 ms 24304 KB n=100
17 Correct 23 ms 24304 KB n=100
18 Correct 23 ms 24304 KB n=100
19 Correct 24 ms 24304 KB n=100
20 Correct 23 ms 24304 KB n=100
21 Correct 24 ms 24304 KB n=100
22 Correct 28 ms 24304 KB n=100
23 Correct 23 ms 24304 KB n=100
24 Correct 23 ms 24304 KB n=100
25 Correct 23 ms 24304 KB n=100
26 Correct 23 ms 24304 KB n=12
27 Correct 23 ms 24304 KB n=100
28 Correct 24 ms 24304 KB n=500
29 Correct 23 ms 24420 KB n=500
30 Correct 24 ms 24420 KB n=500
31 Correct 24 ms 24452 KB n=500
32 Correct 23 ms 24468 KB n=500
33 Correct 25 ms 24468 KB n=500
34 Correct 23 ms 24468 KB n=500
35 Correct 24 ms 24468 KB n=500
36 Correct 23 ms 24468 KB n=500
37 Correct 23 ms 24468 KB n=500
38 Correct 23 ms 24468 KB n=500
39 Correct 23 ms 24468 KB n=500
40 Correct 23 ms 24468 KB n=500
41 Correct 26 ms 24480 KB n=500
42 Correct 24 ms 24496 KB n=500
43 Correct 25 ms 24508 KB n=500
44 Correct 25 ms 24520 KB n=500
45 Correct 24 ms 24536 KB n=500
46 Correct 24 ms 24672 KB n=500
47 Correct 23 ms 24688 KB n=500
48 Correct 25 ms 24688 KB n=500
49 Correct 24 ms 24716 KB n=500
50 Correct 25 ms 24716 KB n=500
51 Correct 23 ms 24716 KB n=500
52 Correct 24 ms 24760 KB n=500
53 Correct 26 ms 24760 KB n=500
54 Correct 24 ms 24760 KB n=500
55 Correct 24 ms 24760 KB n=278
56 Correct 23 ms 24760 KB n=500
57 Correct 24 ms 24760 KB n=500
58 Correct 24 ms 24836 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 22 ms 23952 KB n=100
3 Correct 23 ms 23960 KB n=100
4 Correct 23 ms 23960 KB n=100
5 Correct 24 ms 24012 KB n=100
6 Correct 22 ms 24056 KB n=100
7 Correct 22 ms 24056 KB n=100
8 Correct 24 ms 24096 KB n=100
9 Correct 23 ms 24096 KB n=100
10 Correct 26 ms 24096 KB n=100
11 Correct 23 ms 24108 KB n=100
12 Correct 23 ms 24188 KB n=100
13 Correct 24 ms 24188 KB n=100
14 Correct 24 ms 24224 KB n=100
15 Correct 23 ms 24224 KB n=100
16 Correct 24 ms 24304 KB n=100
17 Correct 23 ms 24304 KB n=100
18 Correct 23 ms 24304 KB n=100
19 Correct 24 ms 24304 KB n=100
20 Correct 23 ms 24304 KB n=100
21 Correct 24 ms 24304 KB n=100
22 Correct 28 ms 24304 KB n=100
23 Correct 23 ms 24304 KB n=100
24 Correct 23 ms 24304 KB n=100
25 Correct 23 ms 24304 KB n=100
26 Correct 23 ms 24304 KB n=12
27 Correct 23 ms 24304 KB n=100
28 Correct 24 ms 24304 KB n=500
29 Correct 23 ms 24420 KB n=500
30 Correct 24 ms 24420 KB n=500
31 Correct 24 ms 24452 KB n=500
32 Correct 23 ms 24468 KB n=500
33 Correct 25 ms 24468 KB n=500
34 Correct 23 ms 24468 KB n=500
35 Correct 24 ms 24468 KB n=500
36 Correct 23 ms 24468 KB n=500
37 Correct 23 ms 24468 KB n=500
38 Correct 23 ms 24468 KB n=500
39 Correct 23 ms 24468 KB n=500
40 Correct 23 ms 24468 KB n=500
41 Correct 26 ms 24480 KB n=500
42 Correct 24 ms 24496 KB n=500
43 Correct 25 ms 24508 KB n=500
44 Correct 25 ms 24520 KB n=500
45 Correct 24 ms 24536 KB n=500
46 Correct 24 ms 24672 KB n=500
47 Correct 23 ms 24688 KB n=500
48 Correct 25 ms 24688 KB n=500
49 Correct 24 ms 24716 KB n=500
50 Correct 25 ms 24716 KB n=500
51 Correct 23 ms 24716 KB n=500
52 Correct 24 ms 24760 KB n=500
53 Correct 26 ms 24760 KB n=500
54 Correct 24 ms 24760 KB n=500
55 Correct 24 ms 24760 KB n=278
56 Correct 23 ms 24760 KB n=500
57 Correct 24 ms 24760 KB n=500
58 Correct 24 ms 24836 KB n=500
59 Correct 30 ms 25236 KB n=2000
60 Correct 28 ms 25416 KB n=2000
61 Correct 27 ms 25420 KB n=2000
62 Correct 28 ms 25432 KB n=2000
63 Correct 28 ms 25488 KB n=2000
64 Correct 29 ms 25556 KB n=2000
65 Correct 28 ms 25612 KB n=2000
66 Correct 27 ms 25820 KB n=2000
67 Correct 28 ms 25820 KB n=2000
68 Correct 28 ms 25836 KB n=2000
69 Correct 27 ms 25848 KB n=2000
70 Correct 26 ms 25900 KB n=2000
71 Correct 27 ms 25952 KB n=2000
72 Correct 26 ms 26004 KB n=2000
73 Correct 27 ms 26124 KB n=2000
74 Correct 35 ms 26124 KB n=1844
75 Correct 25 ms 26124 KB n=2000
76 Correct 27 ms 26124 KB n=2000
77 Correct 28 ms 26124 KB n=2000
78 Correct 27 ms 26124 KB n=2000
79 Correct 28 ms 26124 KB n=2000
80 Correct 27 ms 26124 KB n=2000
81 Correct 28 ms 26124 KB n=2000
82 Correct 28 ms 26124 KB n=2000
83 Correct 26 ms 26164 KB n=2000
84 Correct 27 ms 26164 KB n=2000
85 Correct 28 ms 26164 KB n=2000
86 Correct 30 ms 26164 KB n=2000
87 Correct 28 ms 26164 KB n=2000
88 Correct 26 ms 26284 KB n=2000
89 Correct 26 ms 26284 KB n=2000
90 Correct 26 ms 26284 KB n=2000
91 Correct 26 ms 26284 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 22 ms 23952 KB n=100
3 Correct 23 ms 23960 KB n=100
4 Correct 23 ms 23960 KB n=100
5 Correct 24 ms 24012 KB n=100
6 Correct 22 ms 24056 KB n=100
7 Correct 22 ms 24056 KB n=100
8 Correct 24 ms 24096 KB n=100
9 Correct 23 ms 24096 KB n=100
10 Correct 26 ms 24096 KB n=100
11 Correct 23 ms 24108 KB n=100
12 Correct 23 ms 24188 KB n=100
13 Correct 24 ms 24188 KB n=100
14 Correct 24 ms 24224 KB n=100
15 Correct 23 ms 24224 KB n=100
16 Correct 24 ms 24304 KB n=100
17 Correct 23 ms 24304 KB n=100
18 Correct 23 ms 24304 KB n=100
19 Correct 24 ms 24304 KB n=100
20 Correct 23 ms 24304 KB n=100
21 Correct 24 ms 24304 KB n=100
22 Correct 28 ms 24304 KB n=100
23 Correct 23 ms 24304 KB n=100
24 Correct 23 ms 24304 KB n=100
25 Correct 23 ms 24304 KB n=100
26 Correct 23 ms 24304 KB n=12
27 Correct 23 ms 24304 KB n=100
28 Correct 24 ms 24304 KB n=500
29 Correct 23 ms 24420 KB n=500
30 Correct 24 ms 24420 KB n=500
31 Correct 24 ms 24452 KB n=500
32 Correct 23 ms 24468 KB n=500
33 Correct 25 ms 24468 KB n=500
34 Correct 23 ms 24468 KB n=500
35 Correct 24 ms 24468 KB n=500
36 Correct 23 ms 24468 KB n=500
37 Correct 23 ms 24468 KB n=500
38 Correct 23 ms 24468 KB n=500
39 Correct 23 ms 24468 KB n=500
40 Correct 23 ms 24468 KB n=500
41 Correct 26 ms 24480 KB n=500
42 Correct 24 ms 24496 KB n=500
43 Correct 25 ms 24508 KB n=500
44 Correct 25 ms 24520 KB n=500
45 Correct 24 ms 24536 KB n=500
46 Correct 24 ms 24672 KB n=500
47 Correct 23 ms 24688 KB n=500
48 Correct 25 ms 24688 KB n=500
49 Correct 24 ms 24716 KB n=500
50 Correct 25 ms 24716 KB n=500
51 Correct 23 ms 24716 KB n=500
52 Correct 24 ms 24760 KB n=500
53 Correct 26 ms 24760 KB n=500
54 Correct 24 ms 24760 KB n=500
55 Correct 24 ms 24760 KB n=278
56 Correct 23 ms 24760 KB n=500
57 Correct 24 ms 24760 KB n=500
58 Correct 24 ms 24836 KB n=500
59 Correct 30 ms 25236 KB n=2000
60 Correct 28 ms 25416 KB n=2000
61 Correct 27 ms 25420 KB n=2000
62 Correct 28 ms 25432 KB n=2000
63 Correct 28 ms 25488 KB n=2000
64 Correct 29 ms 25556 KB n=2000
65 Correct 28 ms 25612 KB n=2000
66 Correct 27 ms 25820 KB n=2000
67 Correct 28 ms 25820 KB n=2000
68 Correct 28 ms 25836 KB n=2000
69 Correct 27 ms 25848 KB n=2000
70 Correct 26 ms 25900 KB n=2000
71 Correct 27 ms 25952 KB n=2000
72 Correct 26 ms 26004 KB n=2000
73 Correct 27 ms 26124 KB n=2000
74 Correct 35 ms 26124 KB n=1844
75 Correct 25 ms 26124 KB n=2000
76 Correct 27 ms 26124 KB n=2000
77 Correct 28 ms 26124 KB n=2000
78 Correct 27 ms 26124 KB n=2000
79 Correct 28 ms 26124 KB n=2000
80 Correct 27 ms 26124 KB n=2000
81 Correct 28 ms 26124 KB n=2000
82 Correct 28 ms 26124 KB n=2000
83 Correct 26 ms 26164 KB n=2000
84 Correct 27 ms 26164 KB n=2000
85 Correct 28 ms 26164 KB n=2000
86 Correct 30 ms 26164 KB n=2000
87 Correct 28 ms 26164 KB n=2000
88 Correct 26 ms 26284 KB n=2000
89 Correct 26 ms 26284 KB n=2000
90 Correct 26 ms 26284 KB n=2000
91 Correct 26 ms 26284 KB n=2000
92 Correct 1196 ms 87760 KB n=200000
93 Correct 1262 ms 91656 KB n=200000
94 Correct 1061 ms 97396 KB n=200000
95 Correct 1242 ms 97396 KB n=200000
96 Correct 1211 ms 97796 KB n=200000
97 Correct 1379 ms 100624 KB n=200000
98 Correct 1170 ms 100624 KB n=200000
99 Correct 1311 ms 100624 KB n=200000
100 Correct 1204 ms 100624 KB n=200000
101 Correct 973 ms 106320 KB n=200000
102 Correct 678 ms 106320 KB n=200000
103 Correct 728 ms 106320 KB n=200000
104 Correct 674 ms 106320 KB n=200000
105 Correct 698 ms 106320 KB n=200000
106 Correct 692 ms 106320 KB n=200000
107 Correct 733 ms 106320 KB n=200000
108 Correct 1190 ms 106320 KB n=200000
109 Correct 1220 ms 106320 KB n=200000
110 Correct 1177 ms 106320 KB n=200000
111 Correct 1217 ms 106320 KB n=200000
112 Correct 958 ms 106320 KB n=200000
113 Correct 1282 ms 106320 KB n=200000
114 Correct 1194 ms 106320 KB n=200000
115 Correct 1610 ms 106320 KB n=200000
116 Correct 1175 ms 106320 KB n=200000
117 Correct 940 ms 108008 KB n=200000
118 Correct 1428 ms 108008 KB n=200000
119 Correct 1160 ms 108008 KB n=200000
120 Correct 828 ms 108300 KB n=200000
121 Correct 830 ms 108432 KB n=200000
122 Correct 908 ms 108896 KB n=200000
123 Correct 731 ms 108896 KB n=200000
124 Incorrect 264 ms 108896 KB Wrong range.