Submission #884032

# Submission time Handle Problem Language Result Execution time Memory
884032 2023-12-06T14:49:20 Z fanwen Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 18452 KB
#include <bits/stdc++.h>

#define fi first
#define se second
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }

using namespace std;

#ifdef LOCAL
    #include "debug.h"
#else
    #define clog if(false) cerr
#endif

const int MAX = 1e5 + 5;

int n, m, q, pos[MAX], ans[MAX], time_in[MAX], time_out[MAX], depth[MAX], anc[MAX][20];
vector <int> adj[MAX];

bool is_par(int u, int v) {
    return time_in[u] <= time_in[v] and time_in[v] <= time_out[u];
}

void dfs(int u, int p) {
    static int run = 0;
    time_in[u] = ++run;
    depth[u] = depth[p] + 1;
    anc[u][0] = p;
    for (int i = 1; i <= 17; ++i) anc[u][i] = anc[anc[u][i - 1]][i - 1];
    for (auto v : adj[u]) if(v != p) {
        dfs(v, u);
    }
    time_out[u] = run;
}

int lca(int u, int v) {
    if(depth[u] < depth[v]) swap(u, v);
    for (int i = 17; i >= 0; --i) if(depth[anc[u][i]] >= depth[v]) {
        u = anc[u][i];
    }

    if(u == v) return u;

    for (int i= 17; i >= 0; --i) if(anc[u][i] != anc[v][i]) {
        u = anc[u][i];
        v = anc[v][i];
    }
    return anc[u][0];
}

int solve(int l, int r) {
    int ans = 0;
    vector <int> p;
    for (int i = l; i <= r; ++i) {
        p.push_back(pos[i]);
    }
    sort(p.begin(), p.end(), [&] (const int &x, const int &y) {
         return time_in[x] < time_in[y];
    });
    int sz = (int) p.size();
    for (int i = 0; i < sz - 1; ++i) {
        p.push_back(lca(p[i], p[i + 1]));
    }
    sort(p.begin(), p.end(), [&] (const int &x, const int &y) {
         return time_in[x] < time_in[y];
    });
    p.erase(unique(p.begin(), p.end()), p.end());
    stack <int> st;
    for (int i = 0; i < (int) p.size(); ++i) {
        while(!st.empty() && !is_par(st.top(), p[i])) st.pop();
        if(!st.empty()) ans += depth[p[i]] - depth[st.top()];
        else ans++;
        st.push(p[i]);
    }
    return ans;
}

void you_make_it(void) {
    cin >> n >> m >> q;
    for (int i = 1; i < n; ++i) {
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i = 1; i <= m; ++i) cin >> pos[i];
    dfs(1, 0);
    for (int i = 0; i < q; ++i) {
        int l, r; cin >> l >> r;
        cout << solve(l, r) << '\n';
    }
}

signed main() {

#ifdef LOCAL
    freopen("TASK.inp", "r", stdin);
    freopen("TASK.out", "w", stdout);
#endif
    file("tourism");
    auto start_time = chrono::steady_clock::now();

    cin.tie(0), cout.tie(0) -> sync_with_stdio(0);

    you_make_it();

    auto end_time = chrono::steady_clock::now();

    cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;

    return (0 ^ 0);
}

// Dream it. Wish it. Do it.

Compilation message

tourism.cpp: In function 'int main()':
tourism.cpp:5:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:99:5: note: in expansion of macro 'file'
   99 |     file("tourism");
      |     ^~~~
tourism.cpp:5:90: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:99:5: note: in expansion of macro 'file'
   99 |     file("tourism");
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 7 ms 4700 KB Output is correct
5 Correct 5 ms 4700 KB Output is correct
6 Correct 4 ms 4700 KB Output is correct
7 Correct 5 ms 4700 KB Output is correct
8 Correct 4 ms 4700 KB Output is correct
9 Correct 8 ms 4700 KB Output is correct
10 Correct 10 ms 4792 KB Output is correct
11 Correct 8 ms 4700 KB Output is correct
12 Correct 21 ms 4700 KB Output is correct
13 Correct 20 ms 4700 KB Output is correct
14 Correct 19 ms 4700 KB Output is correct
15 Correct 8 ms 4800 KB Output is correct
16 Correct 6 ms 4800 KB Output is correct
17 Correct 7 ms 4700 KB Output is correct
18 Correct 8 ms 4700 KB Output is correct
19 Correct 8 ms 4788 KB Output is correct
20 Correct 7 ms 4700 KB Output is correct
21 Correct 7 ms 4700 KB Output is correct
22 Correct 9 ms 4796 KB Output is correct
23 Correct 8 ms 4800 KB Output is correct
24 Correct 8 ms 4792 KB Output is correct
25 Correct 8 ms 4700 KB Output is correct
26 Correct 8 ms 4744 KB Output is correct
27 Correct 5 ms 4696 KB Output is correct
28 Correct 2 ms 4740 KB Output is correct
29 Correct 2 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 7 ms 4700 KB Output is correct
5 Correct 5 ms 4700 KB Output is correct
6 Correct 4 ms 4700 KB Output is correct
7 Correct 5 ms 4700 KB Output is correct
8 Correct 4 ms 4700 KB Output is correct
9 Correct 8 ms 4700 KB Output is correct
10 Correct 10 ms 4792 KB Output is correct
11 Correct 8 ms 4700 KB Output is correct
12 Correct 21 ms 4700 KB Output is correct
13 Correct 20 ms 4700 KB Output is correct
14 Correct 19 ms 4700 KB Output is correct
15 Correct 8 ms 4800 KB Output is correct
16 Correct 6 ms 4800 KB Output is correct
17 Correct 7 ms 4700 KB Output is correct
18 Correct 8 ms 4700 KB Output is correct
19 Correct 8 ms 4788 KB Output is correct
20 Correct 7 ms 4700 KB Output is correct
21 Correct 7 ms 4700 KB Output is correct
22 Correct 9 ms 4796 KB Output is correct
23 Correct 8 ms 4800 KB Output is correct
24 Correct 8 ms 4792 KB Output is correct
25 Correct 8 ms 4700 KB Output is correct
26 Correct 8 ms 4744 KB Output is correct
27 Correct 5 ms 4696 KB Output is correct
28 Correct 2 ms 4740 KB Output is correct
29 Correct 2 ms 4700 KB Output is correct
30 Correct 166 ms 4876 KB Output is correct
31 Correct 244 ms 4880 KB Output is correct
32 Correct 350 ms 4928 KB Output is correct
33 Correct 358 ms 4932 KB Output is correct
34 Correct 339 ms 4924 KB Output is correct
35 Correct 1000 ms 4956 KB Output is correct
36 Correct 988 ms 4928 KB Output is correct
37 Correct 997 ms 4696 KB Output is correct
38 Correct 276 ms 4956 KB Output is correct
39 Correct 270 ms 4984 KB Output is correct
40 Correct 276 ms 5008 KB Output is correct
41 Correct 916 ms 5020 KB Output is correct
42 Correct 886 ms 5276 KB Output is correct
43 Correct 887 ms 4964 KB Output is correct
44 Correct 288 ms 4952 KB Output is correct
45 Correct 282 ms 4952 KB Output is correct
46 Correct 277 ms 4956 KB Output is correct
47 Correct 912 ms 4956 KB Output is correct
48 Correct 867 ms 4928 KB Output is correct
49 Correct 899 ms 4956 KB Output is correct
50 Correct 292 ms 4940 KB Output is correct
51 Correct 299 ms 4928 KB Output is correct
52 Correct 302 ms 4700 KB Output is correct
53 Correct 285 ms 5140 KB Output is correct
54 Correct 291 ms 5004 KB Output is correct
55 Correct 292 ms 4924 KB Output is correct
56 Correct 125 ms 4808 KB Output is correct
57 Correct 2 ms 4700 KB Output is correct
58 Correct 2 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 4 ms 4700 KB Output is correct
3 Correct 125 ms 4812 KB Output is correct
4 Execution timed out 5086 ms 17072 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 41 ms 12352 KB Output is correct
3 Correct 60 ms 13020 KB Output is correct
4 Correct 44 ms 13136 KB Output is correct
5 Correct 110 ms 18368 KB Output is correct
6 Correct 105 ms 17812 KB Output is correct
7 Correct 79 ms 17352 KB Output is correct
8 Correct 91 ms 17088 KB Output is correct
9 Correct 80 ms 17192 KB Output is correct
10 Correct 84 ms 17140 KB Output is correct
11 Correct 74 ms 16976 KB Output is correct
12 Correct 78 ms 16980 KB Output is correct
13 Correct 95 ms 17296 KB Output is correct
14 Correct 94 ms 17492 KB Output is correct
15 Correct 67 ms 18384 KB Output is correct
16 Correct 84 ms 17912 KB Output is correct
17 Correct 75 ms 17872 KB Output is correct
18 Correct 112 ms 17840 KB Output is correct
19 Correct 79 ms 18292 KB Output is correct
20 Correct 75 ms 17988 KB Output is correct
21 Correct 86 ms 17528 KB Output is correct
22 Correct 73 ms 17336 KB Output is correct
23 Correct 71 ms 17232 KB Output is correct
24 Correct 75 ms 17168 KB Output is correct
25 Correct 89 ms 17144 KB Output is correct
26 Correct 79 ms 16980 KB Output is correct
27 Correct 83 ms 16976 KB Output is correct
28 Correct 88 ms 17056 KB Output is correct
29 Correct 88 ms 17160 KB Output is correct
30 Correct 84 ms 17196 KB Output is correct
31 Correct 90 ms 17260 KB Output is correct
32 Correct 81 ms 17244 KB Output is correct
33 Correct 81 ms 17796 KB Output is correct
34 Correct 70 ms 18452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 5 ms 4700 KB Output is correct
3 Correct 127 ms 4820 KB Output is correct
4 Execution timed out 5047 ms 14628 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 7 ms 4700 KB Output is correct
5 Correct 5 ms 4700 KB Output is correct
6 Correct 4 ms 4700 KB Output is correct
7 Correct 5 ms 4700 KB Output is correct
8 Correct 4 ms 4700 KB Output is correct
9 Correct 8 ms 4700 KB Output is correct
10 Correct 10 ms 4792 KB Output is correct
11 Correct 8 ms 4700 KB Output is correct
12 Correct 21 ms 4700 KB Output is correct
13 Correct 20 ms 4700 KB Output is correct
14 Correct 19 ms 4700 KB Output is correct
15 Correct 8 ms 4800 KB Output is correct
16 Correct 6 ms 4800 KB Output is correct
17 Correct 7 ms 4700 KB Output is correct
18 Correct 8 ms 4700 KB Output is correct
19 Correct 8 ms 4788 KB Output is correct
20 Correct 7 ms 4700 KB Output is correct
21 Correct 7 ms 4700 KB Output is correct
22 Correct 9 ms 4796 KB Output is correct
23 Correct 8 ms 4800 KB Output is correct
24 Correct 8 ms 4792 KB Output is correct
25 Correct 8 ms 4700 KB Output is correct
26 Correct 8 ms 4744 KB Output is correct
27 Correct 5 ms 4696 KB Output is correct
28 Correct 2 ms 4740 KB Output is correct
29 Correct 2 ms 4700 KB Output is correct
30 Correct 166 ms 4876 KB Output is correct
31 Correct 244 ms 4880 KB Output is correct
32 Correct 350 ms 4928 KB Output is correct
33 Correct 358 ms 4932 KB Output is correct
34 Correct 339 ms 4924 KB Output is correct
35 Correct 1000 ms 4956 KB Output is correct
36 Correct 988 ms 4928 KB Output is correct
37 Correct 997 ms 4696 KB Output is correct
38 Correct 276 ms 4956 KB Output is correct
39 Correct 270 ms 4984 KB Output is correct
40 Correct 276 ms 5008 KB Output is correct
41 Correct 916 ms 5020 KB Output is correct
42 Correct 886 ms 5276 KB Output is correct
43 Correct 887 ms 4964 KB Output is correct
44 Correct 288 ms 4952 KB Output is correct
45 Correct 282 ms 4952 KB Output is correct
46 Correct 277 ms 4956 KB Output is correct
47 Correct 912 ms 4956 KB Output is correct
48 Correct 867 ms 4928 KB Output is correct
49 Correct 899 ms 4956 KB Output is correct
50 Correct 292 ms 4940 KB Output is correct
51 Correct 299 ms 4928 KB Output is correct
52 Correct 302 ms 4700 KB Output is correct
53 Correct 285 ms 5140 KB Output is correct
54 Correct 291 ms 5004 KB Output is correct
55 Correct 292 ms 4924 KB Output is correct
56 Correct 125 ms 4808 KB Output is correct
57 Correct 2 ms 4700 KB Output is correct
58 Correct 2 ms 4700 KB Output is correct
59 Correct 1 ms 4700 KB Output is correct
60 Correct 4 ms 4700 KB Output is correct
61 Correct 125 ms 4812 KB Output is correct
62 Execution timed out 5086 ms 17072 KB Time limit exceeded
63 Halted 0 ms 0 KB -