Submission #924511

#TimeUsernameProblemLanguageResultExecution timeMemory
924511Muhammad_AneeqToll (BOI17_toll)C++17
0 / 100
473 ms29852 KiB
/* بسم الله الرحمن الرحيم 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++) if (seg[lc][j][l]!=0&&seg[rc][l][m]!=0) { if (seg[i][j][m]==0) seg[i][j][m]=1e9+10; 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,0)); 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,0)); if (lc==g) return rc; if (rc==g) return lc; for (int j=0;j<K;j++) { for (int l=0;l<K;l++) { for (int m=0;m<K;m++) { if (lc[j][l]!=0&&rc[l][m]!=0) { if (g[j][m]==0) g[j][m]=1e9+10; 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]=0; 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-(z==0))<<endl; } } int main() { ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 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...