# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
880459 | mkolko | Railway Trip 2 (JOI22_ho_t4) | C++14 | 2072 ms | 25180 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
using namespace std;
const int base=(1<<21);
int tree[base*2+7];
int dp[base][30][2];
int main()
{
int n, m, k, q, p, p2, str, odp;
cin>>n>>k;
for(int a=0; a<=21; a++)
{
for(int i=0; i<=n; i++){
dp[i][a][0]=i;
dp[i][a][1]=i;
}
}
cin>>m;
for(int a=0; a<m; a++)
{
cin>>p>>p2;
if(p<=p2){
for(int i=0; i<p2-p+1 && i<k; i++)
dp[p+i][0][1]=max(dp[p+i][0][1], p2);
}
else{
for(int i=0; i<p-p2+1 && i<k; i++)
dp[p-i][0][0]=min(dp[p-i][0][0], p2);
}
}
for(int a=1; a<=20; a++)
{
for(int i=1; i<=n; i++)
dp[i][a][1]=max(max(dp[dp[i][a-1][1]][a-1][1], dp[dp[i][a-1][0]][a-1][1]), dp[i][a-1][1]);
for(int i=1; i<=n; i++)
dp[i][a][0]=min(min(dp[dp[i][a-1][0]][a-1][0], dp[dp[i][a-1][1]][a-1][0]), dp[i][a-1][0]);
}
/*for(int a=0; a<=n; a++)
{
for(int i=0; i<=n; i++)
cout<<dp[a][i][1]<<" ";
cout<<"\n";
}
cout<<"\n\n";
for(int a=0; a<=n; a++)
{
for(int i=0; i<=n; i++)
cout<<dp[a][i][0]<<" ";
cout<<"\n";
}*/
cin>>q;
for(int a=0; a<q; a++)
{
cin>>p>>p2;
if(p<p2)
{
odp=0;
for(int i=20; i>=0; i--){
if(dp[p][i][1]<p2){
odp=odp+(1<<(i));
p=dp[p][i][1];
}
}
}
else
{
odp=0;
for(int i=20; i>=0; i--){
if(dp[p][i][0]>p2){
odp=odp+(1<<(i));
p=dp[p][i][0];
}
}
}
if(p!=p2)
odp++;
if(odp>=(1<<21))
cout<<-1<<"\n";
else
cout<<odp<<" "<<"\n";
}
}
Compilation message (stderr)
# | 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... |