#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 Lloc[100002], Rloc[100002];
int qlx[500002], qld[500002], qrx[500002], qrd[500002], qlcnt, qrcnt, qllim, qrlim;
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;
}
}
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 = sz[m];
qllim = m, qrlim = m+1;
Lloc[m] = Rloc[m+1] = 1;
for(int i=S[m]; i<=E[m]; i++){
qlx[i-S[m]+1] = i, qld[i-S[m]+1] = 0;
qrx[i-S[m]+1] = i, qrd[i-S[m]+1] = 0;
}
for(Query &p: queries[m]){
int L = p.l, R = p.r;
while(qllim > L){
for(int i=qlcnt - sz[m] + 1; i<=qlcnt; i++){
qlx[i+sz[m]] = nxtL[qlx[i]], qld[i+sz[m]] = qld[i] + dayL[qlx[i]];
}
Lloc[--qllim] = qlcnt + 1, qlcnt += sz[m];
}
while(qrlim < R){
for(int i=qrcnt - sz[m] + 1; i<=qrcnt; i++){
qrx[i+sz[m]] = nxtR[qrx[i]], qrd[i+sz[m]] = qrd[i] + dayR[qrx[i]];
}
Rloc[++qrlim] = qrcnt + 1, qrcnt += sz[m];
}
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:64:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | scanf("%d %lld", &n, &T);
| ~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:67:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | scanf("%d", &s);
| ~~~~~^~~~~~~~~~
Main.cpp:71:37: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | for(int j=0; j<s; j++) scanf("%lld %lld", &vec[j].x, &vec[j].y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:103:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
Main.cpp:106:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | scanf("%d %d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
49 ms |
26808 KB |
Output is correct |
3 |
Correct |
125 ms |
26452 KB |
Output is correct |
4 |
Correct |
112 ms |
29008 KB |
Output is correct |
5 |
Correct |
113 ms |
26448 KB |
Output is correct |
6 |
Correct |
117 ms |
28856 KB |
Output is correct |
7 |
Correct |
118 ms |
29008 KB |
Output is correct |
8 |
Correct |
91 ms |
26800 KB |
Output is correct |
9 |
Correct |
117 ms |
29012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
49 ms |
26808 KB |
Output is correct |
3 |
Correct |
125 ms |
26452 KB |
Output is correct |
4 |
Correct |
112 ms |
29008 KB |
Output is correct |
5 |
Correct |
113 ms |
26448 KB |
Output is correct |
6 |
Correct |
117 ms |
28856 KB |
Output is correct |
7 |
Correct |
118 ms |
29008 KB |
Output is correct |
8 |
Correct |
91 ms |
26800 KB |
Output is correct |
9 |
Correct |
117 ms |
29012 KB |
Output is correct |
10 |
Correct |
2 ms |
16732 KB |
Output is correct |
11 |
Correct |
98 ms |
27888 KB |
Output is correct |
12 |
Correct |
100 ms |
27728 KB |
Output is correct |
13 |
Correct |
97 ms |
27472 KB |
Output is correct |
14 |
Correct |
94 ms |
27460 KB |
Output is correct |
15 |
Correct |
95 ms |
27576 KB |
Output is correct |
16 |
Correct |
49 ms |
27584 KB |
Output is correct |
17 |
Correct |
97 ms |
27944 KB |
Output is correct |
18 |
Correct |
95 ms |
27616 KB |
Output is correct |
19 |
Correct |
108 ms |
28188 KB |
Output is correct |
20 |
Correct |
96 ms |
27728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
49 ms |
26808 KB |
Output is correct |
3 |
Correct |
125 ms |
26452 KB |
Output is correct |
4 |
Correct |
112 ms |
29008 KB |
Output is correct |
5 |
Correct |
113 ms |
26448 KB |
Output is correct |
6 |
Correct |
117 ms |
28856 KB |
Output is correct |
7 |
Correct |
118 ms |
29008 KB |
Output is correct |
8 |
Correct |
91 ms |
26800 KB |
Output is correct |
9 |
Correct |
117 ms |
29012 KB |
Output is correct |
10 |
Execution timed out |
2065 ms |
25732 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
49 ms |
26808 KB |
Output is correct |
3 |
Correct |
125 ms |
26452 KB |
Output is correct |
4 |
Correct |
112 ms |
29008 KB |
Output is correct |
5 |
Correct |
113 ms |
26448 KB |
Output is correct |
6 |
Correct |
117 ms |
28856 KB |
Output is correct |
7 |
Correct |
118 ms |
29008 KB |
Output is correct |
8 |
Correct |
91 ms |
26800 KB |
Output is correct |
9 |
Correct |
117 ms |
29012 KB |
Output is correct |
10 |
Correct |
2 ms |
16732 KB |
Output is correct |
11 |
Correct |
98 ms |
27888 KB |
Output is correct |
12 |
Correct |
100 ms |
27728 KB |
Output is correct |
13 |
Correct |
97 ms |
27472 KB |
Output is correct |
14 |
Correct |
94 ms |
27460 KB |
Output is correct |
15 |
Correct |
95 ms |
27576 KB |
Output is correct |
16 |
Correct |
49 ms |
27584 KB |
Output is correct |
17 |
Correct |
97 ms |
27944 KB |
Output is correct |
18 |
Correct |
95 ms |
27616 KB |
Output is correct |
19 |
Correct |
108 ms |
28188 KB |
Output is correct |
20 |
Correct |
96 ms |
27728 KB |
Output is correct |
21 |
Execution timed out |
2065 ms |
25732 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16732 KB |
Output is correct |
2 |
Correct |
2 ms |
16732 KB |
Output is correct |
3 |
Correct |
106 ms |
22352 KB |
Output is correct |
4 |
Correct |
105 ms |
22340 KB |
Output is correct |
5 |
Correct |
105 ms |
22356 KB |
Output is correct |
6 |
Correct |
95 ms |
22372 KB |
Output is correct |
7 |
Correct |
97 ms |
22356 KB |
Output is correct |
8 |
Correct |
76 ms |
22352 KB |
Output is correct |
9 |
Correct |
47 ms |
22612 KB |
Output is correct |
10 |
Execution timed out |
2074 ms |
22132 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
49 ms |
26808 KB |
Output is correct |
3 |
Correct |
125 ms |
26452 KB |
Output is correct |
4 |
Correct |
112 ms |
29008 KB |
Output is correct |
5 |
Correct |
113 ms |
26448 KB |
Output is correct |
6 |
Correct |
117 ms |
28856 KB |
Output is correct |
7 |
Correct |
118 ms |
29008 KB |
Output is correct |
8 |
Correct |
91 ms |
26800 KB |
Output is correct |
9 |
Correct |
117 ms |
29012 KB |
Output is correct |
10 |
Correct |
2 ms |
16732 KB |
Output is correct |
11 |
Correct |
98 ms |
27888 KB |
Output is correct |
12 |
Correct |
100 ms |
27728 KB |
Output is correct |
13 |
Correct |
97 ms |
27472 KB |
Output is correct |
14 |
Correct |
94 ms |
27460 KB |
Output is correct |
15 |
Correct |
95 ms |
27576 KB |
Output is correct |
16 |
Correct |
49 ms |
27584 KB |
Output is correct |
17 |
Correct |
97 ms |
27944 KB |
Output is correct |
18 |
Correct |
95 ms |
27616 KB |
Output is correct |
19 |
Correct |
108 ms |
28188 KB |
Output is correct |
20 |
Correct |
96 ms |
27728 KB |
Output is correct |
21 |
Execution timed out |
2065 ms |
25732 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |