답안 #84466

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
84466 2018-11-15T10:25:26 Z inom Birthday gift (IZhO18_treearray) C++14
56 / 100
1538 ms 140912 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() {
    ios_base::sync_with_stdio(false);
    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);
        }
    }
    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 {
                printf("-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:61: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:64: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:75:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &node[i]);
         ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &t);
         ~~~~~^~~~~~~~~~
treearray.cpp:87: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:111: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);
             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 24 ms 24084 KB n=100
3 Correct 22 ms 24084 KB n=100
4 Correct 24 ms 24084 KB n=100
5 Correct 24 ms 24084 KB n=100
6 Correct 22 ms 24116 KB n=100
7 Correct 23 ms 24164 KB n=100
8 Correct 29 ms 24296 KB n=100
9 Correct 29 ms 24296 KB n=100
10 Correct 30 ms 24296 KB n=100
11 Correct 27 ms 24296 KB n=100
12 Correct 31 ms 24296 KB n=100
13 Correct 29 ms 24296 KB n=100
14 Correct 23 ms 24296 KB n=100
15 Correct 23 ms 24296 KB n=100
16 Correct 23 ms 24296 KB n=100
17 Correct 22 ms 24296 KB n=100
18 Correct 24 ms 24296 KB n=100
19 Correct 30 ms 24296 KB n=100
20 Correct 23 ms 24296 KB n=100
21 Correct 25 ms 24296 KB n=100
22 Correct 23 ms 24296 KB n=100
23 Correct 23 ms 24296 KB n=100
24 Correct 24 ms 24296 KB n=100
25 Correct 25 ms 24296 KB n=100
26 Correct 23 ms 24296 KB n=12
27 Correct 23 ms 24296 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 24 ms 24084 KB n=100
3 Correct 22 ms 24084 KB n=100
4 Correct 24 ms 24084 KB n=100
5 Correct 24 ms 24084 KB n=100
6 Correct 22 ms 24116 KB n=100
7 Correct 23 ms 24164 KB n=100
8 Correct 29 ms 24296 KB n=100
9 Correct 29 ms 24296 KB n=100
10 Correct 30 ms 24296 KB n=100
11 Correct 27 ms 24296 KB n=100
12 Correct 31 ms 24296 KB n=100
13 Correct 29 ms 24296 KB n=100
14 Correct 23 ms 24296 KB n=100
15 Correct 23 ms 24296 KB n=100
16 Correct 23 ms 24296 KB n=100
17 Correct 22 ms 24296 KB n=100
18 Correct 24 ms 24296 KB n=100
19 Correct 30 ms 24296 KB n=100
20 Correct 23 ms 24296 KB n=100
21 Correct 25 ms 24296 KB n=100
22 Correct 23 ms 24296 KB n=100
23 Correct 23 ms 24296 KB n=100
24 Correct 24 ms 24296 KB n=100
25 Correct 25 ms 24296 KB n=100
26 Correct 23 ms 24296 KB n=12
27 Correct 23 ms 24296 KB n=100
28 Correct 24 ms 24404 KB n=500
29 Correct 24 ms 24548 KB n=500
30 Correct 24 ms 24548 KB n=500
31 Correct 23 ms 24548 KB n=500
32 Correct 23 ms 24548 KB n=500
33 Correct 29 ms 24548 KB n=500
34 Correct 29 ms 24548 KB n=500
35 Correct 23 ms 24548 KB n=500
36 Correct 23 ms 24548 KB n=500
37 Correct 22 ms 24548 KB n=500
38 Correct 24 ms 24548 KB n=500
39 Correct 24 ms 24560 KB n=500
40 Correct 25 ms 24576 KB n=500
41 Correct 25 ms 24592 KB n=500
42 Correct 25 ms 24608 KB n=500
43 Correct 29 ms 24620 KB n=500
44 Correct 24 ms 24632 KB n=500
45 Correct 24 ms 24644 KB n=500
46 Correct 30 ms 24656 KB n=500
47 Correct 29 ms 24672 KB n=500
48 Correct 24 ms 24688 KB n=500
49 Correct 33 ms 24700 KB n=500
50 Correct 25 ms 24716 KB n=500
51 Correct 24 ms 24728 KB n=500
52 Correct 29 ms 24744 KB n=500
53 Correct 23 ms 24760 KB n=500
54 Correct 24 ms 24776 KB n=500
55 Correct 23 ms 24776 KB n=278
56 Correct 30 ms 24796 KB n=500
57 Correct 23 ms 24796 KB n=500
58 Correct 30 ms 24820 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 24 ms 24084 KB n=100
3 Correct 22 ms 24084 KB n=100
4 Correct 24 ms 24084 KB n=100
5 Correct 24 ms 24084 KB n=100
6 Correct 22 ms 24116 KB n=100
7 Correct 23 ms 24164 KB n=100
8 Correct 29 ms 24296 KB n=100
9 Correct 29 ms 24296 KB n=100
10 Correct 30 ms 24296 KB n=100
11 Correct 27 ms 24296 KB n=100
12 Correct 31 ms 24296 KB n=100
13 Correct 29 ms 24296 KB n=100
14 Correct 23 ms 24296 KB n=100
15 Correct 23 ms 24296 KB n=100
16 Correct 23 ms 24296 KB n=100
17 Correct 22 ms 24296 KB n=100
18 Correct 24 ms 24296 KB n=100
19 Correct 30 ms 24296 KB n=100
20 Correct 23 ms 24296 KB n=100
21 Correct 25 ms 24296 KB n=100
22 Correct 23 ms 24296 KB n=100
23 Correct 23 ms 24296 KB n=100
24 Correct 24 ms 24296 KB n=100
25 Correct 25 ms 24296 KB n=100
26 Correct 23 ms 24296 KB n=12
27 Correct 23 ms 24296 KB n=100
28 Correct 24 ms 24404 KB n=500
29 Correct 24 ms 24548 KB n=500
30 Correct 24 ms 24548 KB n=500
31 Correct 23 ms 24548 KB n=500
32 Correct 23 ms 24548 KB n=500
33 Correct 29 ms 24548 KB n=500
34 Correct 29 ms 24548 KB n=500
35 Correct 23 ms 24548 KB n=500
36 Correct 23 ms 24548 KB n=500
37 Correct 22 ms 24548 KB n=500
38 Correct 24 ms 24548 KB n=500
39 Correct 24 ms 24560 KB n=500
40 Correct 25 ms 24576 KB n=500
41 Correct 25 ms 24592 KB n=500
42 Correct 25 ms 24608 KB n=500
43 Correct 29 ms 24620 KB n=500
44 Correct 24 ms 24632 KB n=500
45 Correct 24 ms 24644 KB n=500
46 Correct 30 ms 24656 KB n=500
47 Correct 29 ms 24672 KB n=500
48 Correct 24 ms 24688 KB n=500
49 Correct 33 ms 24700 KB n=500
50 Correct 25 ms 24716 KB n=500
51 Correct 24 ms 24728 KB n=500
52 Correct 29 ms 24744 KB n=500
53 Correct 23 ms 24760 KB n=500
54 Correct 24 ms 24776 KB n=500
55 Correct 23 ms 24776 KB n=278
56 Correct 30 ms 24796 KB n=500
57 Correct 23 ms 24796 KB n=500
58 Correct 30 ms 24820 KB n=500
59 Correct 28 ms 25348 KB n=2000
60 Correct 27 ms 25400 KB n=2000
61 Correct 27 ms 25472 KB n=2000
62 Correct 27 ms 25528 KB n=2000
63 Correct 29 ms 25600 KB n=2000
64 Correct 31 ms 25600 KB n=2000
65 Correct 28 ms 25724 KB n=2000
66 Correct 28 ms 25776 KB n=2000
67 Correct 30 ms 25832 KB n=2000
68 Correct 29 ms 25868 KB n=2000
69 Correct 32 ms 25868 KB n=2000
70 Correct 27 ms 25996 KB n=2000
71 Correct 29 ms 26012 KB n=2000
72 Correct 26 ms 26116 KB n=2000
73 Correct 27 ms 26116 KB n=2000
74 Correct 27 ms 26116 KB n=1844
75 Correct 27 ms 26192 KB n=2000
76 Correct 27 ms 26192 KB n=2000
77 Correct 27 ms 26192 KB n=2000
78 Correct 35 ms 26192 KB n=2000
79 Correct 28 ms 26192 KB n=2000
80 Correct 27 ms 26196 KB n=2000
81 Correct 28 ms 26196 KB n=2000
82 Correct 29 ms 26196 KB n=2000
83 Correct 27 ms 26196 KB n=2000
84 Correct 28 ms 26196 KB n=2000
85 Correct 28 ms 26196 KB n=2000
86 Correct 27 ms 26196 KB n=2000
87 Correct 27 ms 26196 KB n=2000
88 Correct 26 ms 26196 KB n=2000
89 Correct 26 ms 26196 KB n=2000
90 Correct 27 ms 26324 KB n=2000
91 Correct 28 ms 26324 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 24 ms 24084 KB n=100
3 Correct 22 ms 24084 KB n=100
4 Correct 24 ms 24084 KB n=100
5 Correct 24 ms 24084 KB n=100
6 Correct 22 ms 24116 KB n=100
7 Correct 23 ms 24164 KB n=100
8 Correct 29 ms 24296 KB n=100
9 Correct 29 ms 24296 KB n=100
10 Correct 30 ms 24296 KB n=100
11 Correct 27 ms 24296 KB n=100
12 Correct 31 ms 24296 KB n=100
13 Correct 29 ms 24296 KB n=100
14 Correct 23 ms 24296 KB n=100
15 Correct 23 ms 24296 KB n=100
16 Correct 23 ms 24296 KB n=100
17 Correct 22 ms 24296 KB n=100
18 Correct 24 ms 24296 KB n=100
19 Correct 30 ms 24296 KB n=100
20 Correct 23 ms 24296 KB n=100
21 Correct 25 ms 24296 KB n=100
22 Correct 23 ms 24296 KB n=100
23 Correct 23 ms 24296 KB n=100
24 Correct 24 ms 24296 KB n=100
25 Correct 25 ms 24296 KB n=100
26 Correct 23 ms 24296 KB n=12
27 Correct 23 ms 24296 KB n=100
28 Correct 24 ms 24404 KB n=500
29 Correct 24 ms 24548 KB n=500
30 Correct 24 ms 24548 KB n=500
31 Correct 23 ms 24548 KB n=500
32 Correct 23 ms 24548 KB n=500
33 Correct 29 ms 24548 KB n=500
34 Correct 29 ms 24548 KB n=500
35 Correct 23 ms 24548 KB n=500
36 Correct 23 ms 24548 KB n=500
37 Correct 22 ms 24548 KB n=500
38 Correct 24 ms 24548 KB n=500
39 Correct 24 ms 24560 KB n=500
40 Correct 25 ms 24576 KB n=500
41 Correct 25 ms 24592 KB n=500
42 Correct 25 ms 24608 KB n=500
43 Correct 29 ms 24620 KB n=500
44 Correct 24 ms 24632 KB n=500
45 Correct 24 ms 24644 KB n=500
46 Correct 30 ms 24656 KB n=500
47 Correct 29 ms 24672 KB n=500
48 Correct 24 ms 24688 KB n=500
49 Correct 33 ms 24700 KB n=500
50 Correct 25 ms 24716 KB n=500
51 Correct 24 ms 24728 KB n=500
52 Correct 29 ms 24744 KB n=500
53 Correct 23 ms 24760 KB n=500
54 Correct 24 ms 24776 KB n=500
55 Correct 23 ms 24776 KB n=278
56 Correct 30 ms 24796 KB n=500
57 Correct 23 ms 24796 KB n=500
58 Correct 30 ms 24820 KB n=500
59 Correct 28 ms 25348 KB n=2000
60 Correct 27 ms 25400 KB n=2000
61 Correct 27 ms 25472 KB n=2000
62 Correct 27 ms 25528 KB n=2000
63 Correct 29 ms 25600 KB n=2000
64 Correct 31 ms 25600 KB n=2000
65 Correct 28 ms 25724 KB n=2000
66 Correct 28 ms 25776 KB n=2000
67 Correct 30 ms 25832 KB n=2000
68 Correct 29 ms 25868 KB n=2000
69 Correct 32 ms 25868 KB n=2000
70 Correct 27 ms 25996 KB n=2000
71 Correct 29 ms 26012 KB n=2000
72 Correct 26 ms 26116 KB n=2000
73 Correct 27 ms 26116 KB n=2000
74 Correct 27 ms 26116 KB n=1844
75 Correct 27 ms 26192 KB n=2000
76 Correct 27 ms 26192 KB n=2000
77 Correct 27 ms 26192 KB n=2000
78 Correct 35 ms 26192 KB n=2000
79 Correct 28 ms 26192 KB n=2000
80 Correct 27 ms 26196 KB n=2000
81 Correct 28 ms 26196 KB n=2000
82 Correct 29 ms 26196 KB n=2000
83 Correct 27 ms 26196 KB n=2000
84 Correct 28 ms 26196 KB n=2000
85 Correct 28 ms 26196 KB n=2000
86 Correct 27 ms 26196 KB n=2000
87 Correct 27 ms 26196 KB n=2000
88 Correct 26 ms 26196 KB n=2000
89 Correct 26 ms 26196 KB n=2000
90 Correct 27 ms 26324 KB n=2000
91 Correct 28 ms 26324 KB n=2000
92 Correct 1189 ms 89372 KB n=200000
93 Correct 1260 ms 92220 KB n=200000
94 Correct 988 ms 94796 KB n=200000
95 Correct 1094 ms 94796 KB n=200000
96 Correct 1113 ms 94796 KB n=200000
97 Correct 1269 ms 94796 KB n=200000
98 Correct 1162 ms 94796 KB n=200000
99 Correct 1212 ms 94796 KB n=200000
100 Correct 1168 ms 94796 KB n=200000
101 Correct 900 ms 95764 KB n=200000
102 Correct 698 ms 95764 KB n=200000
103 Correct 653 ms 95764 KB n=200000
104 Correct 690 ms 95764 KB n=200000
105 Correct 689 ms 95764 KB n=200000
106 Correct 679 ms 95764 KB n=200000
107 Correct 725 ms 95764 KB n=200000
108 Correct 1159 ms 95764 KB n=200000
109 Correct 1177 ms 95764 KB n=200000
110 Correct 1194 ms 95764 KB n=200000
111 Correct 1182 ms 95764 KB n=200000
112 Correct 937 ms 95764 KB n=200000
113 Correct 1284 ms 95764 KB n=200000
114 Correct 1143 ms 95764 KB n=200000
115 Correct 1538 ms 95764 KB n=200000
116 Correct 1046 ms 95764 KB n=200000
117 Correct 860 ms 103636 KB n=200000
118 Correct 1290 ms 106684 KB n=200000
119 Correct 1063 ms 112192 KB n=200000
120 Correct 802 ms 125468 KB n=200000
121 Correct 865 ms 132760 KB n=200000
122 Correct 832 ms 139936 KB n=200000
123 Correct 705 ms 140912 KB n=200000
124 Incorrect 239 ms 140912 KB Wrong range.