Submission #763591

# Submission time Handle Problem Language Result Execution time Memory
763591 2023-06-22T13:36:50 Z CDuong Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 44044 KB
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")


#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
// - insert(x)
// - find_by_order(k): return iterator to the k-th smallest element
// - order_of_key(x): the number of elements that are strictly smaller

const int mxN = 1e5 + 5;
const int mod = 1e9 + 7;
const int block = 350;
const ll oo = 1e18;

struct query {
    int l, r, idx;
    query() {};
    query(int l, int r, int idx) : l(l), r(r), idx(idx) {}
    bool operator < (const query &o) const {
        int cur_blk = l / block;
        if(cur_blk != o.l / block) return l < o.l;
        return (cur_blk & 1 ? r > o.r : r < o.r);
    }
};

vi G[mxN];
ordered_set cur_tree;
pii euler_tour[18][2 * mxN];
int n, m, q, c[mxN], res, ans[mxN], cnt[mxN], cur_tree_sz;
int tin[mxN], tout[mxN], dep[mxN], frst_ap[mxN], euler_cnt, timer;
vector<query> queries;

void dfs(int v, int p) {
    tin[v] = ++timer;
    dep[v] = dep[p] + 1;
    frst_ap[v] = ++euler_cnt;
    euler_tour[0][euler_cnt] = {dep[v], v};

    for(int u : G[v]) {
        if(u == p) continue;
        dfs(u, v);
        euler_tour[0][++euler_cnt] = {dep[v], v};
    }

    tout[v] = timer;
}

void build_sparse() {
    for(int i = 1; (1 << i) <= euler_cnt; ++i) {
        for(int j = 1; j + (1 << i) - 1 <= euler_cnt; ++j) {
            euler_tour[i][j] = min(euler_tour[i - 1][j], euler_tour[i - 1][j + (1 << (i - 1))]);
        }
    }
}

int lca(int u, int v) {
    int l = frst_ap[u], r = frst_ap[v];
    if(l > r) swap(l, r);
    int len = r - l + 1;
    int lg_len = 31 - __builtin_clz(len);
    return min(euler_tour[lg_len][l], euler_tour[lg_len][r - (1 << lg_len) + 1]).ss;
}

int dis(int u, int v) {
    int acl = lca(u, v);
    return (dep[u] + dep[v] - 2 * dep[acl]);
}

int ll_nxt[mxN], ll_pre[mxN];

void add_linked_list(int idx, int nxt) {
    int pre = ll_pre[nxt];
    res += dis(pre, idx);
    // cout << pre << " " << idx << " " << res << endl;
    res += dis(nxt, idx);
    // cout << pre << " " << idx << " " << res << endl;
    res -= dis(pre, nxt);
    ll_nxt[pre] = ll_pre[nxt] = idx;
    ll_nxt[idx] = nxt, ll_pre[idx] = pre;
}

void del_linked_list(int idx) {
    int pre = ll_pre[idx], nxt = ll_nxt[idx];
    res -= dis(pre, idx);
    res -= dis(nxt, idx);
    res += dis(pre, nxt);
    ll_nxt[pre] = nxt, ll_pre[nxt] = pre;
}

void add(int idx) {
    ++cnt[c[idx]];
    if(cnt[c[idx]] > 1) return;
    int val = tin[c[idx]];
    if(cur_tree_sz == 0) {
        ++cur_tree_sz;
        cur_tree.insert({val, c[idx]});
        ll_nxt[c[idx]] = ll_pre[c[idx]] = c[idx];
        return;
    }
    int cur = cur_tree.order_of_key({val, 0}), nxt;
    if(cur == cur_tree_sz) nxt = (*(cur_tree.find_by_order(0))).ss;
    else nxt = (*(cur_tree.find_by_order(cur))).ss;
    // cout << c[idx] << " " << nxt << endl;
    add_linked_list(c[idx], nxt);
    cur_tree.insert({val, c[idx]});
    ++cur_tree_sz;
}

void del(int idx) {
    --cnt[c[idx]];
    if(cnt[c[idx]]) return;
    int val = tin[c[idx]];
    cur_tree.erase({val, c[idx]});
    --cur_tree_sz;
    if(cur_tree.empty()) return;
    del_linked_list(c[idx]);
}

void solve() {
    cin >> n >> m >> q;
    for(int i = 1; i < n; ++i) {
        int u, v; cin >> u >> v;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }
    dfs(1, 0);
    build_sparse();
    for(int i = 1; i <= m; ++i) {
        cin >> c[i];
    }
    for(int i = 1; i <= q; ++i) {
        int l, r; cin >> l >> r;
        queries.emplace_back(l, r, i);
    }
    sort(all(queries));
    int cur_l = 1, cur_r = 0;
    for(auto &[l, r, idx] : queries) {
        while(cur_l > l) add(--cur_l);
        while(cur_r < r) add(++cur_r);

        while(cur_l < l) del(cur_l++);
        while(cur_r > r) del(cur_r--);

        // cout << res << endl;
        // for(auto tmp : cur_tree) {
        //     cout << tmp.ff << " " << tmp.ss << endl;
        // }
        // cout << endl;

        ans[idx] = (res >> 1) + 1;
    }
    for(int i = 1; i <= q; ++i) {
        cout << ans[i] << "\n";
    }
}

signed main() {

#ifndef CDuongg
    if(fopen(taskname".inp", "r"))
        assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
    freopen("bai3.inp", "r", stdin);
    freopen("bai3.out", "w", stdout);
    auto start = chrono::high_resolution_clock::now();
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1; //cin >> t;
    while(t--) solve();

#ifdef CDuongg
    auto end = chrono::high_resolution_clock::now();
    cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
    cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif

}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2772 KB Output is correct
5 Correct 3 ms 2772 KB Output is correct
6 Correct 4 ms 2772 KB Output is correct
7 Correct 3 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 4 ms 2772 KB Output is correct
10 Correct 4 ms 2772 KB Output is correct
11 Correct 4 ms 2772 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 5 ms 2872 KB Output is correct
16 Correct 4 ms 2772 KB Output is correct
17 Correct 4 ms 2772 KB Output is correct
18 Correct 4 ms 2816 KB Output is correct
19 Correct 4 ms 2772 KB Output is correct
20 Correct 4 ms 2772 KB Output is correct
21 Correct 4 ms 2772 KB Output is correct
22 Correct 5 ms 2772 KB Output is correct
23 Correct 4 ms 2864 KB Output is correct
24 Correct 4 ms 2772 KB Output is correct
25 Correct 4 ms 2772 KB Output is correct
26 Correct 4 ms 2772 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2772 KB Output is correct
5 Correct 3 ms 2772 KB Output is correct
6 Correct 4 ms 2772 KB Output is correct
7 Correct 3 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 4 ms 2772 KB Output is correct
10 Correct 4 ms 2772 KB Output is correct
11 Correct 4 ms 2772 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 5 ms 2872 KB Output is correct
16 Correct 4 ms 2772 KB Output is correct
17 Correct 4 ms 2772 KB Output is correct
18 Correct 4 ms 2816 KB Output is correct
19 Correct 4 ms 2772 KB Output is correct
20 Correct 4 ms 2772 KB Output is correct
21 Correct 4 ms 2772 KB Output is correct
22 Correct 5 ms 2772 KB Output is correct
23 Correct 4 ms 2864 KB Output is correct
24 Correct 4 ms 2772 KB Output is correct
25 Correct 4 ms 2772 KB Output is correct
26 Correct 4 ms 2772 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 22 ms 3192 KB Output is correct
31 Correct 26 ms 3224 KB Output is correct
32 Correct 32 ms 3364 KB Output is correct
33 Correct 32 ms 3284 KB Output is correct
34 Correct 31 ms 3348 KB Output is correct
35 Correct 4 ms 3380 KB Output is correct
36 Correct 4 ms 3284 KB Output is correct
37 Correct 4 ms 3284 KB Output is correct
38 Correct 32 ms 3456 KB Output is correct
39 Correct 32 ms 3412 KB Output is correct
40 Correct 35 ms 3532 KB Output is correct
41 Correct 4 ms 3412 KB Output is correct
42 Correct 4 ms 3412 KB Output is correct
43 Correct 4 ms 3412 KB Output is correct
44 Correct 34 ms 3428 KB Output is correct
45 Correct 33 ms 3412 KB Output is correct
46 Correct 33 ms 3424 KB Output is correct
47 Correct 5 ms 3412 KB Output is correct
48 Correct 4 ms 3412 KB Output is correct
49 Correct 4 ms 3412 KB Output is correct
50 Correct 31 ms 3364 KB Output is correct
51 Correct 29 ms 3284 KB Output is correct
52 Correct 29 ms 3284 KB Output is correct
53 Correct 32 ms 3468 KB Output is correct
54 Correct 37 ms 3376 KB Output is correct
55 Correct 31 ms 3372 KB Output is correct
56 Correct 3 ms 2772 KB Output is correct
57 Correct 2 ms 3284 KB Output is correct
58 Correct 4 ms 3284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 3 ms 2772 KB Output is correct
4 Correct 4677 ms 28804 KB Output is correct
5 Correct 3532 ms 38740 KB Output is correct
6 Execution timed out 5026 ms 44044 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 78 ms 19860 KB Output is correct
3 Correct 107 ms 21488 KB Output is correct
4 Correct 90 ms 24148 KB Output is correct
5 Correct 91 ms 39112 KB Output is correct
6 Correct 147 ms 38232 KB Output is correct
7 Correct 152 ms 36336 KB Output is correct
8 Correct 123 ms 35616 KB Output is correct
9 Correct 123 ms 35288 KB Output is correct
10 Correct 122 ms 35268 KB Output is correct
11 Correct 144 ms 35180 KB Output is correct
12 Correct 158 ms 35280 KB Output is correct
13 Correct 152 ms 35428 KB Output is correct
14 Correct 158 ms 35880 KB Output is correct
15 Correct 175 ms 37008 KB Output is correct
16 Correct 155 ms 37616 KB Output is correct
17 Correct 144 ms 37576 KB Output is correct
18 Correct 152 ms 37632 KB Output is correct
19 Correct 102 ms 39160 KB Output is correct
20 Correct 94 ms 39156 KB Output is correct
21 Correct 121 ms 37292 KB Output is correct
22 Correct 119 ms 36296 KB Output is correct
23 Correct 119 ms 35740 KB Output is correct
24 Correct 113 ms 35420 KB Output is correct
25 Correct 118 ms 35340 KB Output is correct
26 Correct 110 ms 35272 KB Output is correct
27 Correct 128 ms 35320 KB Output is correct
28 Correct 136 ms 35252 KB Output is correct
29 Correct 149 ms 35292 KB Output is correct
30 Correct 149 ms 35424 KB Output is correct
31 Correct 147 ms 35492 KB Output is correct
32 Correct 158 ms 35772 KB Output is correct
33 Correct 142 ms 36208 KB Output is correct
34 Correct 145 ms 37132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2612 KB Output is correct
3 Correct 3 ms 2772 KB Output is correct
4 Execution timed out 5022 ms 24588 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2772 KB Output is correct
5 Correct 3 ms 2772 KB Output is correct
6 Correct 4 ms 2772 KB Output is correct
7 Correct 3 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 4 ms 2772 KB Output is correct
10 Correct 4 ms 2772 KB Output is correct
11 Correct 4 ms 2772 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 5 ms 2872 KB Output is correct
16 Correct 4 ms 2772 KB Output is correct
17 Correct 4 ms 2772 KB Output is correct
18 Correct 4 ms 2816 KB Output is correct
19 Correct 4 ms 2772 KB Output is correct
20 Correct 4 ms 2772 KB Output is correct
21 Correct 4 ms 2772 KB Output is correct
22 Correct 5 ms 2772 KB Output is correct
23 Correct 4 ms 2864 KB Output is correct
24 Correct 4 ms 2772 KB Output is correct
25 Correct 4 ms 2772 KB Output is correct
26 Correct 4 ms 2772 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 22 ms 3192 KB Output is correct
31 Correct 26 ms 3224 KB Output is correct
32 Correct 32 ms 3364 KB Output is correct
33 Correct 32 ms 3284 KB Output is correct
34 Correct 31 ms 3348 KB Output is correct
35 Correct 4 ms 3380 KB Output is correct
36 Correct 4 ms 3284 KB Output is correct
37 Correct 4 ms 3284 KB Output is correct
38 Correct 32 ms 3456 KB Output is correct
39 Correct 32 ms 3412 KB Output is correct
40 Correct 35 ms 3532 KB Output is correct
41 Correct 4 ms 3412 KB Output is correct
42 Correct 4 ms 3412 KB Output is correct
43 Correct 4 ms 3412 KB Output is correct
44 Correct 34 ms 3428 KB Output is correct
45 Correct 33 ms 3412 KB Output is correct
46 Correct 33 ms 3424 KB Output is correct
47 Correct 5 ms 3412 KB Output is correct
48 Correct 4 ms 3412 KB Output is correct
49 Correct 4 ms 3412 KB Output is correct
50 Correct 31 ms 3364 KB Output is correct
51 Correct 29 ms 3284 KB Output is correct
52 Correct 29 ms 3284 KB Output is correct
53 Correct 32 ms 3468 KB Output is correct
54 Correct 37 ms 3376 KB Output is correct
55 Correct 31 ms 3372 KB Output is correct
56 Correct 3 ms 2772 KB Output is correct
57 Correct 2 ms 3284 KB Output is correct
58 Correct 4 ms 3284 KB Output is correct
59 Correct 2 ms 2644 KB Output is correct
60 Correct 2 ms 2644 KB Output is correct
61 Correct 3 ms 2772 KB Output is correct
62 Correct 4677 ms 28804 KB Output is correct
63 Correct 3532 ms 38740 KB Output is correct
64 Execution timed out 5026 ms 44044 KB Time limit exceeded
65 Halted 0 ms 0 KB -