#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[300002], listD[300002];
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]--;
}
}
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);
}
}
scanf("%d", &q);
for(int i=1; i<=q; i++){
int l, r;
scanf("%d %d", &l, &r);
int minIdx = tree.query(1, 1, n-1, l, r-1).second;
queries[minIdx].push_back(Query(l, r, i));
ans[i] = LLONG_MAX;
}
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++) printf("%lld\n", ans[i]);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:92:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | scanf("%d %lld", &n, &T);
| ~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:95:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | scanf("%d", &s);
| ~~~~~^~~~~~~~~~
Main.cpp:99:37: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | for(int j=0; j<s; j++) scanf("%lld %lld", &vec[j].x, &vec[j].y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:133:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
Main.cpp:136:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | scanf("%d %d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22876 KB |
Output is correct |
2 |
Correct |
90 ms |
32688 KB |
Output is correct |
3 |
Correct |
90 ms |
33204 KB |
Output is correct |
4 |
Correct |
114 ms |
36176 KB |
Output is correct |
5 |
Correct |
94 ms |
33364 KB |
Output is correct |
6 |
Correct |
117 ms |
36304 KB |
Output is correct |
7 |
Correct |
122 ms |
36176 KB |
Output is correct |
8 |
Correct |
89 ms |
33300 KB |
Output is correct |
9 |
Correct |
129 ms |
36176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22876 KB |
Output is correct |
2 |
Correct |
90 ms |
32688 KB |
Output is correct |
3 |
Correct |
90 ms |
33204 KB |
Output is correct |
4 |
Correct |
114 ms |
36176 KB |
Output is correct |
5 |
Correct |
94 ms |
33364 KB |
Output is correct |
6 |
Correct |
117 ms |
36304 KB |
Output is correct |
7 |
Correct |
122 ms |
36176 KB |
Output is correct |
8 |
Correct |
89 ms |
33300 KB |
Output is correct |
9 |
Correct |
129 ms |
36176 KB |
Output is correct |
10 |
Correct |
3 ms |
22872 KB |
Output is correct |
11 |
Correct |
117 ms |
44588 KB |
Output is correct |
12 |
Correct |
109 ms |
44368 KB |
Output is correct |
13 |
Correct |
139 ms |
41940 KB |
Output is correct |
14 |
Correct |
123 ms |
41728 KB |
Output is correct |
15 |
Correct |
114 ms |
34952 KB |
Output is correct |
16 |
Correct |
52 ms |
35248 KB |
Output is correct |
17 |
Correct |
118 ms |
35812 KB |
Output is correct |
18 |
Correct |
137 ms |
34912 KB |
Output is correct |
19 |
Correct |
131 ms |
35668 KB |
Output is correct |
20 |
Correct |
149 ms |
34900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22876 KB |
Output is correct |
2 |
Correct |
90 ms |
32688 KB |
Output is correct |
3 |
Correct |
90 ms |
33204 KB |
Output is correct |
4 |
Correct |
114 ms |
36176 KB |
Output is correct |
5 |
Correct |
94 ms |
33364 KB |
Output is correct |
6 |
Correct |
117 ms |
36304 KB |
Output is correct |
7 |
Correct |
122 ms |
36176 KB |
Output is correct |
8 |
Correct |
89 ms |
33300 KB |
Output is correct |
9 |
Correct |
129 ms |
36176 KB |
Output is correct |
10 |
Correct |
196 ms |
48544 KB |
Output is correct |
11 |
Correct |
236 ms |
57268 KB |
Output is correct |
12 |
Correct |
233 ms |
56948 KB |
Output is correct |
13 |
Correct |
196 ms |
53588 KB |
Output is correct |
14 |
Correct |
249 ms |
57180 KB |
Output is correct |
15 |
Correct |
227 ms |
57276 KB |
Output is correct |
16 |
Correct |
178 ms |
48636 KB |
Output is correct |
17 |
Correct |
232 ms |
57168 KB |
Output is correct |
18 |
Correct |
114 ms |
45056 KB |
Output is correct |
19 |
Correct |
92 ms |
44632 KB |
Output is correct |
20 |
Correct |
113 ms |
45332 KB |
Output is correct |
21 |
Correct |
108 ms |
45388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22876 KB |
Output is correct |
2 |
Correct |
90 ms |
32688 KB |
Output is correct |
3 |
Correct |
90 ms |
33204 KB |
Output is correct |
4 |
Correct |
114 ms |
36176 KB |
Output is correct |
5 |
Correct |
94 ms |
33364 KB |
Output is correct |
6 |
Correct |
117 ms |
36304 KB |
Output is correct |
7 |
Correct |
122 ms |
36176 KB |
Output is correct |
8 |
Correct |
89 ms |
33300 KB |
Output is correct |
9 |
Correct |
129 ms |
36176 KB |
Output is correct |
10 |
Correct |
3 ms |
22872 KB |
Output is correct |
11 |
Correct |
117 ms |
44588 KB |
Output is correct |
12 |
Correct |
109 ms |
44368 KB |
Output is correct |
13 |
Correct |
139 ms |
41940 KB |
Output is correct |
14 |
Correct |
123 ms |
41728 KB |
Output is correct |
15 |
Correct |
114 ms |
34952 KB |
Output is correct |
16 |
Correct |
52 ms |
35248 KB |
Output is correct |
17 |
Correct |
118 ms |
35812 KB |
Output is correct |
18 |
Correct |
137 ms |
34912 KB |
Output is correct |
19 |
Correct |
131 ms |
35668 KB |
Output is correct |
20 |
Correct |
149 ms |
34900 KB |
Output is correct |
21 |
Correct |
196 ms |
48544 KB |
Output is correct |
22 |
Correct |
236 ms |
57268 KB |
Output is correct |
23 |
Correct |
233 ms |
56948 KB |
Output is correct |
24 |
Correct |
196 ms |
53588 KB |
Output is correct |
25 |
Correct |
249 ms |
57180 KB |
Output is correct |
26 |
Correct |
227 ms |
57276 KB |
Output is correct |
27 |
Correct |
178 ms |
48636 KB |
Output is correct |
28 |
Correct |
232 ms |
57168 KB |
Output is correct |
29 |
Correct |
114 ms |
45056 KB |
Output is correct |
30 |
Correct |
92 ms |
44632 KB |
Output is correct |
31 |
Correct |
113 ms |
45332 KB |
Output is correct |
32 |
Correct |
108 ms |
45388 KB |
Output is correct |
33 |
Correct |
250 ms |
56268 KB |
Output is correct |
34 |
Correct |
215 ms |
56148 KB |
Output is correct |
35 |
Correct |
225 ms |
51828 KB |
Output is correct |
36 |
Correct |
210 ms |
51796 KB |
Output is correct |
37 |
Correct |
187 ms |
43640 KB |
Output is correct |
38 |
Correct |
192 ms |
43856 KB |
Output is correct |
39 |
Correct |
205 ms |
50260 KB |
Output is correct |
40 |
Correct |
173 ms |
41692 KB |
Output is correct |
41 |
Correct |
178 ms |
44880 KB |
Output is correct |
42 |
Correct |
210 ms |
50516 KB |
Output is correct |
43 |
Correct |
157 ms |
41544 KB |
Output is correct |
44 |
Correct |
191 ms |
44116 KB |
Output is correct |
45 |
Correct |
90 ms |
39508 KB |
Output is correct |
46 |
Correct |
77 ms |
34360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
22872 KB |
Output is correct |
2 |
Correct |
3 ms |
22876 KB |
Output is correct |
3 |
Correct |
273 ms |
244816 KB |
Output is correct |
4 |
Correct |
273 ms |
244744 KB |
Output is correct |
5 |
Correct |
280 ms |
245076 KB |
Output is correct |
6 |
Correct |
276 ms |
244228 KB |
Output is correct |
7 |
Correct |
279 ms |
244312 KB |
Output is correct |
8 |
Correct |
160 ms |
134736 KB |
Output is correct |
9 |
Correct |
53 ms |
31992 KB |
Output is correct |
10 |
Runtime error |
575 ms |
1048576 KB |
Execution killed with signal 9 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22876 KB |
Output is correct |
2 |
Correct |
90 ms |
32688 KB |
Output is correct |
3 |
Correct |
90 ms |
33204 KB |
Output is correct |
4 |
Correct |
114 ms |
36176 KB |
Output is correct |
5 |
Correct |
94 ms |
33364 KB |
Output is correct |
6 |
Correct |
117 ms |
36304 KB |
Output is correct |
7 |
Correct |
122 ms |
36176 KB |
Output is correct |
8 |
Correct |
89 ms |
33300 KB |
Output is correct |
9 |
Correct |
129 ms |
36176 KB |
Output is correct |
10 |
Correct |
3 ms |
22872 KB |
Output is correct |
11 |
Correct |
117 ms |
44588 KB |
Output is correct |
12 |
Correct |
109 ms |
44368 KB |
Output is correct |
13 |
Correct |
139 ms |
41940 KB |
Output is correct |
14 |
Correct |
123 ms |
41728 KB |
Output is correct |
15 |
Correct |
114 ms |
34952 KB |
Output is correct |
16 |
Correct |
52 ms |
35248 KB |
Output is correct |
17 |
Correct |
118 ms |
35812 KB |
Output is correct |
18 |
Correct |
137 ms |
34912 KB |
Output is correct |
19 |
Correct |
131 ms |
35668 KB |
Output is correct |
20 |
Correct |
149 ms |
34900 KB |
Output is correct |
21 |
Correct |
196 ms |
48544 KB |
Output is correct |
22 |
Correct |
236 ms |
57268 KB |
Output is correct |
23 |
Correct |
233 ms |
56948 KB |
Output is correct |
24 |
Correct |
196 ms |
53588 KB |
Output is correct |
25 |
Correct |
249 ms |
57180 KB |
Output is correct |
26 |
Correct |
227 ms |
57276 KB |
Output is correct |
27 |
Correct |
178 ms |
48636 KB |
Output is correct |
28 |
Correct |
232 ms |
57168 KB |
Output is correct |
29 |
Correct |
114 ms |
45056 KB |
Output is correct |
30 |
Correct |
92 ms |
44632 KB |
Output is correct |
31 |
Correct |
113 ms |
45332 KB |
Output is correct |
32 |
Correct |
108 ms |
45388 KB |
Output is correct |
33 |
Correct |
250 ms |
56268 KB |
Output is correct |
34 |
Correct |
215 ms |
56148 KB |
Output is correct |
35 |
Correct |
225 ms |
51828 KB |
Output is correct |
36 |
Correct |
210 ms |
51796 KB |
Output is correct |
37 |
Correct |
187 ms |
43640 KB |
Output is correct |
38 |
Correct |
192 ms |
43856 KB |
Output is correct |
39 |
Correct |
205 ms |
50260 KB |
Output is correct |
40 |
Correct |
173 ms |
41692 KB |
Output is correct |
41 |
Correct |
178 ms |
44880 KB |
Output is correct |
42 |
Correct |
210 ms |
50516 KB |
Output is correct |
43 |
Correct |
157 ms |
41544 KB |
Output is correct |
44 |
Correct |
191 ms |
44116 KB |
Output is correct |
45 |
Correct |
90 ms |
39508 KB |
Output is correct |
46 |
Correct |
77 ms |
34360 KB |
Output is correct |
47 |
Correct |
3 ms |
22872 KB |
Output is correct |
48 |
Correct |
3 ms |
22876 KB |
Output is correct |
49 |
Correct |
273 ms |
244816 KB |
Output is correct |
50 |
Correct |
273 ms |
244744 KB |
Output is correct |
51 |
Correct |
280 ms |
245076 KB |
Output is correct |
52 |
Correct |
276 ms |
244228 KB |
Output is correct |
53 |
Correct |
279 ms |
244312 KB |
Output is correct |
54 |
Correct |
160 ms |
134736 KB |
Output is correct |
55 |
Correct |
53 ms |
31992 KB |
Output is correct |
56 |
Runtime error |
575 ms |
1048576 KB |
Execution killed with signal 9 |
57 |
Halted |
0 ms |
0 KB |
- |