# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
950835 |
2024-03-20T19:05:39 Z |
vjudge1 |
Tourism (JOI23_tourism) |
C++17 |
|
5000 ms |
32868 KB |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using ld = long double; // or double, if TL is tight
using str = string;
using ii = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
struct AINT {
int n;
vector<ii> V; /// (minim, cnt)
vi Lz;
ii join(ii a, ii b) {
if(a.first == b.first)
return {a.first, a.second + b.second};
return min(a, b);
}
AINT(int N) : n(N), V(4 * N), Lz(4 * N) {}
void init(int u, int s, int d) {
if(s == d) {
V[u] = {0, 1};
return;
}
init(u << 1, s, (s + d) >> 1);
init(u << 1 | 1, ((s + d) >> 1) + 1, d);
V[u] = join(V[u << 1], V[u << 1 | 1]);
}
void init() { init(1, 0, n - 1); }
void propag(int u, int s, int d) {
if(!Lz[u]) return;
V[u].first += Lz[u];
if(s != d) {
Lz[u << 1] += Lz[u];
Lz[u << 1 | 1] += Lz[u];
}
Lz[u] = 0;
}
void update(int l, int r, int v, int u, int s, int d) {
propag(u, s, d);
if(r < s || d < l) return;
if(l <= s && d <= r) {
Lz[u] += v;
propag(u, s, d);
return;
}
update(l, r, v, u << 1, s, (s + d) >> 1);
update(l, r, v, u << 1 | 1, ((s + d) >> 1) + 1, d);
V[u] = join(V[u << 1], V[u << 1 | 1]);
}
void update(int l, int r, int v) { update(l, r, v, 1, 0, n - 1); }
int query() { /// nr de 0 uri
if(V[1].first == 0) return V[1].second;
return 0;
}
};
struct tree {
int n, tmr = 0;
vi par, niv, sz, nxt, in;
vector<vi> L, G, Anc;
AINT A;
tree(int N) : n(N), par(N), niv(N), sz(N, 0),
nxt(N, 0), in(N, 0), L(N), G(N), A(N) {} /// nxt[u] e capul la lant
void add_edge(int u, int v) {
L[u].push_back(v);
L[v].push_back(u);
}
void init() {
function<void(int, int, int)> dfs0 = [&](int u, int p, int li) {
par[u] = p;
niv[u] = li;
sz[u] = 1;
for (auto it : L[u])
if (it != p) {
dfs0(it, u, li + 1);
sz[u] += it;
G[u].push_back(it);
}
};
dfs0(0, -1, 0);
A.init();
function<void(int, int)> dfs1 = [&](int u, int p) {
sort(all(G[u]), [&](auto a, auto b) { return sz[a] > sz[b]; });
in[u] = tmr++;
if (G[u].empty()) return;
nxt[G[u][0]] = nxt[u];
dfs1(G[u][0], u);
for (int i = 1; i < G[u].size(); ++i) {
nxt[G[u][i]] = G[u][i];
dfs1(G[u][i], u);
}
};
dfs1(0, -1);
Anc.push_back(par);
for (int k = 1; (1 << k) <= n; ++k) {
Anc.push_back(vi(n, -1));
for (int i = 0; i < n; ++i) {
Anc[k][i] = Anc[k - 1][i] == -1 ? -1 : Anc[k - 1][Anc[k - 1][i]];
}
}
}
int lift(int u, int nr) {
for (int i = 0; i < (int)Anc.size(); ++i)
if (nr & (1 << i))
u = Anc[i][u];
return u;
}
int lca(int u, int v) {
if(niv[u] > niv[v]) swap(u, v);
v = lift(v, niv[v] - niv[u]);
if(u == v) return u;
for(int i = (int)Anc.size() - 1; i >= 0; --i)
if(Anc[i][u] != Anc[i][v]) {
u = Anc[i][u];
v = Anc[i][v];
}
return par[u];
}
void update(int u, int v) {
while(u != -1) {
A.update(min(in[u], in[nxt[u]]), max(in[u], in[nxt[u]]), v);
u = par[nxt[u]];
}
}
int query() { /// nr pornite
return n - A.query();
}
};
struct RMQ_LCA {
int m;
tree &T;
vi C;
vector<vi> Re;
RMQ_LCA(int N, vi C0, tree &T0) : m(C0.size()), C(C0), T(T0) {
Re.push_back(C);
for(int k = 1; (1 << k) <= m; ++k) {
Re.push_back(vi(m, 0));
for(int i = 0; i < m; ++i) {
if(i + (1 << (k - 1)) < m)
Re[k][i] = T.lca(Re[k - 1][i], Re[k - 1][i + (1 << (k - 1))]);
else Re[k][i] = Re[k - 1][i];
}
}
}
int query(int st, int dr) {
int l = 31 - __builtin_clz(dr - st + 1);
return T.lca(Re[l][st], Re[l][dr - (1 << l) + 1]);
}
};
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int n, m, q;
cin >> n >> m >> q;
tree T(n);
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
--u; --v;
T.add_edge(u, v);
}
T.init();
vi C(m);
for(int i = 0; i < m; ++i) {
cin >> C[i];
--C[i];
}
RMQ_LCA Stramos(n, C, T);
//TODO MO
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
--l; --r;
for (int j = l; j <= r; ++j)
T.update(C[j], 1);
int nr0 = T.query();
int nr_plus = T.niv[Stramos.query(l, r)];
cout << nr0 - nr_plus << "\n";
for (int j = l; j <= r; ++j)
T.update(C[j], -1);
}
return 0;
}
Compilation message
tourism.cpp: In lambda function:
tourism.cpp:110:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for (int i = 1; i < G[u].size(); ++i) {
| ~~^~~~~~~~~~~~~
tourism.cpp: In constructor 'RMQ_LCA::RMQ_LCA(int, vi, tree&)':
tourism.cpp:160:8: warning: 'RMQ_LCA::C' will be initialized after [-Wreorder]
160 | vi C;
| ^
tourism.cpp:159:11: warning: 'tree& RMQ_LCA::T' [-Wreorder]
159 | tree &T;
| ^
tourism.cpp:163:5: warning: when initialized here [-Wreorder]
163 | RMQ_LCA(int N, vi C0, tree &T0) : m(C0.size()), C(C0), T(T0) {
| ^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
16 ms |
504 KB |
Output is correct |
5 |
Correct |
8 ms |
344 KB |
Output is correct |
6 |
Correct |
9 ms |
348 KB |
Output is correct |
7 |
Correct |
9 ms |
344 KB |
Output is correct |
8 |
Correct |
11 ms |
344 KB |
Output is correct |
9 |
Correct |
19 ms |
348 KB |
Output is correct |
10 |
Correct |
20 ms |
348 KB |
Output is correct |
11 |
Correct |
15 ms |
536 KB |
Output is correct |
12 |
Correct |
59 ms |
504 KB |
Output is correct |
13 |
Correct |
59 ms |
348 KB |
Output is correct |
14 |
Correct |
64 ms |
508 KB |
Output is correct |
15 |
Correct |
15 ms |
344 KB |
Output is correct |
16 |
Correct |
10 ms |
348 KB |
Output is correct |
17 |
Correct |
9 ms |
344 KB |
Output is correct |
18 |
Correct |
23 ms |
348 KB |
Output is correct |
19 |
Correct |
18 ms |
348 KB |
Output is correct |
20 |
Correct |
14 ms |
348 KB |
Output is correct |
21 |
Correct |
11 ms |
532 KB |
Output is correct |
22 |
Correct |
11 ms |
532 KB |
Output is correct |
23 |
Correct |
11 ms |
536 KB |
Output is correct |
24 |
Correct |
11 ms |
348 KB |
Output is correct |
25 |
Correct |
14 ms |
348 KB |
Output is correct |
26 |
Correct |
11 ms |
528 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
16 ms |
504 KB |
Output is correct |
5 |
Correct |
8 ms |
344 KB |
Output is correct |
6 |
Correct |
9 ms |
348 KB |
Output is correct |
7 |
Correct |
9 ms |
344 KB |
Output is correct |
8 |
Correct |
11 ms |
344 KB |
Output is correct |
9 |
Correct |
19 ms |
348 KB |
Output is correct |
10 |
Correct |
20 ms |
348 KB |
Output is correct |
11 |
Correct |
15 ms |
536 KB |
Output is correct |
12 |
Correct |
59 ms |
504 KB |
Output is correct |
13 |
Correct |
59 ms |
348 KB |
Output is correct |
14 |
Correct |
64 ms |
508 KB |
Output is correct |
15 |
Correct |
15 ms |
344 KB |
Output is correct |
16 |
Correct |
10 ms |
348 KB |
Output is correct |
17 |
Correct |
9 ms |
344 KB |
Output is correct |
18 |
Correct |
23 ms |
348 KB |
Output is correct |
19 |
Correct |
18 ms |
348 KB |
Output is correct |
20 |
Correct |
14 ms |
348 KB |
Output is correct |
21 |
Correct |
11 ms |
532 KB |
Output is correct |
22 |
Correct |
11 ms |
532 KB |
Output is correct |
23 |
Correct |
11 ms |
536 KB |
Output is correct |
24 |
Correct |
11 ms |
348 KB |
Output is correct |
25 |
Correct |
14 ms |
348 KB |
Output is correct |
26 |
Correct |
11 ms |
528 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
622 ms |
828 KB |
Output is correct |
31 |
Correct |
1018 ms |
820 KB |
Output is correct |
32 |
Correct |
1356 ms |
1112 KB |
Output is correct |
33 |
Correct |
2080 ms |
968 KB |
Output is correct |
34 |
Correct |
1678 ms |
956 KB |
Output is correct |
35 |
Correct |
3733 ms |
1104 KB |
Output is correct |
36 |
Correct |
4796 ms |
1108 KB |
Output is correct |
37 |
Correct |
4510 ms |
1104 KB |
Output is correct |
38 |
Correct |
628 ms |
1116 KB |
Output is correct |
39 |
Correct |
580 ms |
1360 KB |
Output is correct |
40 |
Correct |
745 ms |
1204 KB |
Output is correct |
41 |
Correct |
1424 ms |
1360 KB |
Output is correct |
42 |
Correct |
1397 ms |
1232 KB |
Output is correct |
43 |
Correct |
2049 ms |
1156 KB |
Output is correct |
44 |
Correct |
1378 ms |
1112 KB |
Output is correct |
45 |
Correct |
1362 ms |
1076 KB |
Output is correct |
46 |
Correct |
1721 ms |
1112 KB |
Output is correct |
47 |
Correct |
3620 ms |
1084 KB |
Output is correct |
48 |
Correct |
3298 ms |
1072 KB |
Output is correct |
49 |
Correct |
4873 ms |
1348 KB |
Output is correct |
50 |
Correct |
610 ms |
1008 KB |
Output is correct |
51 |
Correct |
621 ms |
984 KB |
Output is correct |
52 |
Correct |
617 ms |
860 KB |
Output is correct |
53 |
Correct |
606 ms |
856 KB |
Output is correct |
54 |
Correct |
627 ms |
1108 KB |
Output is correct |
55 |
Correct |
596 ms |
984 KB |
Output is correct |
56 |
Correct |
11 ms |
348 KB |
Output is correct |
57 |
Correct |
3 ms |
856 KB |
Output is correct |
58 |
Correct |
4 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
11 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
5059 ms |
27092 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
200 ms |
16748 KB |
Output is correct |
3 |
Correct |
332 ms |
20820 KB |
Output is correct |
4 |
Correct |
309 ms |
21132 KB |
Output is correct |
5 |
Correct |
383 ms |
31076 KB |
Output is correct |
6 |
Correct |
449 ms |
31104 KB |
Output is correct |
7 |
Correct |
442 ms |
30980 KB |
Output is correct |
8 |
Correct |
398 ms |
31052 KB |
Output is correct |
9 |
Correct |
398 ms |
30972 KB |
Output is correct |
10 |
Correct |
397 ms |
31060 KB |
Output is correct |
11 |
Correct |
410 ms |
31032 KB |
Output is correct |
12 |
Correct |
561 ms |
30980 KB |
Output is correct |
13 |
Correct |
439 ms |
31372 KB |
Output is correct |
14 |
Correct |
444 ms |
31172 KB |
Output is correct |
15 |
Correct |
513 ms |
31040 KB |
Output is correct |
16 |
Correct |
539 ms |
31208 KB |
Output is correct |
17 |
Correct |
399 ms |
31032 KB |
Output is correct |
18 |
Correct |
376 ms |
31060 KB |
Output is correct |
19 |
Correct |
856 ms |
32592 KB |
Output is correct |
20 |
Correct |
815 ms |
32744 KB |
Output is correct |
21 |
Correct |
740 ms |
32596 KB |
Output is correct |
22 |
Correct |
838 ms |
32780 KB |
Output is correct |
23 |
Correct |
881 ms |
32596 KB |
Output is correct |
24 |
Correct |
959 ms |
32696 KB |
Output is correct |
25 |
Correct |
812 ms |
32740 KB |
Output is correct |
26 |
Correct |
938 ms |
32596 KB |
Output is correct |
27 |
Correct |
839 ms |
32596 KB |
Output is correct |
28 |
Correct |
841 ms |
32596 KB |
Output is correct |
29 |
Correct |
870 ms |
32592 KB |
Output is correct |
30 |
Correct |
958 ms |
32596 KB |
Output is correct |
31 |
Correct |
883 ms |
32568 KB |
Output is correct |
32 |
Correct |
676 ms |
32732 KB |
Output is correct |
33 |
Correct |
642 ms |
32676 KB |
Output is correct |
34 |
Correct |
1167 ms |
32868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
11 ms |
572 KB |
Output is correct |
4 |
Execution timed out |
5081 ms |
19916 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
16 ms |
504 KB |
Output is correct |
5 |
Correct |
8 ms |
344 KB |
Output is correct |
6 |
Correct |
9 ms |
348 KB |
Output is correct |
7 |
Correct |
9 ms |
344 KB |
Output is correct |
8 |
Correct |
11 ms |
344 KB |
Output is correct |
9 |
Correct |
19 ms |
348 KB |
Output is correct |
10 |
Correct |
20 ms |
348 KB |
Output is correct |
11 |
Correct |
15 ms |
536 KB |
Output is correct |
12 |
Correct |
59 ms |
504 KB |
Output is correct |
13 |
Correct |
59 ms |
348 KB |
Output is correct |
14 |
Correct |
64 ms |
508 KB |
Output is correct |
15 |
Correct |
15 ms |
344 KB |
Output is correct |
16 |
Correct |
10 ms |
348 KB |
Output is correct |
17 |
Correct |
9 ms |
344 KB |
Output is correct |
18 |
Correct |
23 ms |
348 KB |
Output is correct |
19 |
Correct |
18 ms |
348 KB |
Output is correct |
20 |
Correct |
14 ms |
348 KB |
Output is correct |
21 |
Correct |
11 ms |
532 KB |
Output is correct |
22 |
Correct |
11 ms |
532 KB |
Output is correct |
23 |
Correct |
11 ms |
536 KB |
Output is correct |
24 |
Correct |
11 ms |
348 KB |
Output is correct |
25 |
Correct |
14 ms |
348 KB |
Output is correct |
26 |
Correct |
11 ms |
528 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
622 ms |
828 KB |
Output is correct |
31 |
Correct |
1018 ms |
820 KB |
Output is correct |
32 |
Correct |
1356 ms |
1112 KB |
Output is correct |
33 |
Correct |
2080 ms |
968 KB |
Output is correct |
34 |
Correct |
1678 ms |
956 KB |
Output is correct |
35 |
Correct |
3733 ms |
1104 KB |
Output is correct |
36 |
Correct |
4796 ms |
1108 KB |
Output is correct |
37 |
Correct |
4510 ms |
1104 KB |
Output is correct |
38 |
Correct |
628 ms |
1116 KB |
Output is correct |
39 |
Correct |
580 ms |
1360 KB |
Output is correct |
40 |
Correct |
745 ms |
1204 KB |
Output is correct |
41 |
Correct |
1424 ms |
1360 KB |
Output is correct |
42 |
Correct |
1397 ms |
1232 KB |
Output is correct |
43 |
Correct |
2049 ms |
1156 KB |
Output is correct |
44 |
Correct |
1378 ms |
1112 KB |
Output is correct |
45 |
Correct |
1362 ms |
1076 KB |
Output is correct |
46 |
Correct |
1721 ms |
1112 KB |
Output is correct |
47 |
Correct |
3620 ms |
1084 KB |
Output is correct |
48 |
Correct |
3298 ms |
1072 KB |
Output is correct |
49 |
Correct |
4873 ms |
1348 KB |
Output is correct |
50 |
Correct |
610 ms |
1008 KB |
Output is correct |
51 |
Correct |
621 ms |
984 KB |
Output is correct |
52 |
Correct |
617 ms |
860 KB |
Output is correct |
53 |
Correct |
606 ms |
856 KB |
Output is correct |
54 |
Correct |
627 ms |
1108 KB |
Output is correct |
55 |
Correct |
596 ms |
984 KB |
Output is correct |
56 |
Correct |
11 ms |
348 KB |
Output is correct |
57 |
Correct |
3 ms |
856 KB |
Output is correct |
58 |
Correct |
4 ms |
860 KB |
Output is correct |
59 |
Correct |
1 ms |
344 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
11 ms |
348 KB |
Output is correct |
62 |
Execution timed out |
5059 ms |
27092 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |