Submission #1101623

# Submission time Handle Problem Language Result Execution time Memory
1101623 2024-10-16T11:56:28 Z Pacybwoah Tourism (JOI23_tourism) C++17
34 / 100
5000 ms 37384 KB
#include<iostream>
#include<vector>
#include<set>
#include<utility>
#include<cassert>
#include<algorithm>
using namespace std;
int n, m, q;
vector<vector<int>> graph, anc;
vector<int> vec, in, out, ord, head, sz, bit, dep;
int cnt = 0;
void dfs(int node, int parent){
    ord[++cnt] = node;
    in[node] = cnt;
    sz[node] = 1;
    anc[0][node] = parent;
    head[node] = node;
    dep[node] = dep[parent] + 1;
    int maxi = 0, pos = -1;
    for(auto &x: graph[node]){
        if(x == parent) continue;
        dfs(x, node);
        sz[node] += sz[x];
        if(maxi < sz[x]){
            maxi = sz[x];
            pos = x;
        }
    }
    //if(pos > 0) head[pos] = node;
    out[node] = cnt;
}
void dfs_head(int node, int parent){
    if(head[node] != node) head[node] = head[parent];
    for(auto &x: graph[node]){
        if(x != parent) dfs_head(x, node);
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m >> q;
    graph.resize(n + 1);
    vec.resize(m + 1);
    in.resize(n + 1);
    out.resize(n + 1);
    ord.resize(n + 1);
    head.resize(n + 1);
    sz.resize(n + 1);
    dep.resize(n + 1);
    for(int i = 1; i < n; i++){
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    anc.resize(18, vector<int>(n + 1));
    dfs(1, 1);
    dfs_head(1, 1);
    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 a, int b){
        if(dep[a] < dep[b]) swap(a, b);
        for(int i = 0; i < 18; i++){
            if((dep[a] - dep[b]) & (1 << i)) a = anc[i][a];
        }
        for(int i = 17; i >= 0; i--){
            if(anc[i][a] != anc[i][b]){
                a = anc[i][a];
                b = anc[i][b];
            }
        }
        if(a != b) return anc[0][a];
        else return a;
    };
    bit.resize(m + 1);
    for(int i = 1; i <= m; i++) cin >> vec[i];
    auto modify = [&](int pos, int val){
        while(pos <= m){
            bit[pos] += val;
            pos += (pos & -pos);
        }
    };
    auto query = [&](int pos){
        int ans = 0;
        while(pos > 0){
            ans += bit[pos];
            pos -= (pos & -pos);
        }
        return ans;
    };
    vector<vector<pair<int, int>>> sweep(m + 1);
    vector<int> ans(q);
    for(int i = 0; i < q; i++){
        int l, r;
        cin >> l >> r;
        sweep[r].emplace_back(l, i);
    }
    vector<vector<int>> st(18, vector<int>(m));
    for(int i = 1; i < m; i++) st[0][i] = lca(vec[i], vec[i + 1]);
    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 - 1, j + (1 << (i - 1)))]);
    }
    vector<int> bases(m);
    for(int i = 2; i < m; i++){
        bases[i] = bases[i - 1];
        if((1 << (bases[i] + 1)) == i) bases[i]++;
    }
    auto qlca = [&](int l, int r){
        r--;
        if(l > r) return vec[l];
        int len = r - l + 1;
        return lca(st[bases[len]][l], st[bases[len]][r - (1 << bases[len]) + 1]);
    };
    set<pair<pair<int, int>, int>> s;
    auto assign = [&](int l, int r, int col){
        //cout << l << " " << r << " " << col << "\n";
        while(!s.empty()){
            auto iter = s.lower_bound(make_pair(make_pair(l, 0), 0));
            if(iter == s.end()) break;
            auto [rng, scol] = (*iter);
            if(rng.first > r) break;
            s.erase(iter);
            modify(scol, -(rng.second - rng.first + 1));
            if(rng.second > r){
                modify(scol, rng.second - r);
                s.insert(make_pair(make_pair(r + 1, rng.second), scol));
            }
        }
        auto iter2 = s.lower_bound(make_pair(make_pair(l, 0), 0));
        if(iter2 != s.begin()){
            iter2 = prev(iter2);
            if((*iter2).first.second >= l){
                auto [rng2, col2] = (*iter2);
                s.erase(iter2);
                modify(col2, -(rng2.second - l + 1));
                s.insert(make_pair(make_pair(rng2.first, l - 1), col2));
                if(r < rng2.second){
                    modify(col2, rng2.second - r);
                    s.insert(make_pair(make_pair(r + 1, rng2.second), col2));
                }
            }
        }
        s.insert(make_pair(make_pair(l, r), col));
        modify(col, r - l + 1);
    };
    for(int i = 1; i <= m; i++){
        int cur = vec[i];
        while(1){
            assign(in[head[cur]], in[cur], i);
            if(head[cur] == 1) break;
            cur = anc[0][head[cur]];
        }
        for(auto &[l, id]: sweep[i]){
            ans[id] += query(i) - query(l - 1);
            ans[id] -= dep[qlca(l, i)];
        }
    }
    for(int i = 0; i < q; i++){
        cout << ans[i] + 1 << "\n";
    }
}

Compilation message

tourism.cpp: In function 'void dfs(int, int)':
tourism.cpp:19:19: warning: variable 'pos' set but not used [-Wunused-but-set-variable]
   19 |     int maxi = 0, pos = -1;
      |                   ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 396 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 5 ms 340 KB Output is correct
16 Correct 6 ms 340 KB Output is correct
17 Correct 6 ms 340 KB Output is correct
18 Correct 4 ms 340 KB Output is correct
19 Correct 6 ms 340 KB Output is correct
20 Correct 4 ms 504 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 508 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 508 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 396 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 5 ms 340 KB Output is correct
16 Correct 6 ms 340 KB Output is correct
17 Correct 6 ms 340 KB Output is correct
18 Correct 4 ms 340 KB Output is correct
19 Correct 6 ms 340 KB Output is correct
20 Correct 4 ms 504 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 508 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 508 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 6 ms 852 KB Output is correct
31 Correct 7 ms 984 KB Output is correct
32 Correct 9 ms 1108 KB Output is correct
33 Correct 9 ms 1108 KB Output is correct
34 Correct 9 ms 1136 KB Output is correct
35 Correct 9 ms 1108 KB Output is correct
36 Correct 9 ms 1108 KB Output is correct
37 Correct 9 ms 1108 KB Output is correct
38 Correct 243 ms 1260 KB Output is correct
39 Correct 258 ms 1108 KB Output is correct
40 Correct 286 ms 1108 KB Output is correct
41 Correct 334 ms 1272 KB Output is correct
42 Correct 380 ms 1108 KB Output is correct
43 Correct 230 ms 1236 KB Output is correct
44 Correct 141 ms 1108 KB Output is correct
45 Correct 78 ms 1108 KB Output is correct
46 Correct 178 ms 1220 KB Output is correct
47 Correct 116 ms 1108 KB Output is correct
48 Correct 88 ms 1108 KB Output is correct
49 Correct 113 ms 1108 KB Output is correct
50 Correct 5 ms 1108 KB Output is correct
51 Correct 5 ms 1108 KB Output is correct
52 Correct 5 ms 1064 KB Output is correct
53 Correct 5 ms 1120 KB Output is correct
54 Correct 5 ms 1108 KB Output is correct
55 Correct 5 ms 1108 KB Output is correct
56 Correct 3 ms 596 KB Output is correct
57 Correct 2 ms 596 KB Output is correct
58 Correct 8 ms 1156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 3 ms 760 KB Output is correct
4 Execution timed out 5099 ms 26944 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 398 ms 17104 KB Output is correct
3 Correct 600 ms 23360 KB Output is correct
4 Correct 483 ms 22288 KB Output is correct
5 Correct 691 ms 32616 KB Output is correct
6 Correct 809 ms 32580 KB Output is correct
7 Correct 745 ms 32708 KB Output is correct
8 Correct 763 ms 32652 KB Output is correct
9 Correct 724 ms 32708 KB Output is correct
10 Correct 777 ms 32452 KB Output is correct
11 Correct 713 ms 32500 KB Output is correct
12 Correct 842 ms 32708 KB Output is correct
13 Correct 721 ms 33148 KB Output is correct
14 Correct 736 ms 34184 KB Output is correct
15 Correct 848 ms 37384 KB Output is correct
16 Correct 800 ms 33148 KB Output is correct
17 Correct 753 ms 33180 KB Output is correct
18 Correct 785 ms 32956 KB Output is correct
19 Execution timed out 5069 ms 33812 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 3 ms 592 KB Output is correct
4 Correct 668 ms 24076 KB Output is correct
5 Correct 662 ms 24824 KB Output is correct
6 Correct 790 ms 30780 KB Output is correct
7 Correct 848 ms 33936 KB Output is correct
8 Correct 794 ms 33728 KB Output is correct
9 Correct 781 ms 33756 KB Output is correct
10 Correct 784 ms 33824 KB Output is correct
11 Correct 809 ms 33884 KB Output is correct
12 Correct 823 ms 33788 KB Output is correct
13 Correct 796 ms 33700 KB Output is correct
14 Correct 133 ms 13504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 396 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 5 ms 340 KB Output is correct
16 Correct 6 ms 340 KB Output is correct
17 Correct 6 ms 340 KB Output is correct
18 Correct 4 ms 340 KB Output is correct
19 Correct 6 ms 340 KB Output is correct
20 Correct 4 ms 504 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 508 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 508 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 6 ms 852 KB Output is correct
31 Correct 7 ms 984 KB Output is correct
32 Correct 9 ms 1108 KB Output is correct
33 Correct 9 ms 1108 KB Output is correct
34 Correct 9 ms 1136 KB Output is correct
35 Correct 9 ms 1108 KB Output is correct
36 Correct 9 ms 1108 KB Output is correct
37 Correct 9 ms 1108 KB Output is correct
38 Correct 243 ms 1260 KB Output is correct
39 Correct 258 ms 1108 KB Output is correct
40 Correct 286 ms 1108 KB Output is correct
41 Correct 334 ms 1272 KB Output is correct
42 Correct 380 ms 1108 KB Output is correct
43 Correct 230 ms 1236 KB Output is correct
44 Correct 141 ms 1108 KB Output is correct
45 Correct 78 ms 1108 KB Output is correct
46 Correct 178 ms 1220 KB Output is correct
47 Correct 116 ms 1108 KB Output is correct
48 Correct 88 ms 1108 KB Output is correct
49 Correct 113 ms 1108 KB Output is correct
50 Correct 5 ms 1108 KB Output is correct
51 Correct 5 ms 1108 KB Output is correct
52 Correct 5 ms 1064 KB Output is correct
53 Correct 5 ms 1120 KB Output is correct
54 Correct 5 ms 1108 KB Output is correct
55 Correct 5 ms 1108 KB Output is correct
56 Correct 3 ms 596 KB Output is correct
57 Correct 2 ms 596 KB Output is correct
58 Correct 8 ms 1156 KB Output is correct
59 Correct 1 ms 336 KB Output is correct
60 Correct 1 ms 336 KB Output is correct
61 Correct 3 ms 760 KB Output is correct
62 Execution timed out 5099 ms 26944 KB Time limit exceeded
63 Halted 0 ms 0 KB -