Submission #988411

# Submission time Handle Problem Language Result Execution time Memory
988411 2024-05-24T16:14:06 Z Zero_OP Tourism (JOI23_tourism) C++14
100 / 100
1069 ms 26448 KB
//no template practicing

#include<bits/stdc++.h>

using namespace std;

const int MAX = 1e5 + 5;
const int inf = 1e9;

int n, m, q, timer_hld, pos[MAX], c[MAX], sz[MAX], depth[MAX], par[MAX], head[MAX], answer[MAX];
vector<int> adj[MAX];
vector<pair<int, int>> queries[MAX];
int val[MAX];
map<int, int> cut;

struct Fenwick{
    vector<int> bit;
    Fenwick(int n) : bit(n + 3, 0) {}

    void update(int id, int val){
        id += 2;
        for(; id > 0; id -= id & (-id)){
            bit[id] += val;
        }
    }

    int query(int id){
        id += 2;
        int sum = 0;
        assert(id > 0);
        for(; id < (int)bit.size(); id += id & (-id)){
            sum += bit[id];
        }
        return sum;
    }
};

Fenwick T(0);

void modify(int l, int r, int x){
    for(int p : {l, r}){
        if(cut.find(p) == cut.end()){
            auto iter = cut.lower_bound(p);
            int k = prev(iter) -> second;
            cut[p] = k;
        }
    }

    while(true){
        auto iter = cut.lower_bound(l);
        int u = iter -> first;
        if(u == r) break;
        int v = next(iter) -> first;
        int w = iter -> second;
        T.update(w, -(v - u));
        cut.erase(iter);
    }

    cut[l] = x;
    T.update(x, r - l);

    for(int p : {l, r}){
        auto iter = cut.lower_bound(p);
        if(iter != cut.begin() && (iter -> second) == (prev(iter) -> second)){
            cut.erase(iter);
        }
    }
}

void update_path(int u, int v, int w){
    while(head[u] != head[v]){
        if(depth[head[u]] < depth[head[v]]) swap(u, v);
        modify(pos[head[u]], pos[u] + 1, w);
        u = par[head[u]];
    }

    if(pos[u] > pos[v]) swap(u, v);
    modify(pos[u], pos[v] + 1, w);
}

int calculate(int L){
    return T.query(L);
}

void dfs_sz(int u){
    sz[u] = 1;
    for(int &v : adj[u]){
        adj[v].erase(find(adj[v].begin(), adj[v].end(), u));
        depth[v] = depth[u] + 1;
        par[v] = u;
        dfs_sz(v);
        sz[u] += sz[v];
        if(sz[adj[u][0]] < sz[v]){
            swap(v, adj[u][0]);
        }
    }
}

void dfs_hld(int u, int top){
    head[u] = top;
    pos[u] = timer_hld++;

    for(int v : adj[u]){
        if(v == adj[u][0]) dfs_hld(v, top);
        else dfs_hld(v, v);
    }
}

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

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

    for(int i = 0; i < m; ++i){
        cin >> c[i]; --c[i];
    }

    for(int i = 0; i < q; ++i){
        int l, r;
        cin >> l >> r;
        --l, --r;
        if(l == r){
            answer[i] = 1;
            continue;
        }
        queries[r].push_back({l, i});
    }

    dfs_sz(0);
    dfs_hld(0, 0);

    T = Fenwick(m);
    cut[0] = -1;
    cut[n] = -1;

    update_path(c[0], c[0], 0);
    for(int i = 1; i < m; ++i){
        update_path(c[i - 1], c[i], i - 1); //change value of every edges from c[i - 1] to c[i] to i - 1
        update_path(c[i], c[i], i); //change the value of vertex c[i] to i

        for(auto [j, id] : queries[i]){
            answer[id] = calculate(j); //number of edges have value >= j
        }
    }

    for(int i = 0; i < q; ++i){
        cout << answer[i] << '\n';
    }

    return 0;
}

Compilation message

tourism.cpp: In function 'int main()':
tourism.cpp:149:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  149 |         for(auto [j, id] : queries[i]){
      |                  ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6492 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 4 ms 6488 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 3 ms 6492 KB Output is correct
8 Correct 2 ms 6488 KB Output is correct
9 Correct 3 ms 6492 KB Output is correct
10 Correct 3 ms 6492 KB Output is correct
11 Correct 3 ms 6492 KB Output is correct
12 Correct 3 ms 6492 KB Output is correct
13 Correct 2 ms 6492 KB Output is correct
14 Correct 3 ms 6504 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6492 KB Output is correct
17 Correct 3 ms 6492 KB Output is correct
18 Correct 2 ms 6492 KB Output is correct
19 Correct 2 ms 6492 KB Output is correct
20 Correct 2 ms 6492 KB Output is correct
21 Correct 3 ms 6580 KB Output is correct
22 Correct 2 ms 6492 KB Output is correct
23 Correct 2 ms 6492 KB Output is correct
24 Correct 2 ms 6492 KB Output is correct
25 Correct 3 ms 6624 KB Output is correct
26 Correct 3 ms 6492 KB Output is correct
27 Correct 2 ms 6492 KB Output is correct
28 Correct 2 ms 6492 KB Output is correct
29 Correct 3 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6492 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 4 ms 6488 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 3 ms 6492 KB Output is correct
8 Correct 2 ms 6488 KB Output is correct
9 Correct 3 ms 6492 KB Output is correct
10 Correct 3 ms 6492 KB Output is correct
11 Correct 3 ms 6492 KB Output is correct
12 Correct 3 ms 6492 KB Output is correct
13 Correct 2 ms 6492 KB Output is correct
14 Correct 3 ms 6504 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6492 KB Output is correct
17 Correct 3 ms 6492 KB Output is correct
18 Correct 2 ms 6492 KB Output is correct
19 Correct 2 ms 6492 KB Output is correct
20 Correct 2 ms 6492 KB Output is correct
21 Correct 3 ms 6580 KB Output is correct
22 Correct 2 ms 6492 KB Output is correct
23 Correct 2 ms 6492 KB Output is correct
24 Correct 2 ms 6492 KB Output is correct
25 Correct 3 ms 6624 KB Output is correct
26 Correct 3 ms 6492 KB Output is correct
27 Correct 2 ms 6492 KB Output is correct
28 Correct 2 ms 6492 KB Output is correct
29 Correct 3 ms 6492 KB Output is correct
30 Correct 7 ms 6748 KB Output is correct
31 Correct 7 ms 6784 KB Output is correct
32 Correct 9 ms 6748 KB Output is correct
33 Correct 9 ms 6748 KB Output is correct
34 Correct 9 ms 6748 KB Output is correct
35 Correct 9 ms 6748 KB Output is correct
36 Correct 9 ms 6748 KB Output is correct
37 Correct 9 ms 6748 KB Output is correct
38 Correct 4 ms 6748 KB Output is correct
39 Correct 4 ms 6748 KB Output is correct
40 Correct 4 ms 6748 KB Output is correct
41 Correct 4 ms 6744 KB Output is correct
42 Correct 4 ms 6748 KB Output is correct
43 Correct 4 ms 6696 KB Output is correct
44 Correct 6 ms 6748 KB Output is correct
45 Correct 6 ms 6748 KB Output is correct
46 Correct 6 ms 6748 KB Output is correct
47 Correct 6 ms 6748 KB Output is correct
48 Correct 6 ms 6748 KB Output is correct
49 Correct 6 ms 6748 KB Output is correct
50 Correct 6 ms 6744 KB Output is correct
51 Correct 6 ms 6748 KB Output is correct
52 Correct 6 ms 6748 KB Output is correct
53 Correct 6 ms 6744 KB Output is correct
54 Correct 6 ms 6748 KB Output is correct
55 Correct 5 ms 6840 KB Output is correct
56 Correct 3 ms 6492 KB Output is correct
57 Correct 3 ms 6748 KB Output is correct
58 Correct 9 ms 6748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 109 ms 18656 KB Output is correct
5 Correct 83 ms 21588 KB Output is correct
6 Correct 105 ms 23380 KB Output is correct
7 Correct 130 ms 26388 KB Output is correct
8 Correct 134 ms 26316 KB Output is correct
9 Correct 133 ms 26352 KB Output is correct
10 Correct 132 ms 26448 KB Output is correct
11 Correct 137 ms 26388 KB Output is correct
12 Correct 143 ms 25776 KB Output is correct
13 Correct 141 ms 25788 KB Output is correct
14 Correct 142 ms 25684 KB Output is correct
15 Correct 44 ms 21844 KB Output is correct
16 Correct 118 ms 23892 KB Output is correct
17 Correct 46 ms 10732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 338 ms 12396 KB Output is correct
3 Correct 529 ms 13092 KB Output is correct
4 Correct 418 ms 13472 KB Output is correct
5 Correct 665 ms 16916 KB Output is correct
6 Correct 652 ms 16596 KB Output is correct
7 Correct 655 ms 16596 KB Output is correct
8 Correct 653 ms 16784 KB Output is correct
9 Correct 665 ms 16840 KB Output is correct
10 Correct 662 ms 16920 KB Output is correct
11 Correct 652 ms 16736 KB Output is correct
12 Correct 658 ms 16632 KB Output is correct
13 Correct 666 ms 17068 KB Output is correct
14 Correct 660 ms 17792 KB Output is correct
15 Correct 684 ms 18000 KB Output is correct
16 Correct 650 ms 17232 KB Output is correct
17 Correct 654 ms 17236 KB Output is correct
18 Correct 640 ms 16928 KB Output is correct
19 Correct 405 ms 13912 KB Output is correct
20 Correct 388 ms 13584 KB Output is correct
21 Correct 415 ms 13616 KB Output is correct
22 Correct 415 ms 13748 KB Output is correct
23 Correct 389 ms 13732 KB Output is correct
24 Correct 406 ms 13908 KB Output is correct
25 Correct 400 ms 13652 KB Output is correct
26 Correct 420 ms 14220 KB Output is correct
27 Correct 407 ms 13616 KB Output is correct
28 Correct 408 ms 13740 KB Output is correct
29 Correct 405 ms 13656 KB Output is correct
30 Correct 426 ms 13888 KB Output is correct
31 Correct 406 ms 14356 KB Output is correct
32 Correct 423 ms 14660 KB Output is correct
33 Correct 432 ms 15444 KB Output is correct
34 Correct 413 ms 14960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 2 ms 6488 KB Output is correct
3 Correct 3 ms 6488 KB Output is correct
4 Correct 858 ms 16232 KB Output is correct
5 Correct 898 ms 16524 KB Output is correct
6 Correct 1016 ms 18988 KB Output is correct
7 Correct 1061 ms 20644 KB Output is correct
8 Correct 1069 ms 20536 KB Output is correct
9 Correct 1061 ms 20456 KB Output is correct
10 Correct 1065 ms 20564 KB Output is correct
11 Correct 1048 ms 20536 KB Output is correct
12 Correct 1060 ms 20528 KB Output is correct
13 Correct 1048 ms 20560 KB Output is correct
14 Correct 46 ms 10836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6492 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 4 ms 6488 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 3 ms 6492 KB Output is correct
8 Correct 2 ms 6488 KB Output is correct
9 Correct 3 ms 6492 KB Output is correct
10 Correct 3 ms 6492 KB Output is correct
11 Correct 3 ms 6492 KB Output is correct
12 Correct 3 ms 6492 KB Output is correct
13 Correct 2 ms 6492 KB Output is correct
14 Correct 3 ms 6504 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6492 KB Output is correct
17 Correct 3 ms 6492 KB Output is correct
18 Correct 2 ms 6492 KB Output is correct
19 Correct 2 ms 6492 KB Output is correct
20 Correct 2 ms 6492 KB Output is correct
21 Correct 3 ms 6580 KB Output is correct
22 Correct 2 ms 6492 KB Output is correct
23 Correct 2 ms 6492 KB Output is correct
24 Correct 2 ms 6492 KB Output is correct
25 Correct 3 ms 6624 KB Output is correct
26 Correct 3 ms 6492 KB Output is correct
27 Correct 2 ms 6492 KB Output is correct
28 Correct 2 ms 6492 KB Output is correct
29 Correct 3 ms 6492 KB Output is correct
30 Correct 7 ms 6748 KB Output is correct
31 Correct 7 ms 6784 KB Output is correct
32 Correct 9 ms 6748 KB Output is correct
33 Correct 9 ms 6748 KB Output is correct
34 Correct 9 ms 6748 KB Output is correct
35 Correct 9 ms 6748 KB Output is correct
36 Correct 9 ms 6748 KB Output is correct
37 Correct 9 ms 6748 KB Output is correct
38 Correct 4 ms 6748 KB Output is correct
39 Correct 4 ms 6748 KB Output is correct
40 Correct 4 ms 6748 KB Output is correct
41 Correct 4 ms 6744 KB Output is correct
42 Correct 4 ms 6748 KB Output is correct
43 Correct 4 ms 6696 KB Output is correct
44 Correct 6 ms 6748 KB Output is correct
45 Correct 6 ms 6748 KB Output is correct
46 Correct 6 ms 6748 KB Output is correct
47 Correct 6 ms 6748 KB Output is correct
48 Correct 6 ms 6748 KB Output is correct
49 Correct 6 ms 6748 KB Output is correct
50 Correct 6 ms 6744 KB Output is correct
51 Correct 6 ms 6748 KB Output is correct
52 Correct 6 ms 6748 KB Output is correct
53 Correct 6 ms 6744 KB Output is correct
54 Correct 6 ms 6748 KB Output is correct
55 Correct 5 ms 6840 KB Output is correct
56 Correct 3 ms 6492 KB Output is correct
57 Correct 3 ms 6748 KB Output is correct
58 Correct 9 ms 6748 KB Output is correct
59 Correct 2 ms 6492 KB Output is correct
60 Correct 2 ms 6492 KB Output is correct
61 Correct 2 ms 6492 KB Output is correct
62 Correct 109 ms 18656 KB Output is correct
63 Correct 83 ms 21588 KB Output is correct
64 Correct 105 ms 23380 KB Output is correct
65 Correct 130 ms 26388 KB Output is correct
66 Correct 134 ms 26316 KB Output is correct
67 Correct 133 ms 26352 KB Output is correct
68 Correct 132 ms 26448 KB Output is correct
69 Correct 137 ms 26388 KB Output is correct
70 Correct 143 ms 25776 KB Output is correct
71 Correct 141 ms 25788 KB Output is correct
72 Correct 142 ms 25684 KB Output is correct
73 Correct 44 ms 21844 KB Output is correct
74 Correct 118 ms 23892 KB Output is correct
75 Correct 46 ms 10732 KB Output is correct
76 Correct 2 ms 6488 KB Output is correct
77 Correct 338 ms 12396 KB Output is correct
78 Correct 529 ms 13092 KB Output is correct
79 Correct 418 ms 13472 KB Output is correct
80 Correct 665 ms 16916 KB Output is correct
81 Correct 652 ms 16596 KB Output is correct
82 Correct 655 ms 16596 KB Output is correct
83 Correct 653 ms 16784 KB Output is correct
84 Correct 665 ms 16840 KB Output is correct
85 Correct 662 ms 16920 KB Output is correct
86 Correct 652 ms 16736 KB Output is correct
87 Correct 658 ms 16632 KB Output is correct
88 Correct 666 ms 17068 KB Output is correct
89 Correct 660 ms 17792 KB Output is correct
90 Correct 684 ms 18000 KB Output is correct
91 Correct 650 ms 17232 KB Output is correct
92 Correct 654 ms 17236 KB Output is correct
93 Correct 640 ms 16928 KB Output is correct
94 Correct 405 ms 13912 KB Output is correct
95 Correct 388 ms 13584 KB Output is correct
96 Correct 415 ms 13616 KB Output is correct
97 Correct 415 ms 13748 KB Output is correct
98 Correct 389 ms 13732 KB Output is correct
99 Correct 406 ms 13908 KB Output is correct
100 Correct 400 ms 13652 KB Output is correct
101 Correct 420 ms 14220 KB Output is correct
102 Correct 407 ms 13616 KB Output is correct
103 Correct 408 ms 13740 KB Output is correct
104 Correct 405 ms 13656 KB Output is correct
105 Correct 426 ms 13888 KB Output is correct
106 Correct 406 ms 14356 KB Output is correct
107 Correct 423 ms 14660 KB Output is correct
108 Correct 432 ms 15444 KB Output is correct
109 Correct 413 ms 14960 KB Output is correct
110 Correct 2 ms 6488 KB Output is correct
111 Correct 2 ms 6488 KB Output is correct
112 Correct 3 ms 6488 KB Output is correct
113 Correct 858 ms 16232 KB Output is correct
114 Correct 898 ms 16524 KB Output is correct
115 Correct 1016 ms 18988 KB Output is correct
116 Correct 1061 ms 20644 KB Output is correct
117 Correct 1069 ms 20536 KB Output is correct
118 Correct 1061 ms 20456 KB Output is correct
119 Correct 1065 ms 20564 KB Output is correct
120 Correct 1048 ms 20536 KB Output is correct
121 Correct 1060 ms 20528 KB Output is correct
122 Correct 1048 ms 20560 KB Output is correct
123 Correct 46 ms 10836 KB Output is correct
124 Correct 604 ms 19672 KB Output is correct
125 Correct 441 ms 17676 KB Output is correct
126 Correct 716 ms 20256 KB Output is correct
127 Correct 707 ms 20176 KB Output is correct
128 Correct 743 ms 20172 KB Output is correct
129 Correct 691 ms 20468 KB Output is correct
130 Correct 688 ms 20308 KB Output is correct
131 Correct 138 ms 24916 KB Output is correct
132 Correct 135 ms 26192 KB Output is correct
133 Correct 160 ms 21708 KB Output is correct
134 Correct 454 ms 17492 KB Output is correct
135 Correct 447 ms 17496 KB Output is correct
136 Correct 436 ms 17488 KB Output is correct
137 Correct 273 ms 21488 KB Output is correct
138 Correct 272 ms 21444 KB Output is correct
139 Correct 269 ms 21444 KB Output is correct
140 Correct 267 ms 21360 KB Output is correct
141 Correct 283 ms 21708 KB Output is correct
142 Correct 270 ms 21336 KB Output is correct
143 Correct 41 ms 12484 KB Output is correct
144 Correct 678 ms 17780 KB Output is correct