#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx=1e5+5, kx=17;
ll n, t, m[nx], q, l, r, res, cur, loc, row;
vector<vector<ll>> pa[nx], cst[nx];
vector<ll> a[nx], b[nx];
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>t;
for (int i=1; i<n; i++)
{
cin>>m[i];
pa[i].resize(m[i]);
cst[i].resize(m[i]);
a[i].resize(m[i]);
b[i].resize(m[i]);
for (int j=0; j<m[i]; j++) cin>>a[i][j]>>b[i][j], pa[i][j].resize(kx), cst[i][j].resize(kx);
}
for (int i=1; i<n-1; i++)
{
for (int j=0; j<m[i]; j++)
{
pair<ll, ll> mn={LLONG_MAX, 0};
for (int k=0; k<m[i+1]; k++)
{
if (b[i][j]<=a[i+1][k]) mn=min(mn, {b[i+1][k]-b[i][j], k});
else mn=min(mn, {t-b[i][j]+b[i+1][k], k});
}
pa[i][j][0]=mn.second;
cst[i][j][0]=mn.first;
}
}
for (int k=1; k<kx; k++)
{
for (int i=1; i<n; i++)
{
if (i+(1<<k)>n-1) continue;
for (int j=0; j<m[i]; j++)
{
pa[i][j][k]=pa[i+(1<<(k-1))][pa[i][j][k-1]][k-1];
cst[i][j][k]=cst[i][j][k-1]+cst[i+(1<<(k-1))][pa[i][j][k-1]][k-1];
}
}
}
cin>>q;
while (q--)
{
cin>>l>>r;
res=LLONG_MAX;
for (int i=0; i<m[l]; i++)
{
loc=l, row=i;
cur=b[l][i]-a[l][i];
for (int j=kx-1; j>=0; j--) if (loc+(1<<j)<r) cur+=cst[loc][row][j], row=pa[loc][row][j], loc+=(1<<j);
res=min(res, cur);
}
cout<<res<<'\n';
}
}
# | 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... |