#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];
int nxtR[100002], dayR[100002], nxtL[100002], dayL[100002];
int nxtRsps[100002][DEPTH], dayRsps[100002][DEPTH], nxtLsps[100002][DEPTH], dayLsps[100002][DEPTH];
int recentL[100002], Lloc[100002], recentR[100002], Rloc[100002];
int qlx[500002], qld[500002], qrx[500002], qrd[500002], qlcnt, qrcnt;
vector<Query> queries[100002];
ll ans[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);
}
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;
}
}
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;
}
}
K = E[n-1];
for(int i=1; i<=K; i++) nxtLsps[i][0] = nxtL[i], nxtRsps[i][0] = nxtR[i], dayLsps[i][0] = dayL[i], dayRsps[i][0] = dayR[i];
for(int d=1; d<DEPTH; d++){
for(int i=1; i<=K; i++){
nxtLsps[i][d] = nxtLsps[nxtLsps[i][d-1]][d-1];
dayLsps[i][d] = dayLsps[i][d-1] + dayLsps[nxtLsps[i][d-1]][d-1];
nxtRsps[i][d] = nxtRsps[nxtRsps[i][d-1]][d-1];
dayRsps[i][d] = dayRsps[i][d-1] + dayRsps[nxtRsps[i][d-1]][d-1];
}
}
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));
}
for(int m=1; m<n; m++){
qlcnt = qrcnt = 0;
for(Query &p: queries[m]){
int L = p.l, R = p.r, LD = m - L, RD = R - m - 1;
if(recentL[L] != m){
recentL[L] = m, Lloc[L] = qlcnt+1;
for(int i=S[m]; i<=E[m]; i++){
int x = ++qlcnt;
qlx[x] = i, qld[x] = 0;
for(int d=0; d<DEPTH && (1<<d)<=LD; d++) if((LD>>d)&1) qld[x] += dayLsps[qlx[x]][d], qlx[x] = nxtLsps[qlx[x]][d];
}
}
if(recentR[R] != m){
recentR[R] = m, Rloc[R] = qrcnt+1;
for(int i=S[m]; i<=E[m]; i++){
int x = ++qrcnt;
qrx[x] = i, qrd[x] = 0;
for(int d=0; d<DEPTH && (1<<d)<=RD; d++) if((RD>>d)&1) qrd[x] += dayRsps[qrx[x]][d], qrx[x] = nxtRsps[qrx[x]][d];
}
}
assert(qlcnt <= 500000 && qrcnt <= 500000);
int idx = p.idx;
ans[idx] = LLONG_MAX;
for(int i=S[m]; i<=E[m]; i++){
int lp = Lloc[L] + (i - S[m]), rp = Rloc[R] + (i - S[m]);
ll v = (qld[lp] + qrd[rp]) * T + (arr[qrx[rp]].y - arrRev[qlx[lp]].y);
ans[idx] = min(ans[idx], v);
}
}
}
for(int i=1; i<=q; i++) printf("%lld\n", ans[i]);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%d %lld", &n, &T);
| ~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | scanf("%d", &s);
| ~~~~~^~~~~~~~~~
Main.cpp:72:37: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | for(int j=0; j<s; j++) scanf("%lld %lld", &vec[j].x, &vec[j].y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:115:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
Main.cpp:118:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | scanf("%d %d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
29020 KB |
Output is correct |
2 |
Correct |
51 ms |
36940 KB |
Output is correct |
3 |
Correct |
92 ms |
38880 KB |
Output is correct |
4 |
Correct |
122 ms |
41040 KB |
Output is correct |
5 |
Correct |
100 ms |
38756 KB |
Output is correct |
6 |
Correct |
131 ms |
41044 KB |
Output is correct |
7 |
Correct |
126 ms |
41300 KB |
Output is correct |
8 |
Correct |
93 ms |
38996 KB |
Output is correct |
9 |
Correct |
133 ms |
41092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
29020 KB |
Output is correct |
2 |
Correct |
51 ms |
36940 KB |
Output is correct |
3 |
Correct |
92 ms |
38880 KB |
Output is correct |
4 |
Correct |
122 ms |
41040 KB |
Output is correct |
5 |
Correct |
100 ms |
38756 KB |
Output is correct |
6 |
Correct |
131 ms |
41044 KB |
Output is correct |
7 |
Correct |
126 ms |
41300 KB |
Output is correct |
8 |
Correct |
93 ms |
38996 KB |
Output is correct |
9 |
Correct |
133 ms |
41092 KB |
Output is correct |
10 |
Correct |
3 ms |
29016 KB |
Output is correct |
11 |
Correct |
109 ms |
40268 KB |
Output is correct |
12 |
Correct |
115 ms |
40016 KB |
Output is correct |
13 |
Correct |
99 ms |
39700 KB |
Output is correct |
14 |
Correct |
101 ms |
39752 KB |
Output is correct |
15 |
Correct |
105 ms |
40016 KB |
Output is correct |
16 |
Correct |
59 ms |
37556 KB |
Output is correct |
17 |
Correct |
112 ms |
40364 KB |
Output is correct |
18 |
Correct |
102 ms |
39680 KB |
Output is correct |
19 |
Correct |
124 ms |
40528 KB |
Output is correct |
20 |
Correct |
102 ms |
39876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
29020 KB |
Output is correct |
2 |
Correct |
51 ms |
36940 KB |
Output is correct |
3 |
Correct |
92 ms |
38880 KB |
Output is correct |
4 |
Correct |
122 ms |
41040 KB |
Output is correct |
5 |
Correct |
100 ms |
38756 KB |
Output is correct |
6 |
Correct |
131 ms |
41044 KB |
Output is correct |
7 |
Correct |
126 ms |
41300 KB |
Output is correct |
8 |
Correct |
93 ms |
38996 KB |
Output is correct |
9 |
Correct |
133 ms |
41092 KB |
Output is correct |
10 |
Correct |
168 ms |
56916 KB |
Output is correct |
11 |
Correct |
250 ms |
60100 KB |
Output is correct |
12 |
Correct |
278 ms |
60244 KB |
Output is correct |
13 |
Correct |
199 ms |
57680 KB |
Output is correct |
14 |
Correct |
257 ms |
60240 KB |
Output is correct |
15 |
Correct |
293 ms |
60244 KB |
Output is correct |
16 |
Correct |
173 ms |
56916 KB |
Output is correct |
17 |
Correct |
257 ms |
60468 KB |
Output is correct |
18 |
Correct |
103 ms |
52996 KB |
Output is correct |
19 |
Correct |
97 ms |
52568 KB |
Output is correct |
20 |
Correct |
109 ms |
53068 KB |
Output is correct |
21 |
Correct |
107 ms |
53056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
29020 KB |
Output is correct |
2 |
Correct |
51 ms |
36940 KB |
Output is correct |
3 |
Correct |
92 ms |
38880 KB |
Output is correct |
4 |
Correct |
122 ms |
41040 KB |
Output is correct |
5 |
Correct |
100 ms |
38756 KB |
Output is correct |
6 |
Correct |
131 ms |
41044 KB |
Output is correct |
7 |
Correct |
126 ms |
41300 KB |
Output is correct |
8 |
Correct |
93 ms |
38996 KB |
Output is correct |
9 |
Correct |
133 ms |
41092 KB |
Output is correct |
10 |
Correct |
3 ms |
29016 KB |
Output is correct |
11 |
Correct |
109 ms |
40268 KB |
Output is correct |
12 |
Correct |
115 ms |
40016 KB |
Output is correct |
13 |
Correct |
99 ms |
39700 KB |
Output is correct |
14 |
Correct |
101 ms |
39752 KB |
Output is correct |
15 |
Correct |
105 ms |
40016 KB |
Output is correct |
16 |
Correct |
59 ms |
37556 KB |
Output is correct |
17 |
Correct |
112 ms |
40364 KB |
Output is correct |
18 |
Correct |
102 ms |
39680 KB |
Output is correct |
19 |
Correct |
124 ms |
40528 KB |
Output is correct |
20 |
Correct |
102 ms |
39876 KB |
Output is correct |
21 |
Correct |
168 ms |
56916 KB |
Output is correct |
22 |
Correct |
250 ms |
60100 KB |
Output is correct |
23 |
Correct |
278 ms |
60244 KB |
Output is correct |
24 |
Correct |
199 ms |
57680 KB |
Output is correct |
25 |
Correct |
257 ms |
60240 KB |
Output is correct |
26 |
Correct |
293 ms |
60244 KB |
Output is correct |
27 |
Correct |
173 ms |
56916 KB |
Output is correct |
28 |
Correct |
257 ms |
60468 KB |
Output is correct |
29 |
Correct |
103 ms |
52996 KB |
Output is correct |
30 |
Correct |
97 ms |
52568 KB |
Output is correct |
31 |
Correct |
109 ms |
53068 KB |
Output is correct |
32 |
Correct |
107 ms |
53056 KB |
Output is correct |
33 |
Correct |
261 ms |
59848 KB |
Output is correct |
34 |
Correct |
270 ms |
59844 KB |
Output is correct |
35 |
Correct |
235 ms |
59604 KB |
Output is correct |
36 |
Correct |
256 ms |
59728 KB |
Output is correct |
37 |
Correct |
205 ms |
60356 KB |
Output is correct |
38 |
Correct |
173 ms |
57172 KB |
Output is correct |
39 |
Correct |
226 ms |
59476 KB |
Output is correct |
40 |
Correct |
176 ms |
56912 KB |
Output is correct |
41 |
Correct |
197 ms |
56272 KB |
Output is correct |
42 |
Correct |
240 ms |
59984 KB |
Output is correct |
43 |
Correct |
183 ms |
57680 KB |
Output is correct |
44 |
Correct |
214 ms |
59472 KB |
Output is correct |
45 |
Correct |
100 ms |
52748 KB |
Output is correct |
46 |
Correct |
96 ms |
52680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
29016 KB |
Output is correct |
2 |
Correct |
3 ms |
29020 KB |
Output is correct |
3 |
Correct |
255 ms |
52288 KB |
Output is correct |
4 |
Correct |
263 ms |
52312 KB |
Output is correct |
5 |
Correct |
257 ms |
52376 KB |
Output is correct |
6 |
Correct |
182 ms |
52304 KB |
Output is correct |
7 |
Correct |
189 ms |
52260 KB |
Output is correct |
8 |
Correct |
153 ms |
44256 KB |
Output is correct |
9 |
Correct |
60 ms |
52556 KB |
Output is correct |
10 |
Execution timed out |
2054 ms |
42012 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
29020 KB |
Output is correct |
2 |
Correct |
51 ms |
36940 KB |
Output is correct |
3 |
Correct |
92 ms |
38880 KB |
Output is correct |
4 |
Correct |
122 ms |
41040 KB |
Output is correct |
5 |
Correct |
100 ms |
38756 KB |
Output is correct |
6 |
Correct |
131 ms |
41044 KB |
Output is correct |
7 |
Correct |
126 ms |
41300 KB |
Output is correct |
8 |
Correct |
93 ms |
38996 KB |
Output is correct |
9 |
Correct |
133 ms |
41092 KB |
Output is correct |
10 |
Correct |
3 ms |
29016 KB |
Output is correct |
11 |
Correct |
109 ms |
40268 KB |
Output is correct |
12 |
Correct |
115 ms |
40016 KB |
Output is correct |
13 |
Correct |
99 ms |
39700 KB |
Output is correct |
14 |
Correct |
101 ms |
39752 KB |
Output is correct |
15 |
Correct |
105 ms |
40016 KB |
Output is correct |
16 |
Correct |
59 ms |
37556 KB |
Output is correct |
17 |
Correct |
112 ms |
40364 KB |
Output is correct |
18 |
Correct |
102 ms |
39680 KB |
Output is correct |
19 |
Correct |
124 ms |
40528 KB |
Output is correct |
20 |
Correct |
102 ms |
39876 KB |
Output is correct |
21 |
Correct |
168 ms |
56916 KB |
Output is correct |
22 |
Correct |
250 ms |
60100 KB |
Output is correct |
23 |
Correct |
278 ms |
60244 KB |
Output is correct |
24 |
Correct |
199 ms |
57680 KB |
Output is correct |
25 |
Correct |
257 ms |
60240 KB |
Output is correct |
26 |
Correct |
293 ms |
60244 KB |
Output is correct |
27 |
Correct |
173 ms |
56916 KB |
Output is correct |
28 |
Correct |
257 ms |
60468 KB |
Output is correct |
29 |
Correct |
103 ms |
52996 KB |
Output is correct |
30 |
Correct |
97 ms |
52568 KB |
Output is correct |
31 |
Correct |
109 ms |
53068 KB |
Output is correct |
32 |
Correct |
107 ms |
53056 KB |
Output is correct |
33 |
Correct |
261 ms |
59848 KB |
Output is correct |
34 |
Correct |
270 ms |
59844 KB |
Output is correct |
35 |
Correct |
235 ms |
59604 KB |
Output is correct |
36 |
Correct |
256 ms |
59728 KB |
Output is correct |
37 |
Correct |
205 ms |
60356 KB |
Output is correct |
38 |
Correct |
173 ms |
57172 KB |
Output is correct |
39 |
Correct |
226 ms |
59476 KB |
Output is correct |
40 |
Correct |
176 ms |
56912 KB |
Output is correct |
41 |
Correct |
197 ms |
56272 KB |
Output is correct |
42 |
Correct |
240 ms |
59984 KB |
Output is correct |
43 |
Correct |
183 ms |
57680 KB |
Output is correct |
44 |
Correct |
214 ms |
59472 KB |
Output is correct |
45 |
Correct |
100 ms |
52748 KB |
Output is correct |
46 |
Correct |
96 ms |
52680 KB |
Output is correct |
47 |
Correct |
4 ms |
29016 KB |
Output is correct |
48 |
Correct |
3 ms |
29020 KB |
Output is correct |
49 |
Correct |
255 ms |
52288 KB |
Output is correct |
50 |
Correct |
263 ms |
52312 KB |
Output is correct |
51 |
Correct |
257 ms |
52376 KB |
Output is correct |
52 |
Correct |
182 ms |
52304 KB |
Output is correct |
53 |
Correct |
189 ms |
52260 KB |
Output is correct |
54 |
Correct |
153 ms |
44256 KB |
Output is correct |
55 |
Correct |
60 ms |
52556 KB |
Output is correct |
56 |
Execution timed out |
2054 ms |
42012 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |