Submission #896354

#TimeUsernameProblemLanguageResultExecution timeMemory
896354pccToll (BOI17_toll)C++14
100 / 100
133 ms248612 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define pll pair<ll,ll> #define pii pair<int,int> #define fs first #define sc second #define tlll tuple<ll,ll,ll> const int mxc = 5; const int mxn = 5e5+10; const int inf = 1e9+10; struct mat{ int val[mxc][mxc]; mat(){ for(auto &i:val)for(auto &j:i)j = inf; } int* operator[](int k){ return (int*)val[k]; } mat operator*(mat b)const{ mat re = mat(); for(int i = 0;i<mxc;i++)for(int j = 0;j<mxc;j++)for(int k = 0;k<mxc;k++){ re[j][k] = min(re[j][k],val[j][i]+b[i][k]); } return re; } }; int N,K,Q,M,blk; mat segtree[mxn*4]; mat paths[mxn]; void build(int now,int l,int r){ if(l == r){ segtree[now] = paths[l]; return; } int mid = (l+r)>>1; build(now*2+1,l,mid); build(now*2+2,mid+1,r); segtree[now] = segtree[now*2+1]*segtree[now*2+2]; return; } mat getval(int now,int l,int r,int s,int e){ if(l>=s&&e>=r)return segtree[now]; int mid = (l+r)>>1; if(mid>=e)return getval(now*2+1,l,mid,s,e); else if(mid<s)return getval(now*2+2,mid+1,r,s,e); else return getval(now*2+1,l,mid,s,e)*getval(now*2+2,mid+1,r,s,e); } main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>K>>N>>M>>Q; int blk = (N+K-1)/K; for(int i = 0;i<M;i++){ int a,b,c; cin>>a>>b>>c; paths[a/K][a%K][b%K] = min(paths[a/K][a%K][b%K],c); } build(0,0,blk); while(Q--){ int a,b; cin>>a>>b; if(a/K>=b/K){ cout<<"-1\n"; continue; } auto re = getval(0,0,blk,a/K,b/K-1); if(re[a%K][b%K]>=inf)cout<<"-1\n"; else cout<<re[a%K][b%K]<<'\n'; } return 0; }

Compilation message (stderr)

toll.cpp:56:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   56 | main(){
      | ^~~~
#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...