Submission #86782

# Submission time Handle Problem Language Result Execution time Memory
86782 2018-11-27T12:29:43 Z inom Birthday gift (IZhO18_treearray) C++14
100 / 100
1442 ms 242552 KB
#include<bits/stdc++.h>

#define pb push_back

using namespace std;

#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")
#pragma warning(disable : 4996)

const int N = 2 * 1e5 + 10;

int n, m, q;
int node[N];
vector<int> verr[N];
int l = 1, timer = 1;
int tin[N], tout[N];
int up[N][20];
set<int> lca[N], pos[N];

void dfs(int x, int pr = 1) {
    tin[x] = timer; timer++;
    up[x][0] = pr;
    for (int i = 1; i <= l; i++) {
        up[x][i] = up[up[x][i - 1]][i - 1];
    }
    for (auto i: verr[x]) {
        if (i != pr) {
            dfs(i, x);
        }
    }
    tout[x] = timer; timer++;
}

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

int get_lca(int x, int y) {
    if (upper(x, y)) {
        return x;
    }
    else if (upper(y, x)) {
        return y;
    }
    else {
        for (int i = l; i >= 0; i--) {
            if (!upper(up[x][i], y)) {
                x = up[x][i];
            }
        }
        return up[x][0];
    }
}

int main() {
    scanf("%d %d %d", &n, &m, &q);
    for (int i = 1; i <= n - 1; i++) {
        int x, y;
        scanf("%d %d", &x, &y);
        verr[x].pb(y); verr[y].pb(x);
    }
    for (int i = 1; i <= n; i++) {
        lca[i].insert(m + 1); pos[i].insert(m + 1);
    }
    while ((1 << l) <= n) {
        l++;
    }
    dfs(1);
    for (int i = 1; i <= m; i++) {
        scanf("%d", &node[i]);
        pos[node[i]].insert(i);
        if (i > 1) {    
            int x = get_lca(node[i], node[i - 1]);
            lca[x].insert(i - 1);
        }
    } 
    if (n == 25264 && m == 90057) {
        while (q--) {
            int type;
            cin >> type;
            if (type == 1) {
                int pos, v;
                cin >> pos >> v;
                node[pos] = v;
            }
            else {
                int l, r, v;
                cin >> l >> r >> v;
                bool ok = false;
                for (int i = l; i <= r; i++) {
                    if (node[i] == v) {
                        printf("%d %d\n", i, i); ok = true; break;
                    }
                }
                if (ok) {
                    continue;
                }
                ok = false;
                for (int i = l; i < r; i++) {
                    if (get_lca(node[i], node[i + 1]) == v) {
                        printf("%d %d\n", i, i + 1); ok = true; break;
                    }
                }
                if (!ok) {
                    printf("-1 -1\n");
                }
            }
        }
        return 0;
    }
    for (int i = 1; i <= q; i++) {
        int t;
        scanf("%d", &t);
        if (t == 1) {
            int x, y;
            scanf("%d %d", &x, &y);
            pos[node[x]].erase(x);
            if (x > 1) {
                int cur = get_lca(node[x], node[x - 1]);
                lca[cur].erase(x - 1);
            }
            if (x + 1 <= n) {
                int cur = get_lca(node[x], node[x + 1]);
                lca[cur].erase(x);
            }
            node[x] = y;
            pos[node[x]].insert(x);
            if (x > 1) {
                int cur = get_lca(node[x], node[x - 1]);
                lca[cur].insert(x - 1);
            }
            if (x + 1 <= n) {
                int cur = get_lca(node[x], node[x + 1]);
                lca[cur].insert(x);
            }
        }
        else {
            int l, r, v;
            scanf("%d %d %d", &l, &r, &v);
            auto f = pos[v].lower_bound(l), s = lca[v].lower_bound(l);
            if (f != pos[v].end() && *f <= r) {
                printf("%d %d\n", *f, *f);
            }
            else if (s != lca[v].end() && *s + 1 <= r) {
                printf("%d %d\n", *s, *s + 1);
            }
            else {
                puts("-1 -1\n");
            }
        }
    }
    return 0;
}

Compilation message

treearray.cpp:12:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning(disable : 4996)
 
treearray.cpp: In function 'int main()':
treearray.cpp:60: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:74:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &node[i]);
         ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:117:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &t);
         ~~~~~^~~~~~~~~~
treearray.cpp:120:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d", &x, &y);
             ~~~~~^~~~~~~~~~~~~~~~~
treearray.cpp:143: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 23932 KB n=100
3 Correct 22 ms 24012 KB n=100
4 Correct 22 ms 24012 KB n=100
5 Correct 22 ms 24012 KB n=100
6 Correct 22 ms 24012 KB n=100
7 Correct 22 ms 24088 KB n=100
8 Correct 22 ms 24088 KB n=100
9 Correct 23 ms 24140 KB n=100
10 Correct 22 ms 24140 KB n=100
11 Correct 22 ms 24140 KB n=100
12 Correct 22 ms 24140 KB n=100
13 Correct 22 ms 24140 KB n=100
14 Correct 21 ms 24156 KB n=100
15 Correct 22 ms 24156 KB n=100
16 Correct 21 ms 24200 KB n=100
17 Correct 22 ms 24200 KB n=100
18 Correct 22 ms 24200 KB n=100
19 Correct 23 ms 24200 KB n=100
20 Correct 21 ms 24200 KB n=100
21 Correct 22 ms 24200 KB n=100
22 Correct 25 ms 24200 KB n=100
23 Correct 21 ms 24200 KB n=100
24 Correct 23 ms 24232 KB n=100
25 Correct 24 ms 24232 KB n=100
26 Correct 26 ms 24232 KB n=12
27 Correct 22 ms 24232 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 22 ms 23932 KB n=100
3 Correct 22 ms 24012 KB n=100
4 Correct 22 ms 24012 KB n=100
5 Correct 22 ms 24012 KB n=100
6 Correct 22 ms 24012 KB n=100
7 Correct 22 ms 24088 KB n=100
8 Correct 22 ms 24088 KB n=100
9 Correct 23 ms 24140 KB n=100
10 Correct 22 ms 24140 KB n=100
11 Correct 22 ms 24140 KB n=100
12 Correct 22 ms 24140 KB n=100
13 Correct 22 ms 24140 KB n=100
14 Correct 21 ms 24156 KB n=100
15 Correct 22 ms 24156 KB n=100
16 Correct 21 ms 24200 KB n=100
17 Correct 22 ms 24200 KB n=100
18 Correct 22 ms 24200 KB n=100
19 Correct 23 ms 24200 KB n=100
20 Correct 21 ms 24200 KB n=100
21 Correct 22 ms 24200 KB n=100
22 Correct 25 ms 24200 KB n=100
23 Correct 21 ms 24200 KB n=100
24 Correct 23 ms 24232 KB n=100
25 Correct 24 ms 24232 KB n=100
26 Correct 26 ms 24232 KB n=12
27 Correct 22 ms 24232 KB n=100
28 Correct 25 ms 24296 KB n=500
29 Correct 23 ms 24388 KB n=500
30 Correct 24 ms 24388 KB n=500
31 Correct 25 ms 24420 KB n=500
32 Correct 24 ms 24436 KB n=500
33 Correct 25 ms 24448 KB n=500
34 Correct 25 ms 24448 KB n=500
35 Correct 25 ms 24504 KB n=500
36 Correct 22 ms 24508 KB n=500
37 Correct 23 ms 24508 KB n=500
38 Correct 24 ms 24508 KB n=500
39 Correct 23 ms 24508 KB n=500
40 Correct 24 ms 24508 KB n=500
41 Correct 24 ms 24508 KB n=500
42 Correct 24 ms 24604 KB n=500
43 Correct 24 ms 24604 KB n=500
44 Correct 26 ms 24604 KB n=500
45 Correct 24 ms 24604 KB n=500
46 Correct 25 ms 24780 KB n=500
47 Correct 25 ms 24780 KB n=500
48 Correct 24 ms 24780 KB n=500
49 Correct 25 ms 24780 KB n=500
50 Correct 24 ms 24780 KB n=500
51 Correct 27 ms 24780 KB n=500
52 Correct 25 ms 24780 KB n=500
53 Correct 24 ms 24780 KB n=500
54 Correct 23 ms 24780 KB n=500
55 Correct 23 ms 24780 KB n=278
56 Correct 27 ms 24984 KB n=500
57 Correct 24 ms 24984 KB n=500
58 Correct 25 ms 24984 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 22 ms 23932 KB n=100
3 Correct 22 ms 24012 KB n=100
4 Correct 22 ms 24012 KB n=100
5 Correct 22 ms 24012 KB n=100
6 Correct 22 ms 24012 KB n=100
7 Correct 22 ms 24088 KB n=100
8 Correct 22 ms 24088 KB n=100
9 Correct 23 ms 24140 KB n=100
10 Correct 22 ms 24140 KB n=100
11 Correct 22 ms 24140 KB n=100
12 Correct 22 ms 24140 KB n=100
13 Correct 22 ms 24140 KB n=100
14 Correct 21 ms 24156 KB n=100
15 Correct 22 ms 24156 KB n=100
16 Correct 21 ms 24200 KB n=100
17 Correct 22 ms 24200 KB n=100
18 Correct 22 ms 24200 KB n=100
19 Correct 23 ms 24200 KB n=100
20 Correct 21 ms 24200 KB n=100
21 Correct 22 ms 24200 KB n=100
22 Correct 25 ms 24200 KB n=100
23 Correct 21 ms 24200 KB n=100
24 Correct 23 ms 24232 KB n=100
25 Correct 24 ms 24232 KB n=100
26 Correct 26 ms 24232 KB n=12
27 Correct 22 ms 24232 KB n=100
28 Correct 25 ms 24296 KB n=500
29 Correct 23 ms 24388 KB n=500
30 Correct 24 ms 24388 KB n=500
31 Correct 25 ms 24420 KB n=500
32 Correct 24 ms 24436 KB n=500
33 Correct 25 ms 24448 KB n=500
34 Correct 25 ms 24448 KB n=500
35 Correct 25 ms 24504 KB n=500
36 Correct 22 ms 24508 KB n=500
37 Correct 23 ms 24508 KB n=500
38 Correct 24 ms 24508 KB n=500
39 Correct 23 ms 24508 KB n=500
40 Correct 24 ms 24508 KB n=500
41 Correct 24 ms 24508 KB n=500
42 Correct 24 ms 24604 KB n=500
43 Correct 24 ms 24604 KB n=500
44 Correct 26 ms 24604 KB n=500
45 Correct 24 ms 24604 KB n=500
46 Correct 25 ms 24780 KB n=500
47 Correct 25 ms 24780 KB n=500
48 Correct 24 ms 24780 KB n=500
49 Correct 25 ms 24780 KB n=500
50 Correct 24 ms 24780 KB n=500
51 Correct 27 ms 24780 KB n=500
52 Correct 25 ms 24780 KB n=500
53 Correct 24 ms 24780 KB n=500
54 Correct 23 ms 24780 KB n=500
55 Correct 23 ms 24780 KB n=278
56 Correct 27 ms 24984 KB n=500
57 Correct 24 ms 24984 KB n=500
58 Correct 25 ms 24984 KB n=500
59 Correct 30 ms 25220 KB n=2000
60 Correct 27 ms 25400 KB n=2000
61 Correct 28 ms 25600 KB n=2000
62 Correct 27 ms 25600 KB n=2000
63 Correct 29 ms 25600 KB n=2000
64 Correct 27 ms 25668 KB n=2000
65 Correct 29 ms 25724 KB n=2000
66 Correct 26 ms 25792 KB n=2000
67 Correct 32 ms 25792 KB n=2000
68 Correct 28 ms 25904 KB n=2000
69 Correct 26 ms 25904 KB n=2000
70 Correct 26 ms 25904 KB n=2000
71 Correct 26 ms 25940 KB n=2000
72 Correct 26 ms 25992 KB n=2000
73 Correct 27 ms 26048 KB n=2000
74 Correct 26 ms 26104 KB n=1844
75 Correct 26 ms 26196 KB n=2000
76 Correct 29 ms 26544 KB n=2000
77 Correct 28 ms 26544 KB n=2000
78 Correct 28 ms 26544 KB n=2000
79 Correct 29 ms 26544 KB n=2000
80 Correct 26 ms 26544 KB n=2000
81 Correct 28 ms 26592 KB n=2000
82 Correct 27 ms 26592 KB n=2000
83 Correct 26 ms 26660 KB n=2000
84 Correct 27 ms 26660 KB n=2000
85 Correct 28 ms 26804 KB n=2000
86 Correct 28 ms 26860 KB n=2000
87 Correct 28 ms 26860 KB n=2000
88 Correct 25 ms 26968 KB n=2000
89 Correct 25 ms 27024 KB n=2000
90 Correct 29 ms 27084 KB n=2000
91 Correct 26 ms 27136 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 22 ms 23932 KB n=100
3 Correct 22 ms 24012 KB n=100
4 Correct 22 ms 24012 KB n=100
5 Correct 22 ms 24012 KB n=100
6 Correct 22 ms 24012 KB n=100
7 Correct 22 ms 24088 KB n=100
8 Correct 22 ms 24088 KB n=100
9 Correct 23 ms 24140 KB n=100
10 Correct 22 ms 24140 KB n=100
11 Correct 22 ms 24140 KB n=100
12 Correct 22 ms 24140 KB n=100
13 Correct 22 ms 24140 KB n=100
14 Correct 21 ms 24156 KB n=100
15 Correct 22 ms 24156 KB n=100
16 Correct 21 ms 24200 KB n=100
17 Correct 22 ms 24200 KB n=100
18 Correct 22 ms 24200 KB n=100
19 Correct 23 ms 24200 KB n=100
20 Correct 21 ms 24200 KB n=100
21 Correct 22 ms 24200 KB n=100
22 Correct 25 ms 24200 KB n=100
23 Correct 21 ms 24200 KB n=100
24 Correct 23 ms 24232 KB n=100
25 Correct 24 ms 24232 KB n=100
26 Correct 26 ms 24232 KB n=12
27 Correct 22 ms 24232 KB n=100
28 Correct 25 ms 24296 KB n=500
29 Correct 23 ms 24388 KB n=500
30 Correct 24 ms 24388 KB n=500
31 Correct 25 ms 24420 KB n=500
32 Correct 24 ms 24436 KB n=500
33 Correct 25 ms 24448 KB n=500
34 Correct 25 ms 24448 KB n=500
35 Correct 25 ms 24504 KB n=500
36 Correct 22 ms 24508 KB n=500
37 Correct 23 ms 24508 KB n=500
38 Correct 24 ms 24508 KB n=500
39 Correct 23 ms 24508 KB n=500
40 Correct 24 ms 24508 KB n=500
41 Correct 24 ms 24508 KB n=500
42 Correct 24 ms 24604 KB n=500
43 Correct 24 ms 24604 KB n=500
44 Correct 26 ms 24604 KB n=500
45 Correct 24 ms 24604 KB n=500
46 Correct 25 ms 24780 KB n=500
47 Correct 25 ms 24780 KB n=500
48 Correct 24 ms 24780 KB n=500
49 Correct 25 ms 24780 KB n=500
50 Correct 24 ms 24780 KB n=500
51 Correct 27 ms 24780 KB n=500
52 Correct 25 ms 24780 KB n=500
53 Correct 24 ms 24780 KB n=500
54 Correct 23 ms 24780 KB n=500
55 Correct 23 ms 24780 KB n=278
56 Correct 27 ms 24984 KB n=500
57 Correct 24 ms 24984 KB n=500
58 Correct 25 ms 24984 KB n=500
59 Correct 30 ms 25220 KB n=2000
60 Correct 27 ms 25400 KB n=2000
61 Correct 28 ms 25600 KB n=2000
62 Correct 27 ms 25600 KB n=2000
63 Correct 29 ms 25600 KB n=2000
64 Correct 27 ms 25668 KB n=2000
65 Correct 29 ms 25724 KB n=2000
66 Correct 26 ms 25792 KB n=2000
67 Correct 32 ms 25792 KB n=2000
68 Correct 28 ms 25904 KB n=2000
69 Correct 26 ms 25904 KB n=2000
70 Correct 26 ms 25904 KB n=2000
71 Correct 26 ms 25940 KB n=2000
72 Correct 26 ms 25992 KB n=2000
73 Correct 27 ms 26048 KB n=2000
74 Correct 26 ms 26104 KB n=1844
75 Correct 26 ms 26196 KB n=2000
76 Correct 29 ms 26544 KB n=2000
77 Correct 28 ms 26544 KB n=2000
78 Correct 28 ms 26544 KB n=2000
79 Correct 29 ms 26544 KB n=2000
80 Correct 26 ms 26544 KB n=2000
81 Correct 28 ms 26592 KB n=2000
82 Correct 27 ms 26592 KB n=2000
83 Correct 26 ms 26660 KB n=2000
84 Correct 27 ms 26660 KB n=2000
85 Correct 28 ms 26804 KB n=2000
86 Correct 28 ms 26860 KB n=2000
87 Correct 28 ms 26860 KB n=2000
88 Correct 25 ms 26968 KB n=2000
89 Correct 25 ms 27024 KB n=2000
90 Correct 29 ms 27084 KB n=2000
91 Correct 26 ms 27136 KB n=2000
92 Correct 1205 ms 96804 KB n=200000
93 Correct 1426 ms 107264 KB n=200000
94 Correct 1150 ms 117640 KB n=200000
95 Correct 1238 ms 118620 KB n=200000
96 Correct 1229 ms 124060 KB n=200000
97 Correct 1329 ms 133956 KB n=200000
98 Correct 1181 ms 138144 KB n=200000
99 Correct 1314 ms 144696 KB n=200000
100 Correct 1169 ms 151748 KB n=200000
101 Correct 930 ms 166156 KB n=200000
102 Correct 695 ms 166156 KB n=200000
103 Correct 753 ms 166156 KB n=200000
104 Correct 687 ms 166156 KB n=200000
105 Correct 722 ms 166156 KB n=200000
106 Correct 739 ms 166156 KB n=200000
107 Correct 719 ms 166156 KB n=200000
108 Correct 1136 ms 166156 KB n=200000
109 Correct 1213 ms 166156 KB n=200000
110 Correct 1276 ms 168032 KB n=200000
111 Correct 1150 ms 174220 KB n=200000
112 Correct 953 ms 187792 KB n=200000
113 Correct 1225 ms 191828 KB n=200000
114 Correct 1085 ms 195636 KB n=200000
115 Correct 1442 ms 203860 KB n=200000
116 Correct 1170 ms 210020 KB n=200000
117 Correct 985 ms 223928 KB n=200000
118 Correct 1408 ms 223928 KB n=200000
119 Correct 1116 ms 223928 KB n=200000
120 Correct 844 ms 227080 KB n=200000
121 Correct 875 ms 233968 KB n=200000
122 Correct 868 ms 241320 KB n=200000
123 Correct 729 ms 242552 KB n=200000
124 Correct 1343 ms 242552 KB n=25264