#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define sz(v) (int)v.size()
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define compact(v) v.erase(unique(ALL(v)), end(v))
#define FOR(i, a, b) for(int i = (a); i <= (int)(b); i++)
#define FORD(i, a, b) for(int i = (a); i >= (int)(b); i--)
#define BIT(mask,i) ((mask>>(i))&1)
#define MASK(a) (1LL << (a))
#define lwb lower_bound
#define upb upper_bound
#define dbg(x) "[" #x " = " << (x) << "]"
#define file(task) if(fopen(task".inp", "r")){ freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); }
template<typename T>
bool minimize(T& a, const T& b){
if(a > b) return a = b, true;
return false;
}
template<typename T>
bool maximize(T& a, const T& b){
if(a < b) return a = b, true;
return false;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using db = double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vb = vector<bool>;
using vll = vector<ll>;
using vpi = vector<pii>;
using vpl = vector<pll>;
const int MAX = 2e5 + 5;
const int LO = 20;
int N, Q, T;
vpi flight[MAX];
int L[MAX], R[MAX];
pii arr_flight[MAX];
int lift[MAX][LO];
ll cost[MAX][LO];
vpi compress(vpi fli){
// we not use : aj ... ai .... bi ... bj
vpi res;
sort(ALL(fli), [&](pii u, pii v){
if(u.fi == v.fi) return u.se > v.se;
return u.fi < v.fi;
});
for(auto x : fli){
while(!res.empty() && res.back().second >= x.se) res.pop_back();
res.push_back(x);
}
return res;
}
ll query(int i, int cnt){
ll sum = 0;
FORD(s, 18, 0) if(cnt >= (1 << s)){
cnt-= (1 << s);
sum+= cost[i][s];
i = lift[i][s];
}
return sum + arr_flight[i].se - arr_flight[i].fi;
}
void solve(){
cin >> N >> T;
int cnt = 0;
FOR(i, 0, N - 2){
int m; cin >> m;
flight[i].resize(m);
FOR(j, 0, m - 1) cin >> flight[i][j].fi >> flight[i][j].se;
flight[i] = compress(flight[i]);
L[i] = cnt;
R[i] = L[i] + flight[i].size() - 1;
cnt = R[i] + 1;
FOR(j, 0, sz(flight[i]) - 1) arr_flight[L[i] + j] = flight[i][j];
}
L[N - 1] = cnt;
R[N - 1] = cnt + 1;
FOR(i, 0, sz(flight[N - 2]) - 1){
lift[L[N - 2] + i][0] = cnt;
cost[L[N - 2] + i][0] = flight[N - 2][i].se - flight[N - 2][i].fi;
}
FOR(i, 0, N - 3){
int ptr = 0;
FOR(j, 0, sz(flight[i]) - 1){
while(ptr < sz(flight[i + 1]) && flight[i + 1][ptr].fi < flight[i][j].se) ptr++;
if(ptr == sz(flight[i + 1])){
lift[L[i] + j][0] = L[i + 1];
cost[L[i] + j][0] = T - flight[i][j].fi + flight[i + 1][0].fi;
} else{
lift[L[i] + j][0] = L[i + 1] + ptr;
cost[L[i] + j][0] = flight[i + 1][ptr].fi - flight[i][j].fi;
}
}
}
for(int j = 1; (1 << j) <= cnt; j++){
FOR(i, 0, cnt){
lift[i][j] = lift[lift[i][j - 1]][j - 1];
cost[i][j] = cost[i][j - 1] + cost[lift[i][j - 1]][j - 1];
}
}
cin >> Q;
while(Q--){
int l, r; cin >> l >> r; l--; r--;
ll ans = 1e18;
FOR(i, L[l], R[l]) minimize(ans, query(i, r - l - 1));
cout << ans << "\n";
}
}
int main(void){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
file("joi24_escape2");
int tc = 1; // cin >> tc;
while (tc--) solve();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:22:55: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | #define file(task) if(fopen(task".inp", "r")){ freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:141:5: note: in expansion of macro 'file'
141 | file("joi24_escape2");
| ^~~~
Main.cpp:22:88: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | #define file(task) if(fopen(task".inp", "r")){ freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:141:5: note: in expansion of macro 'file'
141 | file("joi24_escape2");
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |