This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
بسم الله الرحمن الرحيم
Author:
(:Muhammad Aneeq:)
*/
#include <iostream>
#include <vector>
using namespace std;
int const N=5e4+10,K=6;
int seg[4*N][K][K]={};
void update(int i,int r,int mp,int mq,int st,int en,int val)
{
if (st==en)
{
seg[i][mp][mq]=val;
return;
}
int mid=(st+en)/2;
if (r<=mid)
update(i*2,r,mp,mq,st,mid,val);
else
update(i*2+1,r,mp,mq,mid+1,en,val);
int lc=i*2,rc=i*2+1;
for (int j=0;j<K;j++)
for (int l=0;l<K;l++)
for (int m=0;m<K;m++)
seg[i][j][m]=min(seg[lc][j][l]+seg[rc][l][m],seg[i][j][m]);
}
vector<vector<int>> get(int i,int l,int r,int st,int en)
{
if (st>=l&&en<=r)
{
vector<vector<int>>g;
for (int j=0;j<K;j++)
{
g.push_back({});
for (int l=0;l<K;l++)
g.back().push_back(seg[i][j][l]);
}
return g;
}
if (st>r||en<l)
{
vector<vector<int>>g(K,vector<int>(K,1e9+10));
return g;
}
int mid=(st+en)/2;
vector<vector<int>>lc=get(i*2,l,r,st,mid),rc=get(i*2+1,l,r,mid+1,en);
vector<vector<int>>g(K,vector<int>(K,1e9+10));
if (r<=mid)
return lc;
if (l>mid)
return rc;
for (int j=0;j<K;j++)
for (int l=0;l<K;l++)
for (int m=0;m<K;m++)
g[j][m]=min(lc[j][l]+rc[l][m],g[j][m]);
return g;
}
inline void solve()
{
int k,n,m,o;
cin>>k>>n>>m>>o;
for (int i=0;i<4*N;i++)
for (int j=0;j<K;j++)
for (int l=0;l<K;l++)
seg[i][j][l]=1e9+10;
for (int i=0;i<m;i++)
{
int a,b,w;
cin>>a>>b>>w;
update(1,a/k,a%k,b%k,0,n/k-1,w);
}
while (o--)
{
int a,b;
cin>>a>>b;
int z=get(1,a/k,b/k-1,0,n/k-1)[a%k][b%k];
cout<<(z==1e9+10?-1:z)<<endl;
}
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
solve();
}
# | 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... |