#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const int DEPTH = 17;
struct segTree{
int minV[400002], minIdx[400002];
void init(int i, int l, int r, int *A){
if(l==r){
minV[i] = A[l], minIdx[i] = l;
return;
}
int m = (l+r)>>1;
init(i*2, l, m, A);
init(i*2+1, m+1, r, A);
if(minV[i*2] <= minV[i*2+1]) minV[i] = minV[i*2], minIdx[i] = minIdx[i*2];
else minV[i] = minV[i*2+1], minIdx[i] = minIdx[i*2+1];
}
pair<int, int> query(int i, int l, int r, int s, int e){
if(r<s || e<l) return make_pair(INT_MAX, -1);
if(s<=l && r<=e) return make_pair(minV[i], minIdx[i]);
int m = (l+r)>>1;
return min(query(i*2, l, m, s, e), query(i*2+1, m+1, r, s, e));
}
} tree;
struct dat{
ll x, y;
dat(){}
dat(ll x, ll y): x(x), y(y){}
bool operator<(const dat &r)const{
if(x != r.x) return x < r.x;
return y > r.y;
}
bool operator<(const ll &r)const{
return x < r;
}
};
struct Query{
int l, r, idx;
Query(){}
Query(int l, int r, int idx): l(l), r(r), idx(idx){}
};
int n, q, K; ll T;
dat arr[100002], arrRev[100002];
int S[100002], E[100002], sz[100002], from[100002];
int nxtR[100002], dayR[100002], nxtL[100002], dayL[100002];
vector<int> childL[100002], childR[100002]; /// 왼쪽이 루트인 트리 / 오른쪽이 루트인 트리
vector<Query> queries[100002];
vector<ll> ql[100002];
ll ans[300002];
int listX[100002], listD[100002];
int pcnt = 0;
void dfsL(int x){
listX[from[x]] = x;
ql[x].reserve((int)queries[from[x]].size());
for(Query &p: queries[from[x]]){
ql[x].push_back((listD[from[x]] - listD[p.l]) * T + arr[x].x - arr[listX[p.l]].x);
}
listD[from[x]+1] = listD[from[x]];
for(int y: childL[x]){
if(arrRev[x].x > arrRev[y].y) listD[from[x]+1]++;
dfsL(y);
if(arrRev[x].x > arrRev[y].y) listD[from[x]+1]--;
}
}
void dfsR(int x){
listX[from[x]] = x;
for(int i=0; i<(int)queries[from[x]].size(); i++){
Query p = queries[from[x]][i];
ll qlv = ql[x][i], qrv = (listD[from[x]] - listD[p.r-1]) * T + arr[listX[p.r-1]].y - arr[x].y;
ans[p.idx] = min(ans[p.idx], qlv + qrv + arr[x].y - arr[x].x);
}
listD[from[x]-1] = listD[from[x]];
for(int y: childR[x]){
if(arr[x].x < arr[y].y) listD[from[x]-1]++;
dfsR(y);
if(arr[x].x < arr[y].y) listD[from[x]-1]--;
}
}
map<pair<int, int>, int> queryMap;
int prvQuery[300002];
int main(){
scanf("%d %lld", &n, &T);
for(int i=1; i<n; i++){
int s;
scanf("%d", &s);
vector<dat> vec;
vec.resize(s);
for(int j=0; j<s; j++) scanf("%lld %lld", &vec[j].x, &vec[j].y);
sort(vec.begin(), vec.end());
vector<dat> vec2;
for(int j=0; j<s; j++){
while(!vec2.empty() && vec2.back().y >= vec[j].y) vec2.pop_back();
vec2.push_back(vec[j]);
}
sz[i] = (int)vec2.size();
S[i] = E[i-1] + 1, E[i] = S[i] + sz[i] - 1;
for(int j=0; j<sz[i]; j++) arr[S[i]+j] = vec2[j];
for(int j=S[i]; j<=E[i]; j++) arrRev[j] = dat(arr[j].y, arr[j].x), from[j] = i;
}
tree.init(1, 1, n-1, sz);
for(int d=1; d<=n-2; d++){
for(int i=S[d]; i<=E[d]; i++){
int L = S[d+1], R = E[d+1];
if(arr[R].x >= arr[i].y) nxtR[i] = lower_bound(arr+L, arr+R+1, dat(arr[i].y, INF)) - arr;
else nxtR[i] = L, dayR[i] = 1;
childR[nxtR[i]].push_back(i);
}
}
for(int d=3; d<=n; d++){
for(int i=S[d-1]; i<=E[d-1]; i++){
int L = S[d-2], R = E[d-2];
if(arrRev[L].x <= arrRev[i].y) nxtL[i] = upper_bound(arrRev+L, arrRev+R+1, dat(arrRev[i].y, -INF)) - arrRev - 1;
else nxtL[i] = R, dayL[i] = 1;
childL[nxtL[i]].push_back(i);
}
}
ll qsum = 0;
scanf("%d", &q);
for(int i=1; i<=q; i++){
int l, r;
scanf("%d %d", &l, &r);
if(queryMap.count(make_pair(l, r))){
prvQuery[i] = queryMap[make_pair(l, r)];
continue;
}
queryMap[make_pair(l, r)] = i;
int minIdx = tree.query(1, 1, n-1, l, r-1).second;
queries[minIdx].push_back(Query(l, r, i));
ans[i] = LLONG_MAX;
qsum += sz[minIdx];
}
listD[1] = 0;
for(int i=S[1]; i<=E[1]; i++) dfsL(i);
listD[n-1] = 0;
for(int i=S[n-1]; i<=E[n-1]; i++) dfsR(i);
for(int i=1; i<=q; i++) if(prvQuery[i]) ans[i] = ans[prvQuery[i]];
for(int i=1; i<=q; i++) printf("%lld\n", ans[i]);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:96:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | scanf("%d %lld", &n, &T);
| ~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:99:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | scanf("%d", &s);
| ~~~~~^~~~~~~~~~
Main.cpp:103:37: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | for(int j=0; j<s; j++) scanf("%lld %lld", &vec[j].x, &vec[j].y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:139:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
Main.cpp:142:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
142 | scanf("%d %d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
20828 KB |
Output is correct |
2 |
Correct |
57 ms |
25976 KB |
Output is correct |
3 |
Correct |
202 ms |
45136 KB |
Output is correct |
4 |
Correct |
279 ms |
52048 KB |
Output is correct |
5 |
Correct |
225 ms |
45904 KB |
Output is correct |
6 |
Correct |
302 ms |
51796 KB |
Output is correct |
7 |
Correct |
300 ms |
51660 KB |
Output is correct |
8 |
Correct |
220 ms |
44628 KB |
Output is correct |
9 |
Correct |
296 ms |
51696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
20828 KB |
Output is correct |
2 |
Correct |
57 ms |
25976 KB |
Output is correct |
3 |
Correct |
202 ms |
45136 KB |
Output is correct |
4 |
Correct |
279 ms |
52048 KB |
Output is correct |
5 |
Correct |
225 ms |
45904 KB |
Output is correct |
6 |
Correct |
302 ms |
51796 KB |
Output is correct |
7 |
Correct |
300 ms |
51660 KB |
Output is correct |
8 |
Correct |
220 ms |
44628 KB |
Output is correct |
9 |
Correct |
296 ms |
51696 KB |
Output is correct |
10 |
Correct |
3 ms |
20824 KB |
Output is correct |
11 |
Correct |
175 ms |
36280 KB |
Output is correct |
12 |
Correct |
180 ms |
36432 KB |
Output is correct |
13 |
Correct |
173 ms |
35688 KB |
Output is correct |
14 |
Correct |
170 ms |
35640 KB |
Output is correct |
15 |
Correct |
228 ms |
42580 KB |
Output is correct |
16 |
Correct |
50 ms |
25936 KB |
Output is correct |
17 |
Correct |
272 ms |
49996 KB |
Output is correct |
18 |
Correct |
217 ms |
43092 KB |
Output is correct |
19 |
Correct |
279 ms |
49748 KB |
Output is correct |
20 |
Correct |
228 ms |
41560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
20828 KB |
Output is correct |
2 |
Correct |
57 ms |
25976 KB |
Output is correct |
3 |
Correct |
202 ms |
45136 KB |
Output is correct |
4 |
Correct |
279 ms |
52048 KB |
Output is correct |
5 |
Correct |
225 ms |
45904 KB |
Output is correct |
6 |
Correct |
302 ms |
51796 KB |
Output is correct |
7 |
Correct |
300 ms |
51660 KB |
Output is correct |
8 |
Correct |
220 ms |
44628 KB |
Output is correct |
9 |
Correct |
296 ms |
51696 KB |
Output is correct |
10 |
Correct |
271 ms |
57428 KB |
Output is correct |
11 |
Correct |
396 ms |
70680 KB |
Output is correct |
12 |
Correct |
445 ms |
70740 KB |
Output is correct |
13 |
Correct |
340 ms |
64828 KB |
Output is correct |
14 |
Correct |
464 ms |
72636 KB |
Output is correct |
15 |
Correct |
444 ms |
72528 KB |
Output is correct |
16 |
Correct |
311 ms |
58964 KB |
Output is correct |
17 |
Correct |
426 ms |
72504 KB |
Output is correct |
18 |
Correct |
136 ms |
48472 KB |
Output is correct |
19 |
Correct |
132 ms |
47444 KB |
Output is correct |
20 |
Correct |
143 ms |
48976 KB |
Output is correct |
21 |
Correct |
135 ms |
48980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
20828 KB |
Output is correct |
2 |
Correct |
57 ms |
25976 KB |
Output is correct |
3 |
Correct |
202 ms |
45136 KB |
Output is correct |
4 |
Correct |
279 ms |
52048 KB |
Output is correct |
5 |
Correct |
225 ms |
45904 KB |
Output is correct |
6 |
Correct |
302 ms |
51796 KB |
Output is correct |
7 |
Correct |
300 ms |
51660 KB |
Output is correct |
8 |
Correct |
220 ms |
44628 KB |
Output is correct |
9 |
Correct |
296 ms |
51696 KB |
Output is correct |
10 |
Correct |
3 ms |
20824 KB |
Output is correct |
11 |
Correct |
175 ms |
36280 KB |
Output is correct |
12 |
Correct |
180 ms |
36432 KB |
Output is correct |
13 |
Correct |
173 ms |
35688 KB |
Output is correct |
14 |
Correct |
170 ms |
35640 KB |
Output is correct |
15 |
Correct |
228 ms |
42580 KB |
Output is correct |
16 |
Correct |
50 ms |
25936 KB |
Output is correct |
17 |
Correct |
272 ms |
49996 KB |
Output is correct |
18 |
Correct |
217 ms |
43092 KB |
Output is correct |
19 |
Correct |
279 ms |
49748 KB |
Output is correct |
20 |
Correct |
228 ms |
41560 KB |
Output is correct |
21 |
Correct |
271 ms |
57428 KB |
Output is correct |
22 |
Correct |
396 ms |
70680 KB |
Output is correct |
23 |
Correct |
445 ms |
70740 KB |
Output is correct |
24 |
Correct |
340 ms |
64828 KB |
Output is correct |
25 |
Correct |
464 ms |
72636 KB |
Output is correct |
26 |
Correct |
444 ms |
72528 KB |
Output is correct |
27 |
Correct |
311 ms |
58964 KB |
Output is correct |
28 |
Correct |
426 ms |
72504 KB |
Output is correct |
29 |
Correct |
136 ms |
48472 KB |
Output is correct |
30 |
Correct |
132 ms |
47444 KB |
Output is correct |
31 |
Correct |
143 ms |
48976 KB |
Output is correct |
32 |
Correct |
135 ms |
48980 KB |
Output is correct |
33 |
Correct |
434 ms |
72352 KB |
Output is correct |
34 |
Correct |
474 ms |
72272 KB |
Output is correct |
35 |
Correct |
514 ms |
67924 KB |
Output is correct |
36 |
Correct |
475 ms |
67908 KB |
Output is correct |
37 |
Correct |
458 ms |
59864 KB |
Output is correct |
38 |
Correct |
270 ms |
53844 KB |
Output is correct |
39 |
Correct |
386 ms |
59740 KB |
Output is correct |
40 |
Correct |
351 ms |
52312 KB |
Output is correct |
41 |
Correct |
337 ms |
58072 KB |
Output is correct |
42 |
Correct |
401 ms |
65876 KB |
Output is correct |
43 |
Correct |
306 ms |
54608 KB |
Output is correct |
44 |
Correct |
421 ms |
59988 KB |
Output is correct |
45 |
Correct |
129 ms |
43136 KB |
Output is correct |
46 |
Correct |
115 ms |
38104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
20828 KB |
Output is correct |
2 |
Correct |
3 ms |
20828 KB |
Output is correct |
3 |
Correct |
212 ms |
140176 KB |
Output is correct |
4 |
Correct |
203 ms |
140176 KB |
Output is correct |
5 |
Correct |
214 ms |
140104 KB |
Output is correct |
6 |
Correct |
208 ms |
140484 KB |
Output is correct |
7 |
Correct |
218 ms |
140644 KB |
Output is correct |
8 |
Correct |
147 ms |
82768 KB |
Output is correct |
9 |
Correct |
82 ms |
32040 KB |
Output is correct |
10 |
Correct |
40 ms |
24964 KB |
Output is correct |
11 |
Correct |
142 ms |
48644 KB |
Output is correct |
12 |
Correct |
132 ms |
43092 KB |
Output is correct |
13 |
Correct |
149 ms |
37968 KB |
Output is correct |
14 |
Correct |
95 ms |
33636 KB |
Output is correct |
15 |
Correct |
94 ms |
32820 KB |
Output is correct |
16 |
Correct |
109 ms |
36432 KB |
Output is correct |
17 |
Correct |
117 ms |
35664 KB |
Output is correct |
18 |
Correct |
96 ms |
30552 KB |
Output is correct |
19 |
Correct |
137 ms |
32748 KB |
Output is correct |
20 |
Correct |
72 ms |
36692 KB |
Output is correct |
21 |
Correct |
102 ms |
40008 KB |
Output is correct |
22 |
Correct |
76 ms |
32680 KB |
Output is correct |
23 |
Correct |
104 ms |
35664 KB |
Output is correct |
24 |
Correct |
98 ms |
35928 KB |
Output is correct |
25 |
Correct |
64 ms |
31884 KB |
Output is correct |
26 |
Correct |
115 ms |
38732 KB |
Output is correct |
27 |
Correct |
227 ms |
47568 KB |
Output is correct |
28 |
Correct |
148 ms |
48980 KB |
Output is correct |
29 |
Correct |
194 ms |
48900 KB |
Output is correct |
30 |
Correct |
113 ms |
33048 KB |
Output is correct |
31 |
Correct |
45 ms |
24184 KB |
Output is correct |
32 |
Correct |
135 ms |
32764 KB |
Output is correct |
33 |
Correct |
67 ms |
25372 KB |
Output is correct |
34 |
Correct |
103 ms |
30232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
20828 KB |
Output is correct |
2 |
Correct |
57 ms |
25976 KB |
Output is correct |
3 |
Correct |
202 ms |
45136 KB |
Output is correct |
4 |
Correct |
279 ms |
52048 KB |
Output is correct |
5 |
Correct |
225 ms |
45904 KB |
Output is correct |
6 |
Correct |
302 ms |
51796 KB |
Output is correct |
7 |
Correct |
300 ms |
51660 KB |
Output is correct |
8 |
Correct |
220 ms |
44628 KB |
Output is correct |
9 |
Correct |
296 ms |
51696 KB |
Output is correct |
10 |
Correct |
3 ms |
20824 KB |
Output is correct |
11 |
Correct |
175 ms |
36280 KB |
Output is correct |
12 |
Correct |
180 ms |
36432 KB |
Output is correct |
13 |
Correct |
173 ms |
35688 KB |
Output is correct |
14 |
Correct |
170 ms |
35640 KB |
Output is correct |
15 |
Correct |
228 ms |
42580 KB |
Output is correct |
16 |
Correct |
50 ms |
25936 KB |
Output is correct |
17 |
Correct |
272 ms |
49996 KB |
Output is correct |
18 |
Correct |
217 ms |
43092 KB |
Output is correct |
19 |
Correct |
279 ms |
49748 KB |
Output is correct |
20 |
Correct |
228 ms |
41560 KB |
Output is correct |
21 |
Correct |
271 ms |
57428 KB |
Output is correct |
22 |
Correct |
396 ms |
70680 KB |
Output is correct |
23 |
Correct |
445 ms |
70740 KB |
Output is correct |
24 |
Correct |
340 ms |
64828 KB |
Output is correct |
25 |
Correct |
464 ms |
72636 KB |
Output is correct |
26 |
Correct |
444 ms |
72528 KB |
Output is correct |
27 |
Correct |
311 ms |
58964 KB |
Output is correct |
28 |
Correct |
426 ms |
72504 KB |
Output is correct |
29 |
Correct |
136 ms |
48472 KB |
Output is correct |
30 |
Correct |
132 ms |
47444 KB |
Output is correct |
31 |
Correct |
143 ms |
48976 KB |
Output is correct |
32 |
Correct |
135 ms |
48980 KB |
Output is correct |
33 |
Correct |
434 ms |
72352 KB |
Output is correct |
34 |
Correct |
474 ms |
72272 KB |
Output is correct |
35 |
Correct |
514 ms |
67924 KB |
Output is correct |
36 |
Correct |
475 ms |
67908 KB |
Output is correct |
37 |
Correct |
458 ms |
59864 KB |
Output is correct |
38 |
Correct |
270 ms |
53844 KB |
Output is correct |
39 |
Correct |
386 ms |
59740 KB |
Output is correct |
40 |
Correct |
351 ms |
52312 KB |
Output is correct |
41 |
Correct |
337 ms |
58072 KB |
Output is correct |
42 |
Correct |
401 ms |
65876 KB |
Output is correct |
43 |
Correct |
306 ms |
54608 KB |
Output is correct |
44 |
Correct |
421 ms |
59988 KB |
Output is correct |
45 |
Correct |
129 ms |
43136 KB |
Output is correct |
46 |
Correct |
115 ms |
38104 KB |
Output is correct |
47 |
Correct |
3 ms |
20828 KB |
Output is correct |
48 |
Correct |
3 ms |
20828 KB |
Output is correct |
49 |
Correct |
212 ms |
140176 KB |
Output is correct |
50 |
Correct |
203 ms |
140176 KB |
Output is correct |
51 |
Correct |
214 ms |
140104 KB |
Output is correct |
52 |
Correct |
208 ms |
140484 KB |
Output is correct |
53 |
Correct |
218 ms |
140644 KB |
Output is correct |
54 |
Correct |
147 ms |
82768 KB |
Output is correct |
55 |
Correct |
82 ms |
32040 KB |
Output is correct |
56 |
Correct |
40 ms |
24964 KB |
Output is correct |
57 |
Correct |
142 ms |
48644 KB |
Output is correct |
58 |
Correct |
132 ms |
43092 KB |
Output is correct |
59 |
Correct |
149 ms |
37968 KB |
Output is correct |
60 |
Correct |
95 ms |
33636 KB |
Output is correct |
61 |
Correct |
94 ms |
32820 KB |
Output is correct |
62 |
Correct |
109 ms |
36432 KB |
Output is correct |
63 |
Correct |
117 ms |
35664 KB |
Output is correct |
64 |
Correct |
96 ms |
30552 KB |
Output is correct |
65 |
Correct |
137 ms |
32748 KB |
Output is correct |
66 |
Correct |
72 ms |
36692 KB |
Output is correct |
67 |
Correct |
102 ms |
40008 KB |
Output is correct |
68 |
Correct |
76 ms |
32680 KB |
Output is correct |
69 |
Correct |
104 ms |
35664 KB |
Output is correct |
70 |
Correct |
98 ms |
35928 KB |
Output is correct |
71 |
Correct |
64 ms |
31884 KB |
Output is correct |
72 |
Correct |
115 ms |
38732 KB |
Output is correct |
73 |
Correct |
227 ms |
47568 KB |
Output is correct |
74 |
Correct |
148 ms |
48980 KB |
Output is correct |
75 |
Correct |
194 ms |
48900 KB |
Output is correct |
76 |
Correct |
113 ms |
33048 KB |
Output is correct |
77 |
Correct |
45 ms |
24184 KB |
Output is correct |
78 |
Correct |
135 ms |
32764 KB |
Output is correct |
79 |
Correct |
67 ms |
25372 KB |
Output is correct |
80 |
Correct |
103 ms |
30232 KB |
Output is correct |
81 |
Correct |
571 ms |
255964 KB |
Output is correct |
82 |
Correct |
562 ms |
255904 KB |
Output is correct |
83 |
Correct |
530 ms |
255624 KB |
Output is correct |
84 |
Correct |
508 ms |
255192 KB |
Output is correct |
85 |
Correct |
510 ms |
255060 KB |
Output is correct |
86 |
Correct |
410 ms |
145020 KB |
Output is correct |
87 |
Correct |
273 ms |
43752 KB |
Output is correct |
88 |
Correct |
690 ms |
358364 KB |
Output is correct |
89 |
Correct |
590 ms |
287460 KB |
Output is correct |
90 |
Correct |
474 ms |
166976 KB |
Output is correct |
91 |
Correct |
78 ms |
28892 KB |
Output is correct |
92 |
Correct |
293 ms |
49016 KB |
Output is correct |
93 |
Correct |
316 ms |
47872 KB |
Output is correct |
94 |
Correct |
370 ms |
63580 KB |
Output is correct |
95 |
Correct |
371 ms |
58860 KB |
Output is correct |
96 |
Correct |
288 ms |
46620 KB |
Output is correct |
97 |
Correct |
208 ms |
45320 KB |
Output is correct |
98 |
Correct |
175 ms |
45136 KB |
Output is correct |
99 |
Correct |
338 ms |
55244 KB |
Output is correct |
100 |
Correct |
176 ms |
40980 KB |
Output is correct |
101 |
Correct |
352 ms |
50260 KB |
Output is correct |
102 |
Correct |
244 ms |
48636 KB |
Output is correct |
103 |
Correct |
146 ms |
41712 KB |
Output is correct |
104 |
Correct |
347 ms |
58176 KB |
Output is correct |
105 |
Correct |
428 ms |
54788 KB |
Output is correct |
106 |
Correct |
135 ms |
29560 KB |
Output is correct |
107 |
Correct |
352 ms |
56884 KB |
Output is correct |
108 |
Correct |
185 ms |
33840 KB |
Output is correct |
109 |
Correct |
291 ms |
41792 KB |
Output is correct |