Submission #793779

# Submission time Handle Problem Language Result Execution time Memory
793779 2023-07-26T06:41:30 Z vjudge1 Tourism (JOI23_tourism) C++17
35 / 100
5000 ms 19132 KB
#ifdef Home
#define _GLIBCXX_DEBUG
#endif // Home

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 100100;
const int LOGN = 17;

vector < int > adj[N];
int up[N][LOGN], depth[N], use[N], T, ans, C[N], n, m, q;
int SpTmin[LOGN][N], SpTmax[LOGN][N];

int lg2(int a) {
    return 31 - __builtin_clz(a);
}

int get(int l, int r) {
    int m = lg2(r - l + 1);
    return max(SpTmax[m][l], SpTmax[m][r - (1<<m) + 1]) - 
           min(SpTmin[m][l], SpTmin[m][r - (1<<m) + 1]) + 1;
}

void dfs(int v, int p) {
    depth[v] = depth[p] + 1;
    up[v][0] = p;
    for(int i = 1; i < LOGN; ++ i) {
        up[v][i] = up[ up[v][i - 1] ][i - 1];
    }
    for(auto &u : adj[v]) {
        if(u != p) {
            dfs(u, v);
        }
    }
}

int slow_LCA(int a, int b) {
    if(depth[a] < depth[b]) {
        swap(a, b);
    }
    ans += use[a] != T;
    ans += use[b] != T;
    use[a] = T;
    use[b] = T;
    for(; depth[a] > depth[b];) {
        a = up[a][0];
        if(use[a] == T) {
            return b;
        }
        ++ ans;
        use[a] = T;
    }
    for(; a != b;) {
        a = up[a][0];
        b = up[b][0];
        if(a == b) {
            use[b] = T;
            ++ ans;
            return a;
        }
        ans += 2;
        use[a] = T;
        use[b] = T;
    }
    return b;
}

void solve_subtask_3() {
    for(int i = 1; i <= m; ++ i) {
        SpTmax[0][i] = SpTmin[0][i] = C[i];
    }
    for(int lg = 1; (1<<lg) <= m; ++ lg) {
        for(int i = 1; i <= m - (1<<lg) + 1; ++ i) {
            SpTmax[lg][i] = max(SpTmax[lg - 1][i], SpTmax[lg - 1][i + (1<<(lg - 1))]);
            SpTmin[lg][i] = min(SpTmin[lg - 1][i], SpTmin[lg - 1][i + (1<<(lg - 1))]);
        }
    }
    for(int l, r; q --> 0;) {
        cin >> l >> r;
        cout << get(l, r) << '\n';
    }
}

main() {
#ifdef Home
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // Home
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    cin >> n >> m >> q;
    bool subtsk3 = true;
    for(int i = 1, a, b; i < n; ++ i) {
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
        subtsk3 &= (a == i && b == i + 1);
    }
    for(int i = 1; i <= m; ++ i) {
        cin >> C[i]; 
    }
    if(subtsk3) {
        solve_subtask_3();
        return 0;
    }
    dfs(1, 1);
    for(int l, r, m; q --> 0;) {
        cin >> l >> r;
        ans = 1;
        m = C[l ++];
        use[m] = ++ T;
        for(; l <= r; ++ l) {
            m = slow_LCA(m, C[l]);
        }
        cout << ans << '\n';
    }
}

Compilation message

tourism.cpp:89:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   89 | main() {
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2680 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 1 ms 2772 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2680 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 1 ms 2772 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 10 ms 2864 KB Output is correct
31 Correct 13 ms 2872 KB Output is correct
32 Correct 20 ms 2856 KB Output is correct
33 Correct 19 ms 2920 KB Output is correct
34 Correct 20 ms 2920 KB Output is correct
35 Correct 26 ms 2904 KB Output is correct
36 Correct 26 ms 2900 KB Output is correct
37 Correct 26 ms 2900 KB Output is correct
38 Correct 26 ms 2980 KB Output is correct
39 Correct 26 ms 2900 KB Output is correct
40 Correct 27 ms 2988 KB Output is correct
41 Correct 39 ms 3000 KB Output is correct
42 Correct 50 ms 3000 KB Output is correct
43 Correct 36 ms 2972 KB Output is correct
44 Correct 33 ms 2956 KB Output is correct
45 Correct 27 ms 2920 KB Output is correct
46 Correct 37 ms 2900 KB Output is correct
47 Correct 40 ms 2944 KB Output is correct
48 Correct 39 ms 2936 KB Output is correct
49 Correct 55 ms 2948 KB Output is correct
50 Correct 8 ms 2900 KB Output is correct
51 Correct 8 ms 2932 KB Output is correct
52 Correct 8 ms 2900 KB Output is correct
53 Correct 8 ms 2936 KB Output is correct
54 Correct 13 ms 2932 KB Output is correct
55 Correct 8 ms 2900 KB Output is correct
56 Correct 2 ms 2900 KB Output is correct
57 Correct 2 ms 2900 KB Output is correct
58 Correct 2 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2772 KB Output is correct
3 Correct 2 ms 2900 KB Output is correct
4 Correct 37 ms 16084 KB Output is correct
5 Correct 45 ms 12484 KB Output is correct
6 Correct 39 ms 16208 KB Output is correct
7 Correct 70 ms 19132 KB Output is correct
8 Correct 53 ms 19076 KB Output is correct
9 Correct 75 ms 19068 KB Output is correct
10 Correct 56 ms 19112 KB Output is correct
11 Correct 64 ms 19020 KB Output is correct
12 Correct 52 ms 19036 KB Output is correct
13 Correct 49 ms 19028 KB Output is correct
14 Correct 50 ms 19068 KB Output is correct
15 Correct 32 ms 5908 KB Output is correct
16 Correct 49 ms 18688 KB Output is correct
17 Correct 32 ms 15556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 39 ms 8640 KB Output is correct
3 Correct 35 ms 9244 KB Output is correct
4 Correct 35 ms 10132 KB Output is correct
5 Correct 67 ms 13728 KB Output is correct
6 Correct 49 ms 13660 KB Output is correct
7 Correct 63 ms 13832 KB Output is correct
8 Correct 89 ms 13732 KB Output is correct
9 Correct 65 ms 13728 KB Output is correct
10 Correct 68 ms 13664 KB Output is correct
11 Correct 64 ms 13728 KB Output is correct
12 Correct 90 ms 13740 KB Output is correct
13 Correct 83 ms 13648 KB Output is correct
14 Correct 106 ms 13780 KB Output is correct
15 Correct 77 ms 13904 KB Output is correct
16 Correct 75 ms 13732 KB Output is correct
17 Correct 59 ms 13768 KB Output is correct
18 Correct 56 ms 13752 KB Output is correct
19 Correct 51 ms 13716 KB Output is correct
20 Correct 64 ms 13708 KB Output is correct
21 Correct 73 ms 13692 KB Output is correct
22 Correct 69 ms 13712 KB Output is correct
23 Correct 78 ms 13704 KB Output is correct
24 Correct 166 ms 13700 KB Output is correct
25 Correct 184 ms 13776 KB Output is correct
26 Correct 225 ms 13724 KB Output is correct
27 Correct 367 ms 13716 KB Output is correct
28 Correct 475 ms 13776 KB Output is correct
29 Correct 562 ms 13708 KB Output is correct
30 Correct 614 ms 13744 KB Output is correct
31 Correct 669 ms 13752 KB Output is correct
32 Correct 765 ms 13804 KB Output is correct
33 Correct 565 ms 13896 KB Output is correct
34 Correct 63 ms 13876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2772 KB Output is correct
3 Correct 2 ms 2900 KB Output is correct
4 Execution timed out 5069 ms 8984 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2680 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 1 ms 2772 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 10 ms 2864 KB Output is correct
31 Correct 13 ms 2872 KB Output is correct
32 Correct 20 ms 2856 KB Output is correct
33 Correct 19 ms 2920 KB Output is correct
34 Correct 20 ms 2920 KB Output is correct
35 Correct 26 ms 2904 KB Output is correct
36 Correct 26 ms 2900 KB Output is correct
37 Correct 26 ms 2900 KB Output is correct
38 Correct 26 ms 2980 KB Output is correct
39 Correct 26 ms 2900 KB Output is correct
40 Correct 27 ms 2988 KB Output is correct
41 Correct 39 ms 3000 KB Output is correct
42 Correct 50 ms 3000 KB Output is correct
43 Correct 36 ms 2972 KB Output is correct
44 Correct 33 ms 2956 KB Output is correct
45 Correct 27 ms 2920 KB Output is correct
46 Correct 37 ms 2900 KB Output is correct
47 Correct 40 ms 2944 KB Output is correct
48 Correct 39 ms 2936 KB Output is correct
49 Correct 55 ms 2948 KB Output is correct
50 Correct 8 ms 2900 KB Output is correct
51 Correct 8 ms 2932 KB Output is correct
52 Correct 8 ms 2900 KB Output is correct
53 Correct 8 ms 2936 KB Output is correct
54 Correct 13 ms 2932 KB Output is correct
55 Correct 8 ms 2900 KB Output is correct
56 Correct 2 ms 2900 KB Output is correct
57 Correct 2 ms 2900 KB Output is correct
58 Correct 2 ms 2900 KB Output is correct
59 Correct 2 ms 2644 KB Output is correct
60 Correct 1 ms 2772 KB Output is correct
61 Correct 2 ms 2900 KB Output is correct
62 Correct 37 ms 16084 KB Output is correct
63 Correct 45 ms 12484 KB Output is correct
64 Correct 39 ms 16208 KB Output is correct
65 Correct 70 ms 19132 KB Output is correct
66 Correct 53 ms 19076 KB Output is correct
67 Correct 75 ms 19068 KB Output is correct
68 Correct 56 ms 19112 KB Output is correct
69 Correct 64 ms 19020 KB Output is correct
70 Correct 52 ms 19036 KB Output is correct
71 Correct 49 ms 19028 KB Output is correct
72 Correct 50 ms 19068 KB Output is correct
73 Correct 32 ms 5908 KB Output is correct
74 Correct 49 ms 18688 KB Output is correct
75 Correct 32 ms 15556 KB Output is correct
76 Correct 1 ms 2644 KB Output is correct
77 Correct 39 ms 8640 KB Output is correct
78 Correct 35 ms 9244 KB Output is correct
79 Correct 35 ms 10132 KB Output is correct
80 Correct 67 ms 13728 KB Output is correct
81 Correct 49 ms 13660 KB Output is correct
82 Correct 63 ms 13832 KB Output is correct
83 Correct 89 ms 13732 KB Output is correct
84 Correct 65 ms 13728 KB Output is correct
85 Correct 68 ms 13664 KB Output is correct
86 Correct 64 ms 13728 KB Output is correct
87 Correct 90 ms 13740 KB Output is correct
88 Correct 83 ms 13648 KB Output is correct
89 Correct 106 ms 13780 KB Output is correct
90 Correct 77 ms 13904 KB Output is correct
91 Correct 75 ms 13732 KB Output is correct
92 Correct 59 ms 13768 KB Output is correct
93 Correct 56 ms 13752 KB Output is correct
94 Correct 51 ms 13716 KB Output is correct
95 Correct 64 ms 13708 KB Output is correct
96 Correct 73 ms 13692 KB Output is correct
97 Correct 69 ms 13712 KB Output is correct
98 Correct 78 ms 13704 KB Output is correct
99 Correct 166 ms 13700 KB Output is correct
100 Correct 184 ms 13776 KB Output is correct
101 Correct 225 ms 13724 KB Output is correct
102 Correct 367 ms 13716 KB Output is correct
103 Correct 475 ms 13776 KB Output is correct
104 Correct 562 ms 13708 KB Output is correct
105 Correct 614 ms 13744 KB Output is correct
106 Correct 669 ms 13752 KB Output is correct
107 Correct 765 ms 13804 KB Output is correct
108 Correct 565 ms 13896 KB Output is correct
109 Correct 63 ms 13876 KB Output is correct
110 Correct 1 ms 2644 KB Output is correct
111 Correct 1 ms 2772 KB Output is correct
112 Correct 2 ms 2900 KB Output is correct
113 Execution timed out 5069 ms 8984 KB Time limit exceeded
114 Halted 0 ms 0 KB -