#include <bits/stdc++.h>
#define int long long
using namespace std;
const int sz1=405, sz2=255;
vector<pair<int, int> > nxt[100005], nn[100005], pp[100005], edge[sz2][100005], vec[100005];
int ans[100005][sz1+5], mn[100005];
vector<int> good;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, t, q;
cin >> n >> t;
for (int i=1; i<n; i++)
{
int m;
cin >> m;
for (int j=0; j<m; j++)
{
int l, r;
cin >> l >> r;
vec[i].push_back({l, r});
mn[i]=min(mn[i], r-l);
}
}
//cout << "here\n";
for (int i=1; i<n; i++)
{
sort(vec[i].begin(), vec[i].end());
vector<pair<int, int> > tmp;
for (pair<int, int> x:vec[i])
{
if (tmp.empty())
tmp.push_back(x);
else if (tmp.back().first<x.first)
{
while (tmp.size() && tmp.back().second>=x.second)
tmp.pop_back();
tmp.push_back(x);
}
}
vec[i]=tmp;
}
//cout << "here\n";
for (int i=1; i<=n-2; i++)
{
int ind=0;
for (int j=0; j<vec[i].size(); j++)
{
while (ind<vec[i+1].size() && vec[i][j].second>vec[i+1][ind].first)
ind++;
if (ind==vec[i+1].size())
nn[i].push_back({0, vec[i+1][0].first+t-vec[i][j].first});
else
nn[i].push_back({ind, vec[i+1][ind].first-vec[i][j].first});
}
nxt[i]=nn[i];
}
//cout << "here\n";
for (int i=2; i<n; i++)
{
int ind=vec[i-1].size()-1;
for (int j=vec[i].size()-1; j>=0; j--)
{
while (ind>=0 && vec[i-1][ind].second>vec[i][j].first)
ind--;
//cout << j << ' ' << ind << '\n';
if (ind==-1)
pp[i].push_back({vec[i-1].size()-1, vec[i][j].first+t-vec[i-1].back().first});
else
pp[i].push_back({ind, vec[i][j].first-vec[i-1][ind].first});
}
reverse(pp[i].begin(), pp[i].end());
}
//cout << "here\n";
for (int i=1; i<=n-2; i++)
{
ans[i][1]=1e18;
for (int j=0; j<vec[i].size(); j++)
ans[i][1]=min(ans[i][1], nxt[i][j].second+vec[i+1][nxt[i][j].first].second-vec[i+1][nxt[i][j].first].first);
}
//cout << "here\n";
for (int i=2; i<=sz1; i++)
{
for (int j=1; j+i<n; j++)
{
vector<pair<int, int> > tmp;
ans[j][i]=1e18;
for (int k=0; k<vec[j].size(); k++)
{
int ind=nxt[j][k].first;
tmp.push_back({nn[j+i-1][ind].first, nxt[j][k].second+nn[j+i-1][ind].second});
ans[j][i]=min(ans[j][i], tmp[k].second+vec[j+i][tmp[k].first].second-vec[j+i][tmp[k].first].first);
}
nxt[j]=tmp;
}
}
//cout << "here\n";
int ind=-1;
while (ind+sz1<n)
{
for (int i=ind+sz1; i>ind; i--)
{
if (vec[i].size()<sz2)
{
ind=i;
good.push_back(ind);
break;
}
}
}
//cout << "here\n";
for (int i=0; i<good.size(); i++)
{
int x=good[i];
edge[i][x+1]=nn[x];
for (int j=x+2; j<n; j++)
{
for (int k=0; k<vec[x].size(); k++)
{
int ind=edge[i][j-1][k].first;
edge[i][j].push_back({nn[j-1][ind].first, edge[i][j-1][k].second+nn[j-1][ind].second});
}
}
edge[i][x-1]=pp[x];
for (int j=x-2; j; j--)
{
for (int k=0; k<vec[x].size(); k++)
{
int ind=edge[i][j+1][k].first;
edge[i][j].push_back({pp[j+1][ind].first, edge[i][j+1][k].second+pp[j+1][ind].second});
}
}
}
cin >> q;
for (int i=1; i<=q; i++)
{
int l, r;
cin >> l >> r;
r--;
if (l==r)
{
cout << mn[l] << '\n';
continue;
}
if (r-l<=sz1)
{
cout << ans[l][r-l] << '\n';
continue;
}
ind=lower_bound(good.begin(), good.end(), l+1)-good.begin();
int x=good[ind], ans=1e18;
for (int j=0; j<vec[x].size(); j++)
ans=min(ans, edge[ind][l][j].second+edge[ind][r][j].second+vec[r][edge[ind][r][j].first].second-vec[r][edge[ind][r][j].first].first);
cout << ans << '\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... |