// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<ll, int, int> lii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 100005;
const int MAXQ = 300005;
const int BLK = 183;
int n, t;
int m[MAXN];
vii ba[MAXN];
vector<vi> adj[MAXN];
vector<vector<lii>> big[MAXN];
int q;
vii mpl[MAXN];
ll ans[MAXQ];
ll dp[2][MAXN];
vi p[MAXN];
vll d[MAXN];
vi stk;
void dfs(int i, int j) {
cerr << i << ' ' << j << ": " << d[i][j] << '\n';
stk.pb(j);
if (m[i] <= BLK) {
for (auto [r, id] : mpl[i]) {
int anc = stk[n - r];
mnto(ans[id], d[i][j] - d[r - 1][anc] + ba[i][j].FI - ba[i][j].SE);
}
}
for (auto [w, r, id] : big[i][j]) {
int anc = stk[n - r];
mnto(ans[id], d[i][j] - d[r - 1][anc] + w);
}
for (int k : adj[i][j]) {
int w = ba[i][j].FI - ba[i - 1][k].FI;
if (ba[i - 1][k].FI > ba[i][j].SE) {
w += t;
}
d[i - 1][k] = d[i][j] + w;
dfs(i - 1, k);
}
stk.pop_back();
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> n >> t;
REP (i, 1, n) {
cin >> m[i];
ba[i].resize(m[i]);
adj[i].resize(m[i]);
p[i].resize(m[i]);
d[i].resize(m[i]);
big[i].resize(m[i]);
REP (j, 0, m[i]) {
cin >> ba[i][j].SE >> ba[i][j].FI;
}
sort(ALL(ba[i]));
int ptr = 0;
REP (j, 0, m[i]) {
while (ptr < SZ(ba[i - 1]) && ba[i - 1][ptr].FI <= ba[i][j].SE) {
p[i - 1][ptr] = j;
adj[i][j].pb(ptr++);
}
}
while (ptr < SZ(ba[i - 1])) {
p[i - 1][ptr] = 0;
adj[i][0].pb(ptr++);
}
}
cin >> q;
REP (i, 0, q) {
int l, r; cin >> l >> r;
mpl[l].pb({r, i});
ans[i] = LINF;
}
REP (i, 1, n) {
sort(ALL(mpl[i]));
}
REP (i, 1, n) {
if (m[i] <= BLK) {
continue;
}
REP (j, 0, m[i]) {
dp[i & 1][j] = ba[i][j].FI - ba[i][j].SE;
}
int u = i;
int ptr = 0;
while (m[u] > BLK) {
ll res = LINF;
REP (j, 0, m[u]) {
mnto(res, dp[u & 1][j]);
}
while (ptr < SZ(mpl[i]) && mpl[i][ptr].FI - 1 == u) {
ans[mpl[i][ptr++].SE] = res;
}
if (ptr == SZ(mpl[i])) {
break;
}
REP (j, 0, m[u + 1]) {
dp[u & 1 ^ 1][j] = LINF;
}
REP (j, 0, m[u]) {
int pj = p[u][j];
int w = ba[u + 1][pj].FI - ba[u][j].FI;
if (ba[u][j].FI > ba[u + 1][pj].SE) {
w += t;
}
mnto(dp[u & 1 ^ 1][pj], dp[u & 1][j] + w);
}
u++;
}
while (ptr < SZ(mpl[i])) {
auto [r, id] = mpl[i][ptr++];
REP (j, 0, m[u]) {
big[u][j].pb({dp[u & 1][j], r, id});
}
}
}
REP (j, 0, m[n - 1]) {
dfs(n - 1, j);
}
REP (i, 0, q) {
cout << ans[i] << '\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:138:22: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
138 | dp[u & 1 ^ 1][j] = LINF;
| ~~^~~
Main.cpp:146:27: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
146 | mnto(dp[u & 1 ^ 1][pj], dp[u & 1][j] + w);
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16220 KB |
Output is correct |
2 |
Correct |
55 ms |
23960 KB |
Output is correct |
3 |
Correct |
56 ms |
24972 KB |
Output is correct |
4 |
Correct |
70 ms |
26704 KB |
Output is correct |
5 |
Correct |
56 ms |
25088 KB |
Output is correct |
6 |
Correct |
70 ms |
26956 KB |
Output is correct |
7 |
Correct |
71 ms |
27328 KB |
Output is correct |
8 |
Correct |
55 ms |
25092 KB |
Output is correct |
9 |
Correct |
72 ms |
26960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16220 KB |
Output is correct |
2 |
Correct |
55 ms |
23960 KB |
Output is correct |
3 |
Correct |
56 ms |
24972 KB |
Output is correct |
4 |
Correct |
70 ms |
26704 KB |
Output is correct |
5 |
Correct |
56 ms |
25088 KB |
Output is correct |
6 |
Correct |
70 ms |
26956 KB |
Output is correct |
7 |
Correct |
71 ms |
27328 KB |
Output is correct |
8 |
Correct |
55 ms |
25092 KB |
Output is correct |
9 |
Correct |
72 ms |
26960 KB |
Output is correct |
10 |
Correct |
4 ms |
16220 KB |
Output is correct |
11 |
Correct |
75 ms |
26860 KB |
Output is correct |
12 |
Correct |
76 ms |
27192 KB |
Output is correct |
13 |
Correct |
83 ms |
26304 KB |
Output is correct |
14 |
Correct |
77 ms |
25940 KB |
Output is correct |
15 |
Correct |
72 ms |
27728 KB |
Output is correct |
16 |
Correct |
56 ms |
25788 KB |
Output is correct |
17 |
Correct |
71 ms |
27984 KB |
Output is correct |
18 |
Correct |
73 ms |
26708 KB |
Output is correct |
19 |
Correct |
69 ms |
27988 KB |
Output is correct |
20 |
Correct |
74 ms |
26404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16220 KB |
Output is correct |
2 |
Correct |
55 ms |
23960 KB |
Output is correct |
3 |
Correct |
56 ms |
24972 KB |
Output is correct |
4 |
Correct |
70 ms |
26704 KB |
Output is correct |
5 |
Correct |
56 ms |
25088 KB |
Output is correct |
6 |
Correct |
70 ms |
26956 KB |
Output is correct |
7 |
Correct |
71 ms |
27328 KB |
Output is correct |
8 |
Correct |
55 ms |
25092 KB |
Output is correct |
9 |
Correct |
72 ms |
26960 KB |
Output is correct |
10 |
Correct |
112 ms |
49608 KB |
Output is correct |
11 |
Correct |
177 ms |
58568 KB |
Output is correct |
12 |
Correct |
156 ms |
57044 KB |
Output is correct |
13 |
Correct |
143 ms |
56636 KB |
Output is correct |
14 |
Correct |
154 ms |
59852 KB |
Output is correct |
15 |
Correct |
153 ms |
59084 KB |
Output is correct |
16 |
Correct |
114 ms |
49612 KB |
Output is correct |
17 |
Correct |
159 ms |
58648 KB |
Output is correct |
18 |
Correct |
88 ms |
49268 KB |
Output is correct |
19 |
Correct |
91 ms |
47360 KB |
Output is correct |
20 |
Correct |
97 ms |
47560 KB |
Output is correct |
21 |
Correct |
96 ms |
47576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16220 KB |
Output is correct |
2 |
Correct |
55 ms |
23960 KB |
Output is correct |
3 |
Correct |
56 ms |
24972 KB |
Output is correct |
4 |
Correct |
70 ms |
26704 KB |
Output is correct |
5 |
Correct |
56 ms |
25088 KB |
Output is correct |
6 |
Correct |
70 ms |
26956 KB |
Output is correct |
7 |
Correct |
71 ms |
27328 KB |
Output is correct |
8 |
Correct |
55 ms |
25092 KB |
Output is correct |
9 |
Correct |
72 ms |
26960 KB |
Output is correct |
10 |
Correct |
4 ms |
16220 KB |
Output is correct |
11 |
Correct |
75 ms |
26860 KB |
Output is correct |
12 |
Correct |
76 ms |
27192 KB |
Output is correct |
13 |
Correct |
83 ms |
26304 KB |
Output is correct |
14 |
Correct |
77 ms |
25940 KB |
Output is correct |
15 |
Correct |
72 ms |
27728 KB |
Output is correct |
16 |
Correct |
56 ms |
25788 KB |
Output is correct |
17 |
Correct |
71 ms |
27984 KB |
Output is correct |
18 |
Correct |
73 ms |
26708 KB |
Output is correct |
19 |
Correct |
69 ms |
27988 KB |
Output is correct |
20 |
Correct |
74 ms |
26404 KB |
Output is correct |
21 |
Correct |
112 ms |
49608 KB |
Output is correct |
22 |
Correct |
177 ms |
58568 KB |
Output is correct |
23 |
Correct |
156 ms |
57044 KB |
Output is correct |
24 |
Correct |
143 ms |
56636 KB |
Output is correct |
25 |
Correct |
154 ms |
59852 KB |
Output is correct |
26 |
Correct |
153 ms |
59084 KB |
Output is correct |
27 |
Correct |
114 ms |
49612 KB |
Output is correct |
28 |
Correct |
159 ms |
58648 KB |
Output is correct |
29 |
Correct |
88 ms |
49268 KB |
Output is correct |
30 |
Correct |
91 ms |
47360 KB |
Output is correct |
31 |
Correct |
97 ms |
47560 KB |
Output is correct |
32 |
Correct |
96 ms |
47576 KB |
Output is correct |
33 |
Correct |
134 ms |
39504 KB |
Output is correct |
34 |
Correct |
150 ms |
39288 KB |
Output is correct |
35 |
Correct |
130 ms |
39248 KB |
Output is correct |
36 |
Correct |
129 ms |
38596 KB |
Output is correct |
37 |
Correct |
134 ms |
39500 KB |
Output is correct |
38 |
Correct |
112 ms |
44316 KB |
Output is correct |
39 |
Correct |
149 ms |
50560 KB |
Output is correct |
40 |
Correct |
109 ms |
37712 KB |
Output is correct |
41 |
Correct |
116 ms |
42036 KB |
Output is correct |
42 |
Correct |
159 ms |
49616 KB |
Output is correct |
43 |
Correct |
113 ms |
40292 KB |
Output is correct |
44 |
Correct |
128 ms |
42676 KB |
Output is correct |
45 |
Correct |
80 ms |
40920 KB |
Output is correct |
46 |
Correct |
66 ms |
33876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16216 KB |
Output is correct |
2 |
Correct |
4 ms |
16220 KB |
Output is correct |
3 |
Correct |
88 ms |
30036 KB |
Output is correct |
4 |
Correct |
86 ms |
30036 KB |
Output is correct |
5 |
Correct |
98 ms |
29432 KB |
Output is correct |
6 |
Correct |
87 ms |
30032 KB |
Output is correct |
7 |
Correct |
87 ms |
30292 KB |
Output is correct |
8 |
Correct |
87 ms |
28240 KB |
Output is correct |
9 |
Correct |
51 ms |
29672 KB |
Output is correct |
10 |
Correct |
41 ms |
25804 KB |
Output is correct |
11 |
Correct |
86 ms |
50128 KB |
Output is correct |
12 |
Correct |
82 ms |
42460 KB |
Output is correct |
13 |
Correct |
67 ms |
34712 KB |
Output is correct |
14 |
Correct |
62 ms |
32968 KB |
Output is correct |
15 |
Correct |
57 ms |
32848 KB |
Output is correct |
16 |
Correct |
57 ms |
32044 KB |
Output is correct |
17 |
Correct |
59 ms |
32592 KB |
Output is correct |
18 |
Correct |
53 ms |
28696 KB |
Output is correct |
19 |
Correct |
151 ms |
139604 KB |
Output is correct |
20 |
Correct |
97 ms |
43740 KB |
Output is correct |
21 |
Correct |
97 ms |
42032 KB |
Output is correct |
22 |
Correct |
245 ms |
224512 KB |
Output is correct |
23 |
Correct |
158 ms |
151636 KB |
Output is correct |
24 |
Correct |
66 ms |
36872 KB |
Output is correct |
25 |
Correct |
66 ms |
38424 KB |
Output is correct |
26 |
Correct |
69 ms |
39704 KB |
Output is correct |
27 |
Correct |
82 ms |
49872 KB |
Output is correct |
28 |
Correct |
87 ms |
50636 KB |
Output is correct |
29 |
Correct |
98 ms |
51044 KB |
Output is correct |
30 |
Correct |
51 ms |
28444 KB |
Output is correct |
31 |
Correct |
68 ms |
42412 KB |
Output is correct |
32 |
Correct |
58 ms |
30292 KB |
Output is correct |
33 |
Correct |
59 ms |
32080 KB |
Output is correct |
34 |
Correct |
126 ms |
127824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16220 KB |
Output is correct |
2 |
Correct |
55 ms |
23960 KB |
Output is correct |
3 |
Correct |
56 ms |
24972 KB |
Output is correct |
4 |
Correct |
70 ms |
26704 KB |
Output is correct |
5 |
Correct |
56 ms |
25088 KB |
Output is correct |
6 |
Correct |
70 ms |
26956 KB |
Output is correct |
7 |
Correct |
71 ms |
27328 KB |
Output is correct |
8 |
Correct |
55 ms |
25092 KB |
Output is correct |
9 |
Correct |
72 ms |
26960 KB |
Output is correct |
10 |
Correct |
4 ms |
16220 KB |
Output is correct |
11 |
Correct |
75 ms |
26860 KB |
Output is correct |
12 |
Correct |
76 ms |
27192 KB |
Output is correct |
13 |
Correct |
83 ms |
26304 KB |
Output is correct |
14 |
Correct |
77 ms |
25940 KB |
Output is correct |
15 |
Correct |
72 ms |
27728 KB |
Output is correct |
16 |
Correct |
56 ms |
25788 KB |
Output is correct |
17 |
Correct |
71 ms |
27984 KB |
Output is correct |
18 |
Correct |
73 ms |
26708 KB |
Output is correct |
19 |
Correct |
69 ms |
27988 KB |
Output is correct |
20 |
Correct |
74 ms |
26404 KB |
Output is correct |
21 |
Correct |
112 ms |
49608 KB |
Output is correct |
22 |
Correct |
177 ms |
58568 KB |
Output is correct |
23 |
Correct |
156 ms |
57044 KB |
Output is correct |
24 |
Correct |
143 ms |
56636 KB |
Output is correct |
25 |
Correct |
154 ms |
59852 KB |
Output is correct |
26 |
Correct |
153 ms |
59084 KB |
Output is correct |
27 |
Correct |
114 ms |
49612 KB |
Output is correct |
28 |
Correct |
159 ms |
58648 KB |
Output is correct |
29 |
Correct |
88 ms |
49268 KB |
Output is correct |
30 |
Correct |
91 ms |
47360 KB |
Output is correct |
31 |
Correct |
97 ms |
47560 KB |
Output is correct |
32 |
Correct |
96 ms |
47576 KB |
Output is correct |
33 |
Correct |
134 ms |
39504 KB |
Output is correct |
34 |
Correct |
150 ms |
39288 KB |
Output is correct |
35 |
Correct |
130 ms |
39248 KB |
Output is correct |
36 |
Correct |
129 ms |
38596 KB |
Output is correct |
37 |
Correct |
134 ms |
39500 KB |
Output is correct |
38 |
Correct |
112 ms |
44316 KB |
Output is correct |
39 |
Correct |
149 ms |
50560 KB |
Output is correct |
40 |
Correct |
109 ms |
37712 KB |
Output is correct |
41 |
Correct |
116 ms |
42036 KB |
Output is correct |
42 |
Correct |
159 ms |
49616 KB |
Output is correct |
43 |
Correct |
113 ms |
40292 KB |
Output is correct |
44 |
Correct |
128 ms |
42676 KB |
Output is correct |
45 |
Correct |
80 ms |
40920 KB |
Output is correct |
46 |
Correct |
66 ms |
33876 KB |
Output is correct |
47 |
Correct |
4 ms |
16216 KB |
Output is correct |
48 |
Correct |
4 ms |
16220 KB |
Output is correct |
49 |
Correct |
88 ms |
30036 KB |
Output is correct |
50 |
Correct |
86 ms |
30036 KB |
Output is correct |
51 |
Correct |
98 ms |
29432 KB |
Output is correct |
52 |
Correct |
87 ms |
30032 KB |
Output is correct |
53 |
Correct |
87 ms |
30292 KB |
Output is correct |
54 |
Correct |
87 ms |
28240 KB |
Output is correct |
55 |
Correct |
51 ms |
29672 KB |
Output is correct |
56 |
Correct |
41 ms |
25804 KB |
Output is correct |
57 |
Correct |
86 ms |
50128 KB |
Output is correct |
58 |
Correct |
82 ms |
42460 KB |
Output is correct |
59 |
Correct |
67 ms |
34712 KB |
Output is correct |
60 |
Correct |
62 ms |
32968 KB |
Output is correct |
61 |
Correct |
57 ms |
32848 KB |
Output is correct |
62 |
Correct |
57 ms |
32044 KB |
Output is correct |
63 |
Correct |
59 ms |
32592 KB |
Output is correct |
64 |
Correct |
53 ms |
28696 KB |
Output is correct |
65 |
Correct |
151 ms |
139604 KB |
Output is correct |
66 |
Correct |
97 ms |
43740 KB |
Output is correct |
67 |
Correct |
97 ms |
42032 KB |
Output is correct |
68 |
Correct |
245 ms |
224512 KB |
Output is correct |
69 |
Correct |
158 ms |
151636 KB |
Output is correct |
70 |
Correct |
66 ms |
36872 KB |
Output is correct |
71 |
Correct |
66 ms |
38424 KB |
Output is correct |
72 |
Correct |
69 ms |
39704 KB |
Output is correct |
73 |
Correct |
82 ms |
49872 KB |
Output is correct |
74 |
Correct |
87 ms |
50636 KB |
Output is correct |
75 |
Correct |
98 ms |
51044 KB |
Output is correct |
76 |
Correct |
51 ms |
28444 KB |
Output is correct |
77 |
Correct |
68 ms |
42412 KB |
Output is correct |
78 |
Correct |
58 ms |
30292 KB |
Output is correct |
79 |
Correct |
59 ms |
32080 KB |
Output is correct |
80 |
Correct |
126 ms |
127824 KB |
Output is correct |
81 |
Correct |
195 ms |
34996 KB |
Output is correct |
82 |
Correct |
188 ms |
36436 KB |
Output is correct |
83 |
Correct |
203 ms |
38736 KB |
Output is correct |
84 |
Correct |
180 ms |
37712 KB |
Output is correct |
85 |
Correct |
182 ms |
34768 KB |
Output is correct |
86 |
Correct |
192 ms |
33404 KB |
Output is correct |
87 |
Correct |
97 ms |
38996 KB |
Output is correct |
88 |
Correct |
237 ms |
39348 KB |
Output is correct |
89 |
Correct |
259 ms |
37896 KB |
Output is correct |
90 |
Correct |
205 ms |
34080 KB |
Output is correct |
91 |
Correct |
86 ms |
32580 KB |
Output is correct |
92 |
Correct |
134 ms |
37640 KB |
Output is correct |
93 |
Correct |
125 ms |
39616 KB |
Output is correct |
94 |
Correct |
126 ms |
42956 KB |
Output is correct |
95 |
Correct |
125 ms |
40860 KB |
Output is correct |
96 |
Correct |
133 ms |
38344 KB |
Output is correct |
97 |
Correct |
432 ms |
353076 KB |
Output is correct |
98 |
Correct |
263 ms |
48480 KB |
Output is correct |
99 |
Correct |
223 ms |
48112 KB |
Output is correct |
100 |
Correct |
1136 ms |
856572 KB |
Output is correct |
101 |
Correct |
724 ms |
609268 KB |
Output is correct |
102 |
Correct |
140 ms |
54424 KB |
Output is correct |
103 |
Correct |
132 ms |
46424 KB |
Output is correct |
104 |
Correct |
145 ms |
43836 KB |
Output is correct |
105 |
Correct |
113 ms |
33916 KB |
Output is correct |
106 |
Correct |
139 ms |
61196 KB |
Output is correct |
107 |
Correct |
161 ms |
34048 KB |
Output is correct |
108 |
Correct |
140 ms |
37168 KB |
Output is correct |
109 |
Correct |
357 ms |
320908 KB |
Output is correct |