Submission #916666

# Submission time Handle Problem Language Result Execution time Memory
916666 2024-01-26T09:01:17 Z thangdz2k7 Tourism (JOI23_tourism) C++17
34 / 100
5000 ms 20564 KB
#include <bits/stdc++.h>
#define ii pair <int, int>
#define iii pair <ii, int>
#define F first
#define S second
#define pb push_back

using namespace std;

const int N = 1e5 + 5;

int n, m, q;
int c[N], l[N], r[N];
vector <int> adj[N], query[N];

int bit[N];

void update(int u, int v){
    while (u <= m){
        bit[u] += v;
        u += (u & (-u));
    }
}

int get(int u){
    int ans = 0;
    while (u > 0){
        ans += bit[u];
        u -= (u & (-u));
    }

    return ans;
}

int par[N], heavy[N], depth[N];

int dfs(int u, int pa){
    int sz = 0;
    par[u] = pa;
    int maxsz = 0;
    depth[u] = depth[pa] + 1;

    for (int v : adj[u]){
        if (v == pa) continue;
        int cur = dfs(v, u);
        sz += cur;
        if (maxsz < cur){
            cur = maxsz;
            heavy[u] = v;

        }
    }

    return sz;
}

int pos[N], head[N], cu;
set <ii> s;

void decompose(int u, int h){
    head[u] = h;
    ++ cu;
    pos[u] = cu;
    s.insert(ii(cu, cu));

    if (heavy[u]){
        decompose(heavy[u], h);
    }

    for (int v : adj[u]){
        if (v != par[u] && v != heavy[u]) decompose(v, v);
    }
}

int cur[N];

void add(int l, int r, int val){
    while (true){
        ii z = *s.lower_bound(ii(l, -1));
        if (z.F > r) break;
        s.erase(z);
        if (z.S > r) {
            s.insert(ii(r + 1, z.S));
            int le = r - z.F + 1;
            update(cur[z.F] + 1, le);
            update(val + 1, -le);
            cur[r + 1] = cur[z.F];
        }
        else {
            int le = z.S - z.F + 1;
            update(cur[z.F] + 1, le);
            update(val + 1, -le);
        }
    }

    ii z = *(--s.lower_bound(ii(l, -1)));
    if (z.S > r){
        s.erase(z);
        s.insert(ii(z.F, l - 1));
        s.insert(ii(r + 1, z.S));
        int le = r - l + 1;
        update(cur[z.F] + 1, le);
        update(val + 1, -le);
    }

    else if (z.S >= l){
         s.erase(z);
         s.insert(ii(z.F, l - 1));
         int le = z.S - l + 1;
         update(cur[z.F] + 1, le);
         update(val + 1, -le);
    }

    s.insert(ii(l, r));
    cur[l] = val;
}

void upd(int u, int v, int val){
    for (; head[u] != head[v]; v = par[head[v]]){
        if (depth[head[u]] > depth[head[v]]) swap(u, v);
        add(pos[head[v]], pos[v], val);

    }

    if (depth[u] > depth[v]) swap(u, v);
    add(pos[u], pos[v], val);
}

int ans[N];

void solve(){
    cin >> n >> m >> q;
    for (int i = 1, u, v; i <= n - 1; ++ i){
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    for (int i = 1; i <= m; ++ i) cin >> c[i];
    for (int i = 1, r; i <= q; ++ i){
        cin >> l[i] >> r;
        query[r].pb(i);
    }

    dfs(1, 0);
    decompose(1, 1);

    s.insert(ii(0, 0)), s.insert(ii(n + 1, n + 1));

    for (int i = 1; i <= m; ++ i){
        if (i > 1) upd(c[i], c[i - 1], i - 1);
        for (int k : query[i]){
            ans[k] = max(1, get(l[k]));
        }
    }

    for (int i = 1; i <= q; ++ i) cout << ans[i] << '\n';
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 3 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 3 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 3 ms 8636 KB Output is correct
10 Correct 3 ms 8540 KB Output is correct
11 Correct 3 ms 8540 KB Output is correct
12 Correct 4 ms 8540 KB Output is correct
13 Correct 3 ms 8608 KB Output is correct
14 Correct 3 ms 8540 KB Output is correct
15 Correct 9 ms 8540 KB Output is correct
16 Correct 9 ms 8540 KB Output is correct
17 Correct 9 ms 8632 KB Output is correct
18 Correct 6 ms 8540 KB Output is correct
19 Correct 8 ms 8540 KB Output is correct
20 Correct 10 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 3 ms 8536 KB Output is correct
26 Correct 2 ms 8540 KB Output is correct
27 Correct 2 ms 8536 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 3 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 3 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 3 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 3 ms 8636 KB Output is correct
10 Correct 3 ms 8540 KB Output is correct
11 Correct 3 ms 8540 KB Output is correct
12 Correct 4 ms 8540 KB Output is correct
13 Correct 3 ms 8608 KB Output is correct
14 Correct 3 ms 8540 KB Output is correct
15 Correct 9 ms 8540 KB Output is correct
16 Correct 9 ms 8540 KB Output is correct
17 Correct 9 ms 8632 KB Output is correct
18 Correct 6 ms 8540 KB Output is correct
19 Correct 8 ms 8540 KB Output is correct
20 Correct 10 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 3 ms 8536 KB Output is correct
26 Correct 2 ms 8540 KB Output is correct
27 Correct 2 ms 8536 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 3 ms 8540 KB Output is correct
30 Correct 8 ms 8792 KB Output is correct
31 Correct 9 ms 8796 KB Output is correct
32 Correct 13 ms 8884 KB Output is correct
33 Correct 12 ms 8796 KB Output is correct
34 Correct 11 ms 8792 KB Output is correct
35 Correct 11 ms 8796 KB Output is correct
36 Correct 12 ms 8792 KB Output is correct
37 Correct 12 ms 8796 KB Output is correct
38 Correct 394 ms 8796 KB Output is correct
39 Correct 404 ms 9040 KB Output is correct
40 Correct 408 ms 8972 KB Output is correct
41 Correct 387 ms 8792 KB Output is correct
42 Correct 393 ms 8976 KB Output is correct
43 Correct 392 ms 8796 KB Output is correct
44 Correct 186 ms 8796 KB Output is correct
45 Correct 152 ms 8892 KB Output is correct
46 Correct 174 ms 8920 KB Output is correct
47 Correct 162 ms 8872 KB Output is correct
48 Correct 134 ms 9048 KB Output is correct
49 Correct 185 ms 8888 KB Output is correct
50 Correct 5 ms 8796 KB Output is correct
51 Correct 5 ms 8796 KB Output is correct
52 Correct 5 ms 8792 KB Output is correct
53 Correct 5 ms 8792 KB Output is correct
54 Correct 5 ms 8796 KB Output is correct
55 Correct 5 ms 8796 KB Output is correct
56 Correct 2 ms 8540 KB Output is correct
57 Correct 3 ms 8792 KB Output is correct
58 Correct 11 ms 8796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Execution timed out 5055 ms 19508 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 501 ms 13528 KB Output is correct
3 Correct 842 ms 14024 KB Output is correct
4 Correct 636 ms 14352 KB Output is correct
5 Correct 1008 ms 16976 KB Output is correct
6 Correct 1002 ms 16980 KB Output is correct
7 Correct 1014 ms 16980 KB Output is correct
8 Correct 994 ms 16980 KB Output is correct
9 Correct 1058 ms 17236 KB Output is correct
10 Correct 1032 ms 16988 KB Output is correct
11 Correct 1006 ms 17012 KB Output is correct
12 Correct 1071 ms 17152 KB Output is correct
13 Correct 1035 ms 17568 KB Output is correct
14 Correct 1018 ms 18000 KB Output is correct
15 Correct 1040 ms 20564 KB Output is correct
16 Correct 1045 ms 17520 KB Output is correct
17 Correct 1104 ms 17804 KB Output is correct
18 Correct 1002 ms 17228 KB Output is correct
19 Execution timed out 5036 ms 16852 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Correct 1070 ms 15824 KB Output is correct
5 Correct 1104 ms 15880 KB Output is correct
6 Correct 1295 ms 18516 KB Output is correct
7 Correct 1292 ms 19568 KB Output is correct
8 Correct 1297 ms 19644 KB Output is correct
9 Correct 1359 ms 20048 KB Output is correct
10 Correct 1315 ms 19536 KB Output is correct
11 Correct 1377 ms 19568 KB Output is correct
12 Correct 1368 ms 19576 KB Output is correct
13 Correct 1332 ms 19544 KB Output is correct
14 Correct 38 ms 11232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 3 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 3 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 3 ms 8636 KB Output is correct
10 Correct 3 ms 8540 KB Output is correct
11 Correct 3 ms 8540 KB Output is correct
12 Correct 4 ms 8540 KB Output is correct
13 Correct 3 ms 8608 KB Output is correct
14 Correct 3 ms 8540 KB Output is correct
15 Correct 9 ms 8540 KB Output is correct
16 Correct 9 ms 8540 KB Output is correct
17 Correct 9 ms 8632 KB Output is correct
18 Correct 6 ms 8540 KB Output is correct
19 Correct 8 ms 8540 KB Output is correct
20 Correct 10 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 3 ms 8536 KB Output is correct
26 Correct 2 ms 8540 KB Output is correct
27 Correct 2 ms 8536 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 3 ms 8540 KB Output is correct
30 Correct 8 ms 8792 KB Output is correct
31 Correct 9 ms 8796 KB Output is correct
32 Correct 13 ms 8884 KB Output is correct
33 Correct 12 ms 8796 KB Output is correct
34 Correct 11 ms 8792 KB Output is correct
35 Correct 11 ms 8796 KB Output is correct
36 Correct 12 ms 8792 KB Output is correct
37 Correct 12 ms 8796 KB Output is correct
38 Correct 394 ms 8796 KB Output is correct
39 Correct 404 ms 9040 KB Output is correct
40 Correct 408 ms 8972 KB Output is correct
41 Correct 387 ms 8792 KB Output is correct
42 Correct 393 ms 8976 KB Output is correct
43 Correct 392 ms 8796 KB Output is correct
44 Correct 186 ms 8796 KB Output is correct
45 Correct 152 ms 8892 KB Output is correct
46 Correct 174 ms 8920 KB Output is correct
47 Correct 162 ms 8872 KB Output is correct
48 Correct 134 ms 9048 KB Output is correct
49 Correct 185 ms 8888 KB Output is correct
50 Correct 5 ms 8796 KB Output is correct
51 Correct 5 ms 8796 KB Output is correct
52 Correct 5 ms 8792 KB Output is correct
53 Correct 5 ms 8792 KB Output is correct
54 Correct 5 ms 8796 KB Output is correct
55 Correct 5 ms 8796 KB Output is correct
56 Correct 2 ms 8540 KB Output is correct
57 Correct 3 ms 8792 KB Output is correct
58 Correct 11 ms 8796 KB Output is correct
59 Correct 2 ms 8540 KB Output is correct
60 Correct 2 ms 8540 KB Output is correct
61 Correct 2 ms 8540 KB Output is correct
62 Execution timed out 5055 ms 19508 KB Time limit exceeded
63 Halted 0 ms 0 KB -