#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <map>
#include <set>
typedef long long llong;
const int MAXN = 100000 + 10;
const int MAXQ = 300000 + 10;
const int BUCKET_SIZE = 317;
const llong INF = 1e18;
int n, q, t;
struct Flight
{
int from;
int to;
int idx;
};
int cntFlights;
int inTime[MAXN];
llong answer[MAXQ];
llong answer2[MAXN];
std::vector <Flight> v[MAXN];
std::vector <int> minimumTo[MAXN];
std::vector <std::pair <int,int>> g[MAXN];
std::vector <std::pair <int,int>> queries[MAXN];
std::vector <std::pair <int,int>> byLevel[MAXN];
llong toRemove[MAXN];
int flightFrom[MAXN];
int flightTo[MAXN];
int parent[MAXN];
int parEdge[MAXN];
int parEdge2[MAXN];
int dep[MAXN];
llong dist[MAXN];
void buildTree(int node)
{
// std::cout << "build tree: " << node << ' ' << dep[node] << ' ' << dist[node] << '\n';
for (const auto &[u, val] : g[node])
{
dep[u] = dep[node] + 1;
dist[u] = dist[node] + val;
buildTree(u);
}
}
void solveDFS(int node)
{
// std::cout << "here: " << node << ' ' << dep
toRemove[dep[node]] = dist[node] - parEdge2[node];
for (const auto &[level, idx] : byLevel[dep[node]])
{
answer[idx] = std::min(answer[idx], dist[node] - toRemove[level]);
}
for (const auto &[u, val] : g[node])
{
solveDFS(u);
}
}
int specialDep;
llong solveDFS2(int node)
{
if (dep[node] == specialDep)
{
answer2[dep[node]] = std::min(answer2[dep[node]], (llong)parEdge2[node]);
return dist[node];
}
llong minAdd = INF;
for (const auto &[u, val] : g[node])
{
minAdd = std::min(minAdd, solveDFS2(u));
}
answer2[dep[node]] = std::min(answer2[dep[node]], minAdd - dist[node] + parEdge2[node]);
return minAdd;
}
void solve()
{
for (int i = 1 ; i < n ; ++i)
{
std::sort(v[i].begin(), v[i].end(), [&](auto x, auto y)
{
return x.from < y.from || (x.from == y.from && x.to < y.to);
});
for (int j = 0 ; j < v[i].size() ; ++j)
{
v[i][j].idx = ++cntFlights;
flightFrom[v[i][j].idx] = v[i][j].from;
flightTo[v[i][j].idx] = v[i][j].to;
}
}
cntFlights++;
for (const Flight &f : v[n - 1])
{
parent[f.idx] = cntFlights;
parEdge2[f.idx] = f.to - f.from;
g[cntFlights].push_back({f.idx, f.to - f.from});
}
for (int i = n - 2 ; i >= 1 ; --i)
{
minimumTo[i + 1].resize(v[i + 1].size());
minimumTo[i + 1].back() = v[i + 1][v[i + 1].size() - 1].idx;
for (int j = (int)v[i + 1].size() - 2 ; j >= 0 ; --j)
{
minimumTo[i + 1][j] = minimumTo[i + 1][j + 1];
if (v[i + 1][j].to < flightTo[minimumTo[i + 1][j]])
{
minimumTo[i + 1][j] = v[i + 1][j].idx;
}
}
for (int j = 0 ; j < v[i].size() ; ++j)
{
int l = -1, r = v[i + 1].size(), mid;
while (l < r - 1)
{
mid = (l + r) / 2;
if (v[i + 1][mid].from < v[i][j].to) l = mid;
else r = mid;
}
if (r != v[i + 1].size())
{
parent[v[i][j].idx] = minimumTo[i + 1][r];
parEdge[v[i][j].idx] = flightFrom[minimumTo[i + 1][r]] - v[i][j].from;
parEdge2[v[i][j].idx] = v[i][j].to - v[i][j].from;
g[minimumTo[i + 1][r]].push_back({v[i][j].idx, flightFrom[minimumTo[i + 1][r]] - v[i][j].from});
} else
{
parent[v[i][j].idx] = minimumTo[i + 1][0];
parEdge[v[i][j].idx] = t - v[i][j].from + flightFrom[minimumTo[i + 1][0]];
parEdge2[v[i][j].idx] = v[i][j].to - v[i][j].from;
g[minimumTo[i + 1][0]].push_back({v[i][j].idx, t - v[i][j].from + flightFrom[minimumTo[i + 1][0]]});
}
}
}
for (int i = 1 ; i <= q ; ++i)
{
answer[i] = INF;
}
buildTree(cntFlights);
for (int l = 1 ; l <= n ; ++l)
{
if (v[l].size() < BUCKET_SIZE)
{
for (const auto &[r, idx] : queries[l])
{
// std::cout << "add query: " << n - l << ' ' << n - (r - 1) << ' ' << idx << '\n';
byLevel[n - l].push_back({n - (r - 1), idx});
/*answer[idx] = INF;
for (const Flight &f : v[l])
{
llong curr = 0;
int flightIdx = f.idx;
int times = r - l - 1;
for (int i = 0 ; i < times ; ++i)
{
curr += parEdge[flightIdx];
flightIdx = parent[flightIdx];
}
curr += parEdge2[flightIdx];
answer[idx] = std::min(answer[idx], curr);
}*/
}
} else
{
std::fill(answer2, answer2 + n, INF);
specialDep = n - l;
solveDFS2(cntFlights);
for (const auto &[r, idx] : queries[l])
{
answer[idx] = answer2[n - r + 1];
}
}
}
solveDFS(cntFlights);
}
void print()
{
for (int i = 1 ; i <= q ; ++i)
{
std::cout << answer[i] << '\n';
}
}
void input()
{
std::cin >> n >> t;
for (int i = 1 ; i < n ; ++i)
{
int sz;
std::cin >> sz;
v[i].resize(sz);
for (int j = 0 ; j < sz ; ++j)
{
std::cin >> v[i][j].from >> v[i][j].to;
}
}
std::cin >> q;
for (int i = 1 ; i <= q ; ++i)
{
int l, r;
std::cin >> l >> r;
queries[l].push_back({r, i});
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
print();
return 0;
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:95:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Flight>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for (int j = 0 ; j < v[i].size() ; ++j)
| ~~^~~~~~~~~~~~~
Main.cpp:124:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Flight>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | for (int j = 0 ; j < v[i].size() ; ++j)
| ~~^~~~~~~~~~~~~
Main.cpp:134:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Flight>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | if (r != v[i + 1].size())
| ~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12120 KB |
Output is correct |
2 |
Correct |
53 ms |
23496 KB |
Output is correct |
3 |
Correct |
53 ms |
23892 KB |
Output is correct |
4 |
Correct |
66 ms |
26880 KB |
Output is correct |
5 |
Correct |
53 ms |
22608 KB |
Output is correct |
6 |
Correct |
66 ms |
26160 KB |
Output is correct |
7 |
Correct |
66 ms |
26344 KB |
Output is correct |
8 |
Correct |
51 ms |
23636 KB |
Output is correct |
9 |
Correct |
74 ms |
27960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12120 KB |
Output is correct |
2 |
Correct |
53 ms |
23496 KB |
Output is correct |
3 |
Correct |
53 ms |
23892 KB |
Output is correct |
4 |
Correct |
66 ms |
26880 KB |
Output is correct |
5 |
Correct |
53 ms |
22608 KB |
Output is correct |
6 |
Correct |
66 ms |
26160 KB |
Output is correct |
7 |
Correct |
66 ms |
26344 KB |
Output is correct |
8 |
Correct |
51 ms |
23636 KB |
Output is correct |
9 |
Correct |
74 ms |
27960 KB |
Output is correct |
10 |
Correct |
5 ms |
12120 KB |
Output is correct |
11 |
Correct |
64 ms |
26708 KB |
Output is correct |
12 |
Correct |
65 ms |
26964 KB |
Output is correct |
13 |
Correct |
70 ms |
25864 KB |
Output is correct |
14 |
Correct |
62 ms |
24912 KB |
Output is correct |
15 |
Correct |
62 ms |
26960 KB |
Output is correct |
16 |
Correct |
55 ms |
24232 KB |
Output is correct |
17 |
Correct |
70 ms |
27176 KB |
Output is correct |
18 |
Correct |
64 ms |
26512 KB |
Output is correct |
19 |
Correct |
66 ms |
26708 KB |
Output is correct |
20 |
Correct |
62 ms |
26116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12120 KB |
Output is correct |
2 |
Correct |
53 ms |
23496 KB |
Output is correct |
3 |
Correct |
53 ms |
23892 KB |
Output is correct |
4 |
Correct |
66 ms |
26880 KB |
Output is correct |
5 |
Correct |
53 ms |
22608 KB |
Output is correct |
6 |
Correct |
66 ms |
26160 KB |
Output is correct |
7 |
Correct |
66 ms |
26344 KB |
Output is correct |
8 |
Correct |
51 ms |
23636 KB |
Output is correct |
9 |
Correct |
74 ms |
27960 KB |
Output is correct |
10 |
Correct |
98 ms |
40372 KB |
Output is correct |
11 |
Correct |
133 ms |
49864 KB |
Output is correct |
12 |
Correct |
134 ms |
47444 KB |
Output is correct |
13 |
Correct |
114 ms |
45724 KB |
Output is correct |
14 |
Correct |
133 ms |
50620 KB |
Output is correct |
15 |
Correct |
135 ms |
48468 KB |
Output is correct |
16 |
Correct |
102 ms |
39284 KB |
Output is correct |
17 |
Correct |
132 ms |
51024 KB |
Output is correct |
18 |
Correct |
67 ms |
36548 KB |
Output is correct |
19 |
Correct |
65 ms |
35812 KB |
Output is correct |
20 |
Correct |
69 ms |
36692 KB |
Output is correct |
21 |
Correct |
70 ms |
36948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12120 KB |
Output is correct |
2 |
Correct |
53 ms |
23496 KB |
Output is correct |
3 |
Correct |
53 ms |
23892 KB |
Output is correct |
4 |
Correct |
66 ms |
26880 KB |
Output is correct |
5 |
Correct |
53 ms |
22608 KB |
Output is correct |
6 |
Correct |
66 ms |
26160 KB |
Output is correct |
7 |
Correct |
66 ms |
26344 KB |
Output is correct |
8 |
Correct |
51 ms |
23636 KB |
Output is correct |
9 |
Correct |
74 ms |
27960 KB |
Output is correct |
10 |
Correct |
5 ms |
12120 KB |
Output is correct |
11 |
Correct |
64 ms |
26708 KB |
Output is correct |
12 |
Correct |
65 ms |
26964 KB |
Output is correct |
13 |
Correct |
70 ms |
25864 KB |
Output is correct |
14 |
Correct |
62 ms |
24912 KB |
Output is correct |
15 |
Correct |
62 ms |
26960 KB |
Output is correct |
16 |
Correct |
55 ms |
24232 KB |
Output is correct |
17 |
Correct |
70 ms |
27176 KB |
Output is correct |
18 |
Correct |
64 ms |
26512 KB |
Output is correct |
19 |
Correct |
66 ms |
26708 KB |
Output is correct |
20 |
Correct |
62 ms |
26116 KB |
Output is correct |
21 |
Correct |
98 ms |
40372 KB |
Output is correct |
22 |
Correct |
133 ms |
49864 KB |
Output is correct |
23 |
Correct |
134 ms |
47444 KB |
Output is correct |
24 |
Correct |
114 ms |
45724 KB |
Output is correct |
25 |
Correct |
133 ms |
50620 KB |
Output is correct |
26 |
Correct |
135 ms |
48468 KB |
Output is correct |
27 |
Correct |
102 ms |
39284 KB |
Output is correct |
28 |
Correct |
132 ms |
51024 KB |
Output is correct |
29 |
Correct |
67 ms |
36548 KB |
Output is correct |
30 |
Correct |
65 ms |
35812 KB |
Output is correct |
31 |
Correct |
69 ms |
36692 KB |
Output is correct |
32 |
Correct |
70 ms |
36948 KB |
Output is correct |
33 |
Correct |
114 ms |
40272 KB |
Output is correct |
34 |
Correct |
119 ms |
39272 KB |
Output is correct |
35 |
Correct |
112 ms |
34840 KB |
Output is correct |
36 |
Correct |
123 ms |
34152 KB |
Output is correct |
37 |
Correct |
109 ms |
34956 KB |
Output is correct |
38 |
Correct |
102 ms |
37096 KB |
Output is correct |
39 |
Correct |
119 ms |
42068 KB |
Output is correct |
40 |
Correct |
98 ms |
35148 KB |
Output is correct |
41 |
Correct |
106 ms |
37204 KB |
Output is correct |
42 |
Correct |
133 ms |
43236 KB |
Output is correct |
43 |
Correct |
103 ms |
35852 KB |
Output is correct |
44 |
Correct |
116 ms |
37716 KB |
Output is correct |
45 |
Correct |
63 ms |
29524 KB |
Output is correct |
46 |
Correct |
57 ms |
25428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12124 KB |
Output is correct |
3 |
Correct |
101 ms |
23408 KB |
Output is correct |
4 |
Correct |
110 ms |
23632 KB |
Output is correct |
5 |
Correct |
109 ms |
22864 KB |
Output is correct |
6 |
Correct |
97 ms |
23380 KB |
Output is correct |
7 |
Correct |
96 ms |
24712 KB |
Output is correct |
8 |
Correct |
87 ms |
23896 KB |
Output is correct |
9 |
Correct |
73 ms |
21960 KB |
Output is correct |
10 |
Correct |
46 ms |
20308 KB |
Output is correct |
11 |
Correct |
71 ms |
36380 KB |
Output is correct |
12 |
Correct |
70 ms |
30968 KB |
Output is correct |
13 |
Correct |
58 ms |
26092 KB |
Output is correct |
14 |
Correct |
53 ms |
26220 KB |
Output is correct |
15 |
Correct |
56 ms |
26612 KB |
Output is correct |
16 |
Correct |
51 ms |
27356 KB |
Output is correct |
17 |
Correct |
64 ms |
26704 KB |
Output is correct |
18 |
Correct |
52 ms |
22224 KB |
Output is correct |
19 |
Correct |
134 ms |
22096 KB |
Output is correct |
20 |
Correct |
75 ms |
31640 KB |
Output is correct |
21 |
Correct |
76 ms |
30160 KB |
Output is correct |
22 |
Correct |
82 ms |
26708 KB |
Output is correct |
23 |
Correct |
81 ms |
27988 KB |
Output is correct |
24 |
Correct |
63 ms |
25540 KB |
Output is correct |
25 |
Correct |
53 ms |
27064 KB |
Output is correct |
26 |
Correct |
63 ms |
28328 KB |
Output is correct |
27 |
Correct |
66 ms |
35920 KB |
Output is correct |
28 |
Correct |
70 ms |
36784 KB |
Output is correct |
29 |
Correct |
69 ms |
36732 KB |
Output is correct |
30 |
Correct |
54 ms |
21880 KB |
Output is correct |
31 |
Correct |
50 ms |
21964 KB |
Output is correct |
32 |
Correct |
60 ms |
24248 KB |
Output is correct |
33 |
Correct |
54 ms |
22932 KB |
Output is correct |
34 |
Correct |
93 ms |
22096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12120 KB |
Output is correct |
2 |
Correct |
53 ms |
23496 KB |
Output is correct |
3 |
Correct |
53 ms |
23892 KB |
Output is correct |
4 |
Correct |
66 ms |
26880 KB |
Output is correct |
5 |
Correct |
53 ms |
22608 KB |
Output is correct |
6 |
Correct |
66 ms |
26160 KB |
Output is correct |
7 |
Correct |
66 ms |
26344 KB |
Output is correct |
8 |
Correct |
51 ms |
23636 KB |
Output is correct |
9 |
Correct |
74 ms |
27960 KB |
Output is correct |
10 |
Correct |
5 ms |
12120 KB |
Output is correct |
11 |
Correct |
64 ms |
26708 KB |
Output is correct |
12 |
Correct |
65 ms |
26964 KB |
Output is correct |
13 |
Correct |
70 ms |
25864 KB |
Output is correct |
14 |
Correct |
62 ms |
24912 KB |
Output is correct |
15 |
Correct |
62 ms |
26960 KB |
Output is correct |
16 |
Correct |
55 ms |
24232 KB |
Output is correct |
17 |
Correct |
70 ms |
27176 KB |
Output is correct |
18 |
Correct |
64 ms |
26512 KB |
Output is correct |
19 |
Correct |
66 ms |
26708 KB |
Output is correct |
20 |
Correct |
62 ms |
26116 KB |
Output is correct |
21 |
Correct |
98 ms |
40372 KB |
Output is correct |
22 |
Correct |
133 ms |
49864 KB |
Output is correct |
23 |
Correct |
134 ms |
47444 KB |
Output is correct |
24 |
Correct |
114 ms |
45724 KB |
Output is correct |
25 |
Correct |
133 ms |
50620 KB |
Output is correct |
26 |
Correct |
135 ms |
48468 KB |
Output is correct |
27 |
Correct |
102 ms |
39284 KB |
Output is correct |
28 |
Correct |
132 ms |
51024 KB |
Output is correct |
29 |
Correct |
67 ms |
36548 KB |
Output is correct |
30 |
Correct |
65 ms |
35812 KB |
Output is correct |
31 |
Correct |
69 ms |
36692 KB |
Output is correct |
32 |
Correct |
70 ms |
36948 KB |
Output is correct |
33 |
Correct |
114 ms |
40272 KB |
Output is correct |
34 |
Correct |
119 ms |
39272 KB |
Output is correct |
35 |
Correct |
112 ms |
34840 KB |
Output is correct |
36 |
Correct |
123 ms |
34152 KB |
Output is correct |
37 |
Correct |
109 ms |
34956 KB |
Output is correct |
38 |
Correct |
102 ms |
37096 KB |
Output is correct |
39 |
Correct |
119 ms |
42068 KB |
Output is correct |
40 |
Correct |
98 ms |
35148 KB |
Output is correct |
41 |
Correct |
106 ms |
37204 KB |
Output is correct |
42 |
Correct |
133 ms |
43236 KB |
Output is correct |
43 |
Correct |
103 ms |
35852 KB |
Output is correct |
44 |
Correct |
116 ms |
37716 KB |
Output is correct |
45 |
Correct |
63 ms |
29524 KB |
Output is correct |
46 |
Correct |
57 ms |
25428 KB |
Output is correct |
47 |
Correct |
5 ms |
12124 KB |
Output is correct |
48 |
Correct |
5 ms |
12124 KB |
Output is correct |
49 |
Correct |
101 ms |
23408 KB |
Output is correct |
50 |
Correct |
110 ms |
23632 KB |
Output is correct |
51 |
Correct |
109 ms |
22864 KB |
Output is correct |
52 |
Correct |
97 ms |
23380 KB |
Output is correct |
53 |
Correct |
96 ms |
24712 KB |
Output is correct |
54 |
Correct |
87 ms |
23896 KB |
Output is correct |
55 |
Correct |
73 ms |
21960 KB |
Output is correct |
56 |
Correct |
46 ms |
20308 KB |
Output is correct |
57 |
Correct |
71 ms |
36380 KB |
Output is correct |
58 |
Correct |
70 ms |
30968 KB |
Output is correct |
59 |
Correct |
58 ms |
26092 KB |
Output is correct |
60 |
Correct |
53 ms |
26220 KB |
Output is correct |
61 |
Correct |
56 ms |
26612 KB |
Output is correct |
62 |
Correct |
51 ms |
27356 KB |
Output is correct |
63 |
Correct |
64 ms |
26704 KB |
Output is correct |
64 |
Correct |
52 ms |
22224 KB |
Output is correct |
65 |
Correct |
134 ms |
22096 KB |
Output is correct |
66 |
Correct |
75 ms |
31640 KB |
Output is correct |
67 |
Correct |
76 ms |
30160 KB |
Output is correct |
68 |
Correct |
82 ms |
26708 KB |
Output is correct |
69 |
Correct |
81 ms |
27988 KB |
Output is correct |
70 |
Correct |
63 ms |
25540 KB |
Output is correct |
71 |
Correct |
53 ms |
27064 KB |
Output is correct |
72 |
Correct |
63 ms |
28328 KB |
Output is correct |
73 |
Correct |
66 ms |
35920 KB |
Output is correct |
74 |
Correct |
70 ms |
36784 KB |
Output is correct |
75 |
Correct |
69 ms |
36732 KB |
Output is correct |
76 |
Correct |
54 ms |
21880 KB |
Output is correct |
77 |
Correct |
50 ms |
21964 KB |
Output is correct |
78 |
Correct |
60 ms |
24248 KB |
Output is correct |
79 |
Correct |
54 ms |
22932 KB |
Output is correct |
80 |
Correct |
93 ms |
22096 KB |
Output is correct |
81 |
Correct |
245 ms |
31832 KB |
Output is correct |
82 |
Correct |
212 ms |
33080 KB |
Output is correct |
83 |
Correct |
214 ms |
35668 KB |
Output is correct |
84 |
Correct |
216 ms |
34568 KB |
Output is correct |
85 |
Correct |
210 ms |
31652 KB |
Output is correct |
86 |
Correct |
201 ms |
30456 KB |
Output is correct |
87 |
Correct |
136 ms |
32256 KB |
Output is correct |
88 |
Correct |
182 ms |
36624 KB |
Output is correct |
89 |
Correct |
170 ms |
35868 KB |
Output is correct |
90 |
Correct |
161 ms |
31316 KB |
Output is correct |
91 |
Correct |
84 ms |
26404 KB |
Output is correct |
92 |
Correct |
115 ms |
34080 KB |
Output is correct |
93 |
Correct |
107 ms |
35408 KB |
Output is correct |
94 |
Correct |
106 ms |
39184 KB |
Output is correct |
95 |
Correct |
116 ms |
37068 KB |
Output is correct |
96 |
Correct |
115 ms |
34964 KB |
Output is correct |
97 |
Correct |
220 ms |
34132 KB |
Output is correct |
98 |
Correct |
190 ms |
38976 KB |
Output is correct |
99 |
Correct |
157 ms |
39504 KB |
Output is correct |
100 |
Correct |
205 ms |
35404 KB |
Output is correct |
101 |
Correct |
189 ms |
38044 KB |
Output is correct |
102 |
Correct |
105 ms |
38268 KB |
Output is correct |
103 |
Correct |
92 ms |
32948 KB |
Output is correct |
104 |
Correct |
116 ms |
34116 KB |
Output is correct |
105 |
Correct |
96 ms |
31200 KB |
Output is correct |
106 |
Correct |
109 ms |
27288 KB |
Output is correct |
107 |
Correct |
126 ms |
31088 KB |
Output is correct |
108 |
Correct |
109 ms |
29452 KB |
Output is correct |
109 |
Correct |
193 ms |
29824 KB |
Output is correct |