Submission #988472

#TimeUsernameProblemLanguageResultExecution timeMemory
988472amirhoseinfar1385Escape Route 2 (JOI24_escape2)C++17
100 / 100
1643 ms310400 KiB
#include<bits/stdc++.h> using namespace std; #pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2") const int maxn=100000+10,maxq=300000+10,lg=20,sq=100,msq=maxn/sq+10; int t,n,q; long long inf=1e16; vector<pair<int,int>>all[maxn]; vector<int>dp[maxn],lnk[lg][maxn],allp; vector<long long>wlnk[lg][maxn]; long long res[msq][maxn]; vector<long long>allz[maxn]; void vorod(){ cin>>n>>t; for(int i=1;i<n;i++){ int d; cin>>d; all[i].resize(d); if(d>=sq){ allp.push_back(i); } for(int j=0;j<d;j++){ cin>>all[i][j].first>>all[i][j].second; } sort(all[i].begin(),all[i].end()); dp[i].resize(d); for(int j=0;j<lg;j++){ lnk[j][i].resize(d); wlnk[j][i].resize(d); } allz[i].resize(d); } cin>>q; } void callnk(){ for(int i=1;i<lg;i++){ for(int j=1;j<n-1;j++){ if((1<<i)+j>n){ continue; } for(int h=0;h<(int)all[j].size();h++){ lnk[i][j][h]=lnk[i-1][j+(1<<(i-1))][lnk[i-1][j][h]]; wlnk[i][j][h]=wlnk[i-1][j][h]+wlnk[i-1][j+(1<<(i-1))][lnk[i-1][j][h]]; } } } } void calresp(int z){ for(int i=1;i<n-1;i++){ for(int j=0;j<(int)all[i].size();j++){ //cout<<i<<" "<<j<<" "<<z<<" "<<allz[i][j]<<" "<<((all[i+1][lnk[0][i][j]].first-all[i][j].first)+t)%t<<"\n"; allz[i+1][lnk[0][i][j]]=min(allz[i+1][lnk[0][i][j]],allz[i][j]+wlnk[0][i][j]); res[z][i+1]=min(res[z][i+1],allz[i][j]+dp[i][j]-all[i][j].first); } } int i=n-1; for(int j=0;j<(int)all[n-1].size();j++){ //cout<<z<<" "<<i<<" "<<j<<" "<<allz[i][j]<<" "<<dp[i][j]<<" wtf"<<endl; res[z][i+1]=min(res[z][i+1],allz[i][j]+dp[i][j]-all[i][j].first); } } void calp(){ int ted=0; for(int i=1;i<n;i++){ if((int)all[i].size()<sq){ continue; } for(int j=1;j<=n;j++){ for(int h=0;h<(int)all[j].size();h++){ allz[j][h]=inf; } res[ted][j]=inf; } for(int j=0;j<(int)all[i].size();j++){ allz[i][j]=0; } res[ted][i]=0; calresp(ted); ted++; } } void pre(){ for(int i=n-1;i>=1;i--){ int mn=t; for(int j=(int)all[i].size()-1;j>=0;j--){ mn=min(all[i][j].second,mn); dp[i][j]=mn; } } for(int i=n-2;i>=1;i--){ for(int j=0;j<(int)all[i].size();j++){ if(all[i+1].back().first<dp[i][j]){ lnk[0][i][j]=0; wlnk[0][i][j]=t-all[i][j].first+all[i+1][0].first; continue; } int p=lower_bound(all[i+1].begin(),all[i+1].end(),make_pair(dp[i][j],-1))-all[i+1].begin(); lnk[0][i][j]=p; wlnk[0][i][j]=all[i+1][p].first-all[i][j].first; } } calp(); callnk(); } long long boro(int ind,int r,int p){ long long ret=0; if(ind==r){ return 0; } int dis=r-ind-1; for(int i=lg-1;i>=0;i--){ if((dis>>i)&1){ ret+=wlnk[i][ind][p]; p=lnk[i][ind][p]; ind+=(1<<i); } } ret+=dp[ind][p]-all[ind][p].first; return ret; } void solve(){ for(int i=0;i<q;i++){ int l,r; cin>>l>>r; if((int)all[l].size()>=sq){ int p=lower_bound(allp.begin(),allp.end(),l)-allp.begin(); cout<<res[p][r]<<"\n"; continue; } long long ret=inf; for(int i=0;i<(int)all[l].size();i++){ ret=min(ret,boro(l,r,i)); } cout<<ret<<"\n"; } } int main(){ ios::sync_with_stdio(0); cin.tie(0); vorod(); pre(); solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...