Submission #1019855

# Submission time Handle Problem Language Result Execution time Memory
1019855 2024-07-11T09:52:14 Z Pacybwoah Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 47924 KB
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cassert>
#include<set>
#include<utility>
using namespace std;
const int k = 300;
vector<vector<int>> graph;
vector<int> in, dep, pos;
vector<vector<int>> anc;
int tst = 0;
void dfs(int node, int parent){
    in[++tst] = node;
    pos[node] = tst;
    anc[0][node] = parent;
    dep[node] = dep[parent] + 1;
    for(auto x: graph[node]){
        if(x == parent) continue;
        dfs(x, node);
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    //assert(n <= 2000);
    in.resize(n + 1);
    dep.resize(n + 1);
    pos.resize(n + 1);
    anc.resize(18, vector<int>(n + 1));
    graph.resize(n + 1);
    int a, b;
    for(int i = 1; i < n; i++){
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    dfs(1, 1);
    vector<int> vec(m + 1);
    for(int i = 1; i <= m; i++) cin >> vec[i], vec[i] = pos[vec[i]];
    vector<int> ans(q);
    for(int i = 1; i < 18; i++){
        for(int j = 1; j <= n; j++){
            anc[i][j] = anc[i - 1][anc[i - 1][j]];
        }
    }
    auto lca = [&](int u, int v){
        u = in[u], v = in[v];
        if(dep[u] < dep[v]) swap(u, v);
        for(int i = 17; i >= 0; i--){
            if((1 << i) & (dep[u] - dep[v])) u = anc[i][u];
        }
        for(int i = 17; i >= 0; i--){
            if(anc[i][u] != anc[i][v]) u = anc[i][u], v = anc[i][v];
        }
        if(u != v) u = anc[0][u], v = anc[0][v];
        return pos[u];
    };
    auto dist = [&](int u, int v){
        return dep[in[u]] + dep[in[v]] - 2 * dep[in[lca(u, v)]];
    };
    auto left = [&](int aa, int bb, int cc){
        return (dist(aa, bb) + dist(aa, cc) - dist(bb, cc)) / 2;
    };
    vector<pair<pair<int, int>, int>> queries;
    for(int i = 0; i < q; i++){
        cin >> a >> b;
        queries.push_back(make_pair(make_pair(a, b), i));
    }
    auto cmp = [&](pair<pair<int, int>, int> c, pair<pair<int, int>, int> d){
        if((c.first.first - 1) / k == (d.first.first - 1) / k){
            if((c.first.first - 1) / k % 2 == 0) return c.first.second < d.first.second;
            else return c.first.second > d.first.second;
        }
        else return (c.first.first - 1) / k < (d.first.first - 1) / k;
    };
    sort(queries.begin(), queries.end(), cmp);
    int now_lca = 1;
    multiset<int> s;
    int cnt = 0;
    vector<vector<int>> st(18, vector<int>(m + 1));
    for(int i = 1; i <= m; i++) st[0][i] = vec[i];
    for(int i = 1; i < 18; i++){
        for(int j = 1; j <= m; j++) st[i][j] = lca(st[i - 1][j], st[i - 1][min(m, j + (1 << (i - 1)))]);
    }
    vector<int> bases(n + 1);
    int tmp = 0;
    for(int i = 1; i <= n; i++){
        if((1 << (tmp + 1)) <= i)  tmp++;
        bases[i] = tmp;
    }
    auto query = [&](int l, int r){
        tmp = r - l + 1;
        return lca(st[bases[tmp]][l], st[bases[tmp]][r - (1 << bases[tmp]) + 1]);
    };
    int lptr = 1, rptr = 0;
    auto add = [&](int node){
        if(s.empty()){
            now_lca = node;
            s.insert(node);
            cnt = 1;
            return;
        }
        auto iter = s.lower_bound(node);
        if(iter != s.end() && (*iter) == node){
            s.insert(node);
            return;
        }
        if(iter == s.begin()){
            cnt += left(node, (*iter), now_lca);
            now_lca = lca(node, now_lca);
            s.insert(node);
            return;
        }
        if(iter == s.end()){
            iter = prev(iter);
            cnt += left(node, (*iter), now_lca);
            now_lca = lca(node, now_lca);
            s.insert(node);
            return;
        }
        int bf = (*prev(iter)), gf = (*iter);
        cnt += left(node, bf, gf);
        s.insert(node);
        now_lca = lca(node, now_lca);
    };
    auto sub = [&](int node){
        auto iter2 = s.lower_bound(node);
        s.erase(iter2);
        if(s.empty()) return;
        auto iter = s.lower_bound(node);
        if(iter != s.end() && (*iter) == node){
            return;
        }
        if(iter == s.begin()){
            now_lca = query(lptr, rptr);
            cnt -= left(node, (*iter), now_lca);
            return;
        }
        if(iter == s.end()){
            iter = prev(iter);
            now_lca = query(lptr, rptr);
            cnt -= left(node, (*iter), now_lca);
            return;
        }
        int bf = (*prev(iter)), gf = (*iter);
        cnt -= left(node, bf, gf);
    };
    for(auto &x: queries){
        a = x.first.first, b = x.first.second;
        while(a < lptr) add(vec[--lptr]);
        while(rptr < b) add(vec[++rptr]);
        while(lptr < a) lptr++, sub(vec[lptr - 1]);
        while(b < rptr) rptr--, sub(vec[rptr + 1]);
        ans[x.second] = cnt;
        //cout << now_lca;
        //cout << a << " " << b << " " << cnt << "\n";
    }
    for(int i = 0; i < q; i++) cout << ans[i] << "\n";
}
// g++ -std=c++17 pC.cpp -o run -fsanitize=undefined -fsanitize=address -Wall -Wextra -Wshadow -Wfatal-errors
/*
7 6 2
1 2
1 3
2 4
2 5
3 6
3 7
2 3 6 4 5 7
1 3
4 6
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 4 ms 524 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 4 ms 348 KB Output is correct
7 Correct 4 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 8 ms 548 KB Output is correct
10 Correct 6 ms 348 KB Output is correct
11 Correct 7 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 10 ms 540 KB Output is correct
16 Correct 10 ms 456 KB Output is correct
17 Correct 6 ms 344 KB Output is correct
18 Correct 6 ms 344 KB Output is correct
19 Correct 6 ms 552 KB Output is correct
20 Correct 9 ms 348 KB Output is correct
21 Correct 6 ms 348 KB Output is correct
22 Correct 5 ms 476 KB Output is correct
23 Correct 5 ms 348 KB Output is correct
24 Correct 5 ms 348 KB Output is correct
25 Correct 6 ms 348 KB Output is correct
26 Correct 7 ms 348 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 4 ms 524 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 4 ms 348 KB Output is correct
7 Correct 4 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 8 ms 548 KB Output is correct
10 Correct 6 ms 348 KB Output is correct
11 Correct 7 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 10 ms 540 KB Output is correct
16 Correct 10 ms 456 KB Output is correct
17 Correct 6 ms 344 KB Output is correct
18 Correct 6 ms 344 KB Output is correct
19 Correct 6 ms 552 KB Output is correct
20 Correct 9 ms 348 KB Output is correct
21 Correct 6 ms 348 KB Output is correct
22 Correct 5 ms 476 KB Output is correct
23 Correct 5 ms 348 KB Output is correct
24 Correct 5 ms 348 KB Output is correct
25 Correct 6 ms 348 KB Output is correct
26 Correct 7 ms 348 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 49 ms 1112 KB Output is correct
31 Correct 59 ms 856 KB Output is correct
32 Correct 67 ms 856 KB Output is correct
33 Correct 65 ms 856 KB Output is correct
34 Correct 62 ms 860 KB Output is correct
35 Correct 8 ms 856 KB Output is correct
36 Correct 8 ms 896 KB Output is correct
37 Correct 9 ms 956 KB Output is correct
38 Correct 66 ms 1044 KB Output is correct
39 Correct 65 ms 1108 KB Output is correct
40 Correct 65 ms 1116 KB Output is correct
41 Correct 9 ms 1112 KB Output is correct
42 Correct 8 ms 1140 KB Output is correct
43 Correct 10 ms 1116 KB Output is correct
44 Correct 69 ms 860 KB Output is correct
45 Correct 65 ms 860 KB Output is correct
46 Correct 82 ms 856 KB Output is correct
47 Correct 8 ms 856 KB Output is correct
48 Correct 8 ms 856 KB Output is correct
49 Correct 8 ms 1080 KB Output is correct
50 Correct 59 ms 856 KB Output is correct
51 Correct 53 ms 1040 KB Output is correct
52 Correct 57 ms 856 KB Output is correct
53 Correct 59 ms 1036 KB Output is correct
54 Correct 56 ms 1004 KB Output is correct
55 Correct 56 ms 996 KB Output is correct
56 Correct 11 ms 604 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 6 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 11 ms 544 KB Output is correct
4 Runtime error 526 ms 47924 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 261 ms 13204 KB Output is correct
3 Correct 406 ms 16744 KB Output is correct
4 Correct 326 ms 16456 KB Output is correct
5 Correct 336 ms 28364 KB Output is correct
6 Correct 361 ms 26944 KB Output is correct
7 Correct 405 ms 24776 KB Output is correct
8 Correct 405 ms 24096 KB Output is correct
9 Correct 400 ms 23752 KB Output is correct
10 Correct 388 ms 23884 KB Output is correct
11 Correct 492 ms 23752 KB Output is correct
12 Correct 513 ms 23756 KB Output is correct
13 Correct 489 ms 24528 KB Output is correct
14 Correct 518 ms 24896 KB Output is correct
15 Correct 499 ms 26696 KB Output is correct
16 Correct 432 ms 26060 KB Output is correct
17 Correct 453 ms 26284 KB Output is correct
18 Correct 459 ms 26032 KB Output is correct
19 Correct 324 ms 28440 KB Output is correct
20 Correct 351 ms 28272 KB Output is correct
21 Correct 381 ms 25600 KB Output is correct
22 Correct 407 ms 24520 KB Output is correct
23 Correct 413 ms 24268 KB Output is correct
24 Correct 457 ms 23756 KB Output is correct
25 Correct 460 ms 23752 KB Output is correct
26 Correct 463 ms 23756 KB Output is correct
27 Correct 610 ms 23748 KB Output is correct
28 Correct 625 ms 23752 KB Output is correct
29 Correct 671 ms 24012 KB Output is correct
30 Correct 713 ms 24012 KB Output is correct
31 Correct 657 ms 24280 KB Output is correct
32 Correct 681 ms 24972 KB Output is correct
33 Correct 679 ms 25200 KB Output is correct
34 Correct 659 ms 26552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 11 ms 672 KB Output is correct
4 Execution timed out 5012 ms 21968 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 4 ms 524 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 4 ms 348 KB Output is correct
7 Correct 4 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 8 ms 548 KB Output is correct
10 Correct 6 ms 348 KB Output is correct
11 Correct 7 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 10 ms 540 KB Output is correct
16 Correct 10 ms 456 KB Output is correct
17 Correct 6 ms 344 KB Output is correct
18 Correct 6 ms 344 KB Output is correct
19 Correct 6 ms 552 KB Output is correct
20 Correct 9 ms 348 KB Output is correct
21 Correct 6 ms 348 KB Output is correct
22 Correct 5 ms 476 KB Output is correct
23 Correct 5 ms 348 KB Output is correct
24 Correct 5 ms 348 KB Output is correct
25 Correct 6 ms 348 KB Output is correct
26 Correct 7 ms 348 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 49 ms 1112 KB Output is correct
31 Correct 59 ms 856 KB Output is correct
32 Correct 67 ms 856 KB Output is correct
33 Correct 65 ms 856 KB Output is correct
34 Correct 62 ms 860 KB Output is correct
35 Correct 8 ms 856 KB Output is correct
36 Correct 8 ms 896 KB Output is correct
37 Correct 9 ms 956 KB Output is correct
38 Correct 66 ms 1044 KB Output is correct
39 Correct 65 ms 1108 KB Output is correct
40 Correct 65 ms 1116 KB Output is correct
41 Correct 9 ms 1112 KB Output is correct
42 Correct 8 ms 1140 KB Output is correct
43 Correct 10 ms 1116 KB Output is correct
44 Correct 69 ms 860 KB Output is correct
45 Correct 65 ms 860 KB Output is correct
46 Correct 82 ms 856 KB Output is correct
47 Correct 8 ms 856 KB Output is correct
48 Correct 8 ms 856 KB Output is correct
49 Correct 8 ms 1080 KB Output is correct
50 Correct 59 ms 856 KB Output is correct
51 Correct 53 ms 1040 KB Output is correct
52 Correct 57 ms 856 KB Output is correct
53 Correct 59 ms 1036 KB Output is correct
54 Correct 56 ms 1004 KB Output is correct
55 Correct 56 ms 996 KB Output is correct
56 Correct 11 ms 604 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 6 ms 860 KB Output is correct
59 Correct 0 ms 344 KB Output is correct
60 Correct 2 ms 348 KB Output is correct
61 Correct 11 ms 544 KB Output is correct
62 Runtime error 526 ms 47924 KB Execution killed with signal 11
63 Halted 0 ms 0 KB -