/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
const ll INF = 1e18;
int n;
ll t, ANS[N];
vector<array<ll, 3>> go[N];
array<ll, 2> to[N];
vector<pair<int, ll>> g[N], Q[N];
void solve(){
cin >> n >> t;
int id = 1;
for(int i = 1; i < n; ++i){
int m; cin >> m;
for(int j = 0; j < m; ++j){
int l, r; cin >> l >> r;
go[i].pb({l, r, id++});
}
sort(all(go[i]));
vector<array<ll, 3>> v;
for(int j = m-1; j >= 0; --j){
if(v.empty()) v.pb(go[i][j]);
else{
if(v.back()[1] > go[i][j][1]){
v.pb(go[i][j]);
}
}
}
reverse(all(v));
v.swap(go[i]);
}
for(int i = 1; i + 1 < n; ++i){
for(auto [l, r, ID]: go[i]){
ll w = 0;
int pos = lower_bound(all(go[i + 1]), array<ll, 3>{r, -1, -1}) - go[i + 1].begin();
if(pos == go[i + 1].size()) pos = 0, w = go[i + 1][pos][1] + t - r;
else w = go[i + 1][pos][1] - r;
to[ID] = {w, go[i + 1][pos][2]};
g[to[ID][1]].pb({ID, w});
}
}
int q; cin >> q;
for(int i = 1; i <= q; ++i){
int l, r; cin >> l >> r;
Q[l].pb({r, i});
}
const int C = 1;
vector<ll> res(n + 1);
vector<ll> dist(N);
for(int i = 1; i < n; ++i){
if(go[i].size() >= C){
ll mn = INF;
for(auto [l, r, ID]: go[i]){
dist[ID] = r - l;
mn = min(mn, dist[ID]);
}
res[i + 1] = mn;
for(int j = i + 1; j < n; ++j){
mn = INF;
for(auto [l, r, ID]: go[j]){
dist[ID] = INF;
for(auto [u, w]: g[ID]){
dist[ID] = min(dist[ID], dist[u] + w);
}
mn = min(mn, dist[ID]);
}
res[j + 1] = mn;
}
for(auto [l, idx]: Q[i]){
ANS[idx] = res[l];
}
}
}
for(int i = 1; i <= q; ++i){
cout << ANS[i] << '\n';
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
# | 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... |