Submission #884160

# Submission time Handle Problem Language Result Execution time Memory
884160 2023-12-06T16:26:36 Z fanwen Tourism (JOI23_tourism) C++17
100 / 100
510 ms 26196 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

template <class T> struct Fenwick_Tree {
    vector<T> bit;
    int n;
    Fenwick_Tree(int _n = 0) : n(_n), bit(_n + 5){}

    void clear() { fill(bit.begin(), bit.end(), T(0)); }

    void update(int u, T val) {
        for (; u <= n; u += u & -u) bit[u] += val;
    }

    T get(int u) {
        T ans = 0;
        for (; u; u -= u & -u) ans += bit[u];
        return ans;
    }

    T get(int l, int r) {
        return get(r) - get(l - 1);
    }
};


const int MAX = 1e5 + 5;

int n, m, q, c[MAX], ans[MAX], local[MAX], depth[MAX], header[MAX], par[MAX];
int numChild[MAX];
vector <int> adj[MAX];
vector <pair <int, int>> queries[MAX];

void dfs(int u, int p) {
    numChild[u] = 1;
    int Max = 0, id = -1;
    for (int i = 0; i < (int) adj[u].size(); ++i) if(adj[u][i] != p) {
        int v = adj[u][i];
        dfs(v, u);
        numChild[u] += numChild[v];
        if(Max < numChild[v]) {
            Max = numChild[v];
            id = i;
        }
    }
    if(id != -1) swap(adj[u][0], adj[u][id]);
}

void dfs_hld(int u, int p) {
    static int run = 0;
    local[u] = ++run;
    par[u] = p;
    for (int i = 0; i < (int) adj[u].size(); ++i) if(adj[u][i] != p) {
        int v = adj[u][i];
        if(i == 0 && numChild[v] * 2 >= numChild[u]) {
            header[v] = header[u], depth[v] = depth[u];
        } else {
            header[v] = v;
            depth[v] = depth[u] + 1;
        }
        dfs_hld(v, u);
    }
}

int it[4 * MAX];

Fenwick_Tree <int> bit;

void pushdown(int idx) {
    if(it[idx] == -1) return;
    it[idx << 1] = it[idx << 1 | 1] = it[idx];
}

void update(int idx, int l, int r, int u, int v, int val) {
    if(l > v || r < u) return;
    if(l >= u && r <= v && it[idx] >= 0) {
        if(it[idx] > 0) bit.update(it[idx], - (r - l + 1));
        bit.update(it[idx] = val, r - l + 1);
        return;
    }

    pushdown(idx);

    int mid = l + r >> 1;
    update(idx << 1, l, mid, u, v, val);
    update(idx << 1 | 1, mid + 1, r, u, v, val);
    it[idx] = (it[idx << 1] == it[idx << 1 | 1] ? it[idx << 1] : -1);
}

void update_hld(int u, int v, int val) {
    while(header[u] != header[v]) {
        if(local[header[u]] < local[header[v]]) swap(u, v);
        update(1, 1, n, local[header[u]], local[u], val);
        u = par[header[u]];
    }
    update(1, 1, n, min(local[u], local[v]), max(local[u], local[v]), val);
}

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 >> c[i];
    dfs(1, 0); depth[1] = header[1] = 1; dfs_hld(1, 0);

    fill(ans + 1, ans + q + 1, 1);
    for (int i = 1; i <= q; ++i) {
        int l, r; cin >> l >> r;
        if(l < r) queries[l].emplace_back(r, i);
    }

    bit = Fenwick_Tree <int> (m);
    for (int i = m - 1; i >= 1; --i) {
        update_hld(c[i], c[i + 1], i + 1);
        for (auto [r, id] : queries[i]) ans[id] = bit.get(r);
    }
    for (int i = 1; i <= q; ++i) cout << ans[i] << '\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 'void update(int, int, int, int, int, int)':
tourism.cpp:95:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   95 |     int mid = l + r >> 1;
      |               ~~^~~
tourism.cpp: In instantiation of 'Fenwick_Tree<T>::Fenwick_Tree(int) [with T = int]':
tourism.cpp:78:20:   required from here
tourism.cpp:17:9: warning: 'Fenwick_Tree<int>::n' will be initialized after [-Wreorder]
   17 |     int n;
      |         ^
tourism.cpp:16:15: warning:   'std::vector<int> Fenwick_Tree<int>::bit' [-Wreorder]
   16 |     vector<T> bit;
      |               ^~~
tourism.cpp:18:5: warning:   when initialized here [-Wreorder]
   18 |     Fenwick_Tree(int _n = 0) : n(_n), bit(_n + 5){}
      |     ^~~~~~~~~~~~
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:140:5: note: in expansion of macro 'file'
  140 |     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:140:5: note: in expansion of macro 'file'
  140 |     file("tourism");
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8536 KB Output is correct
2 Correct 3 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8536 KB Output is correct
7 Correct 2 ms 8536 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 2 ms 8632 KB Output is correct
15 Correct 2 ms 8540 KB Output is correct
16 Correct 2 ms 8540 KB Output is correct
17 Correct 2 ms 8536 KB Output is correct
18 Correct 2 ms 8536 KB Output is correct
19 Correct 2 ms 8536 KB Output is correct
20 Correct 2 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8540 KB Output is correct
24 Correct 2 ms 8540 KB Output is correct
25 Correct 2 ms 8536 KB Output is correct
26 Correct 2 ms 8540 KB Output is correct
27 Correct 1 ms 8540 KB Output is correct
28 Correct 1 ms 8600 KB Output is correct
29 Correct 2 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8536 KB Output is correct
2 Correct 3 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8536 KB Output is correct
7 Correct 2 ms 8536 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 2 ms 8632 KB Output is correct
15 Correct 2 ms 8540 KB Output is correct
16 Correct 2 ms 8540 KB Output is correct
17 Correct 2 ms 8536 KB Output is correct
18 Correct 2 ms 8536 KB Output is correct
19 Correct 2 ms 8536 KB Output is correct
20 Correct 2 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8540 KB Output is correct
24 Correct 2 ms 8540 KB Output is correct
25 Correct 2 ms 8536 KB Output is correct
26 Correct 2 ms 8540 KB Output is correct
27 Correct 1 ms 8540 KB Output is correct
28 Correct 1 ms 8600 KB Output is correct
29 Correct 2 ms 8540 KB Output is correct
30 Correct 4 ms 8792 KB Output is correct
31 Correct 4 ms 8788 KB Output is correct
32 Correct 5 ms 9044 KB Output is correct
33 Correct 4 ms 8796 KB Output is correct
34 Correct 5 ms 8796 KB Output is correct
35 Correct 4 ms 8792 KB Output is correct
36 Correct 4 ms 8796 KB Output is correct
37 Correct 4 ms 8796 KB Output is correct
38 Correct 4 ms 8796 KB Output is correct
39 Correct 4 ms 8796 KB Output is correct
40 Correct 5 ms 8796 KB Output is correct
41 Correct 4 ms 8796 KB Output is correct
42 Correct 4 ms 8952 KB Output is correct
43 Correct 4 ms 8796 KB Output is correct
44 Correct 4 ms 8796 KB Output is correct
45 Correct 4 ms 8796 KB Output is correct
46 Correct 4 ms 8796 KB Output is correct
47 Correct 4 ms 8796 KB Output is correct
48 Correct 5 ms 8824 KB Output is correct
49 Correct 4 ms 8796 KB Output is correct
50 Correct 3 ms 8796 KB Output is correct
51 Correct 3 ms 8824 KB Output is correct
52 Correct 3 ms 8792 KB Output is correct
53 Correct 3 ms 8796 KB Output is correct
54 Correct 3 ms 8796 KB Output is correct
55 Correct 3 ms 8796 KB Output is correct
56 Correct 3 ms 8540 KB Output is correct
57 Correct 2 ms 8540 KB Output is correct
58 Correct 4 ms 8536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 2 ms 8536 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 130 ms 18292 KB Output is correct
5 Correct 100 ms 21732 KB Output is correct
6 Correct 135 ms 23124 KB Output is correct
7 Correct 170 ms 26196 KB Output is correct
8 Correct 167 ms 26192 KB Output is correct
9 Correct 171 ms 26164 KB Output is correct
10 Correct 172 ms 26080 KB Output is correct
11 Correct 167 ms 26172 KB Output is correct
12 Correct 100 ms 25292 KB Output is correct
13 Correct 98 ms 25212 KB Output is correct
14 Correct 96 ms 25172 KB Output is correct
15 Correct 37 ms 21496 KB Output is correct
16 Correct 163 ms 23692 KB Output is correct
17 Correct 31 ms 13140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 125 ms 11100 KB Output is correct
3 Correct 201 ms 11520 KB Output is correct
4 Correct 153 ms 11612 KB Output is correct
5 Correct 246 ms 12628 KB Output is correct
6 Correct 243 ms 12880 KB Output is correct
7 Correct 242 ms 12864 KB Output is correct
8 Correct 236 ms 12864 KB Output is correct
9 Correct 248 ms 12824 KB Output is correct
10 Correct 244 ms 12872 KB Output is correct
11 Correct 243 ms 13136 KB Output is correct
12 Correct 244 ms 12952 KB Output is correct
13 Correct 241 ms 13172 KB Output is correct
14 Correct 257 ms 13704 KB Output is correct
15 Correct 262 ms 13144 KB Output is correct
16 Correct 236 ms 13172 KB Output is correct
17 Correct 245 ms 13172 KB Output is correct
18 Correct 231 ms 12984 KB Output is correct
19 Correct 292 ms 12912 KB Output is correct
20 Correct 286 ms 12888 KB Output is correct
21 Correct 296 ms 12880 KB Output is correct
22 Correct 285 ms 12896 KB Output is correct
23 Correct 285 ms 12880 KB Output is correct
24 Correct 292 ms 13008 KB Output is correct
25 Correct 286 ms 12624 KB Output is correct
26 Correct 290 ms 12892 KB Output is correct
27 Correct 278 ms 12880 KB Output is correct
28 Correct 291 ms 12892 KB Output is correct
29 Correct 302 ms 12952 KB Output is correct
30 Correct 299 ms 12888 KB Output is correct
31 Correct 294 ms 13208 KB Output is correct
32 Correct 290 ms 13404 KB Output is correct
33 Correct 299 ms 13904 KB Output is correct
34 Correct 316 ms 13468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 397 ms 14720 KB Output is correct
5 Correct 385 ms 15956 KB Output is correct
6 Correct 459 ms 17556 KB Output is correct
7 Correct 479 ms 18340 KB Output is correct
8 Correct 472 ms 18372 KB Output is correct
9 Correct 484 ms 18352 KB Output is correct
10 Correct 463 ms 18260 KB Output is correct
11 Correct 472 ms 18340 KB Output is correct
12 Correct 474 ms 18344 KB Output is correct
13 Correct 510 ms 18260 KB Output is correct
14 Correct 32 ms 13140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8536 KB Output is correct
2 Correct 3 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8536 KB Output is correct
7 Correct 2 ms 8536 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 2 ms 8632 KB Output is correct
15 Correct 2 ms 8540 KB Output is correct
16 Correct 2 ms 8540 KB Output is correct
17 Correct 2 ms 8536 KB Output is correct
18 Correct 2 ms 8536 KB Output is correct
19 Correct 2 ms 8536 KB Output is correct
20 Correct 2 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8540 KB Output is correct
24 Correct 2 ms 8540 KB Output is correct
25 Correct 2 ms 8536 KB Output is correct
26 Correct 2 ms 8540 KB Output is correct
27 Correct 1 ms 8540 KB Output is correct
28 Correct 1 ms 8600 KB Output is correct
29 Correct 2 ms 8540 KB Output is correct
30 Correct 4 ms 8792 KB Output is correct
31 Correct 4 ms 8788 KB Output is correct
32 Correct 5 ms 9044 KB Output is correct
33 Correct 4 ms 8796 KB Output is correct
34 Correct 5 ms 8796 KB Output is correct
35 Correct 4 ms 8792 KB Output is correct
36 Correct 4 ms 8796 KB Output is correct
37 Correct 4 ms 8796 KB Output is correct
38 Correct 4 ms 8796 KB Output is correct
39 Correct 4 ms 8796 KB Output is correct
40 Correct 5 ms 8796 KB Output is correct
41 Correct 4 ms 8796 KB Output is correct
42 Correct 4 ms 8952 KB Output is correct
43 Correct 4 ms 8796 KB Output is correct
44 Correct 4 ms 8796 KB Output is correct
45 Correct 4 ms 8796 KB Output is correct
46 Correct 4 ms 8796 KB Output is correct
47 Correct 4 ms 8796 KB Output is correct
48 Correct 5 ms 8824 KB Output is correct
49 Correct 4 ms 8796 KB Output is correct
50 Correct 3 ms 8796 KB Output is correct
51 Correct 3 ms 8824 KB Output is correct
52 Correct 3 ms 8792 KB Output is correct
53 Correct 3 ms 8796 KB Output is correct
54 Correct 3 ms 8796 KB Output is correct
55 Correct 3 ms 8796 KB Output is correct
56 Correct 3 ms 8540 KB Output is correct
57 Correct 2 ms 8540 KB Output is correct
58 Correct 4 ms 8536 KB Output is correct
59 Correct 1 ms 8536 KB Output is correct
60 Correct 2 ms 8536 KB Output is correct
61 Correct 2 ms 8540 KB Output is correct
62 Correct 130 ms 18292 KB Output is correct
63 Correct 100 ms 21732 KB Output is correct
64 Correct 135 ms 23124 KB Output is correct
65 Correct 170 ms 26196 KB Output is correct
66 Correct 167 ms 26192 KB Output is correct
67 Correct 171 ms 26164 KB Output is correct
68 Correct 172 ms 26080 KB Output is correct
69 Correct 167 ms 26172 KB Output is correct
70 Correct 100 ms 25292 KB Output is correct
71 Correct 98 ms 25212 KB Output is correct
72 Correct 96 ms 25172 KB Output is correct
73 Correct 37 ms 21496 KB Output is correct
74 Correct 163 ms 23692 KB Output is correct
75 Correct 31 ms 13140 KB Output is correct
76 Correct 1 ms 8536 KB Output is correct
77 Correct 125 ms 11100 KB Output is correct
78 Correct 201 ms 11520 KB Output is correct
79 Correct 153 ms 11612 KB Output is correct
80 Correct 246 ms 12628 KB Output is correct
81 Correct 243 ms 12880 KB Output is correct
82 Correct 242 ms 12864 KB Output is correct
83 Correct 236 ms 12864 KB Output is correct
84 Correct 248 ms 12824 KB Output is correct
85 Correct 244 ms 12872 KB Output is correct
86 Correct 243 ms 13136 KB Output is correct
87 Correct 244 ms 12952 KB Output is correct
88 Correct 241 ms 13172 KB Output is correct
89 Correct 257 ms 13704 KB Output is correct
90 Correct 262 ms 13144 KB Output is correct
91 Correct 236 ms 13172 KB Output is correct
92 Correct 245 ms 13172 KB Output is correct
93 Correct 231 ms 12984 KB Output is correct
94 Correct 292 ms 12912 KB Output is correct
95 Correct 286 ms 12888 KB Output is correct
96 Correct 296 ms 12880 KB Output is correct
97 Correct 285 ms 12896 KB Output is correct
98 Correct 285 ms 12880 KB Output is correct
99 Correct 292 ms 13008 KB Output is correct
100 Correct 286 ms 12624 KB Output is correct
101 Correct 290 ms 12892 KB Output is correct
102 Correct 278 ms 12880 KB Output is correct
103 Correct 291 ms 12892 KB Output is correct
104 Correct 302 ms 12952 KB Output is correct
105 Correct 299 ms 12888 KB Output is correct
106 Correct 294 ms 13208 KB Output is correct
107 Correct 290 ms 13404 KB Output is correct
108 Correct 299 ms 13904 KB Output is correct
109 Correct 316 ms 13468 KB Output is correct
110 Correct 2 ms 8536 KB Output is correct
111 Correct 1 ms 8540 KB Output is correct
112 Correct 2 ms 8540 KB Output is correct
113 Correct 397 ms 14720 KB Output is correct
114 Correct 385 ms 15956 KB Output is correct
115 Correct 459 ms 17556 KB Output is correct
116 Correct 479 ms 18340 KB Output is correct
117 Correct 472 ms 18372 KB Output is correct
118 Correct 484 ms 18352 KB Output is correct
119 Correct 463 ms 18260 KB Output is correct
120 Correct 472 ms 18340 KB Output is correct
121 Correct 474 ms 18344 KB Output is correct
122 Correct 510 ms 18260 KB Output is correct
123 Correct 32 ms 13140 KB Output is correct
124 Correct 248 ms 17988 KB Output is correct
125 Correct 173 ms 15952 KB Output is correct
126 Correct 271 ms 18452 KB Output is correct
127 Correct 272 ms 18456 KB Output is correct
128 Correct 288 ms 18468 KB Output is correct
129 Correct 272 ms 18516 KB Output is correct
130 Correct 270 ms 18512 KB Output is correct
131 Correct 184 ms 24996 KB Output is correct
132 Correct 192 ms 26072 KB Output is correct
133 Correct 184 ms 22352 KB Output is correct
134 Correct 315 ms 18512 KB Output is correct
135 Correct 330 ms 18536 KB Output is correct
136 Correct 309 ms 18536 KB Output is correct
137 Correct 107 ms 18632 KB Output is correct
138 Correct 108 ms 18712 KB Output is correct
139 Correct 103 ms 18636 KB Output is correct
140 Correct 108 ms 18632 KB Output is correct
141 Correct 119 ms 18832 KB Output is correct
142 Correct 106 ms 18636 KB Output is correct
143 Correct 39 ms 13784 KB Output is correct
144 Correct 261 ms 15956 KB Output is correct