# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
500578 | mars4 | Toll (BOI17_toll) | C++17 | 145 ms | 16752 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 <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ff first
#define ss second
#define ll int64_t
#define ld long double
#define nl cout<<"\n"
#define all(v) v.begin(),v.end()
#define mset(a,v) memset((a),(v),sizeof(a))
#define forn(i,a,b) for(int64_t i=int64_t(a);i<int64_t(b);++i)
#define forb(i,a,b) for(int64_t i=int64_t(a);i>=int64_t(b);--i)
#define fastio() ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mod 1'000'000'007
#define mod2 998'244'353
#define inf 1'000'000'000'000'007
#define pi 3.14159265358979323846
template<class key,class cmp=std::less<key>>
using ordered_set=tree<key,null_type,cmp,rb_tree_tag,tree_order_statistics_node_update>;
template<class L,class R> ostream& operator<<(ostream& out,pair<L,R> &p) {return out<<"("<<p.ff<<", "<<p.ss<<")";}
template<class T> ostream& operator<<(ostream& out,vector<T> &v) {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,deque<T> &v) {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,set<T> &s) {out<<"{";for(auto it=s.begin();it!=s.end();++it){if(it!=s.begin())out<<", ";out<<*it;}return out<<"}";}
template<class L,class R> ostream& operator<<(ostream& out,map<L,R> &m) {out<<"{";for(auto it=m.begin();it!=m.end();++it){if(it!=m.begin())out<<", ";out<<*it;}return out<<"}";}
void dbg_out() {cerr<<"]\n";}
template<typename Head,typename... Tail>
void dbg_out(Head H,Tail... T) {cerr<<H;if(sizeof...(Tail))cerr<<", ";dbg_out(T...);}
#ifdef LOCAL
#define dbg(...) cerr<<"["<<#__VA_ARGS__<<"] = [",dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
//---------------------------------mars4---------------------------------
vector<tuple<ll,ll,ll>> edges;
vector<pair<ll,ll>> queries;
vector<vector<ll>> dist;
vector<ll> ans;
ll K;
void calc(ll l,ll r,vector<ll> ei,vector<ll> qi)
{
if(l>=r)
{
return;
}
ll mid=(l+r)/2;
forn(i,l*K,(r+1)*K)
{
forn(j,0,K)
{
dist[j][i]=inf;
}
}
vector<ll> elft,erht,emid;
vector<ll> qlft,qrht,qmid;
vector<vector<ll>> mm(K,vector<ll>(K,inf));
for(ll i:ei)
{
auto [a,b,w]=edges[i];
if(b/K<=mid)
{
elft.push_back(i);
}
else if(a/K>mid)
{
erht.push_back(i);
}
else
{
emid.push_back(i);
mm[a%K][b%K]=w;
}
}
for(ll i:qi)
{
auto [a,b]=queries[i];
if(b/K<=mid)
{
qlft.push_back(i);
}
else if(a/K>mid)
{
qrht.push_back(i);
}
else
{
qmid.push_back(i);
}
}
forn(i,(mid+1)*K,(mid+2)*K)
{
dist[i%K][i]=0;
}
for(ll i:erht)
{
auto [a,b,w]=edges[i];
forn(j,0,K)
{
dist[j][b]=min(dist[j][b],dist[j][a]+w);
}
}
vector<ll> rev=elft;
reverse(all(rev));
forn(i,mid*K,(mid+1)*K)
{
dist[i%K][i]=0;
}
for(ll i:rev)
{
auto [a,b,w]=edges[i];
forn(j,0,K)
{
dist[j][a]=min(dist[j][a],dist[j][b]+w);
}
}
for(ll i:qmid)
{
auto [a,b]=queries[i];
if(a/K!=mid and b/K!=mid+1)
{
forn(j,0,K)
{
forn(k,0,K)
{
ans[i]=min(ans[i],dist[j][a]+mm[j][k]+dist[k][b]);
}
}
}
if(a/K==mid and b/K!=mid+1)
{
forn(k,0,K)
{
ans[i]=min(ans[i],mm[a%K][k]+dist[k][b]);
}
}
if(a/K!=mid and b/K==mid+1)
{
forn(j,0,K)
{
ans[i]=min(ans[i],dist[j][a]+mm[j][b%K]);
}
}
if(a/K==mid and b/K==mid+1)
{
ans[i]=min(ans[i],mm[a%K][b%K]);
}
}
calc(l,mid,elft,qlft);
calc(mid+1,r,erht,qrht);
}
int main()
{
fastio();
ll z,n,m,t,k,i,j,l,d,h,r;
ll q;
cin>>K>>n>>m>>q;
edges=vector<tuple<ll,ll,ll>>(m);
queries=vector<pair<ll,ll>>(q);
ans=vector<ll>(q,inf);
dist=vector<vector<ll>>(K,vector<ll>(n+2*K,inf));
for(auto &[l,r,w]:edges)
{
cin>>l>>r>>w;
}
for(auto &[l,r]:queries)
{
cin>>l>>r;
}
sort(all(edges));
vector<ll> ei(m);
vector<ll> qi(q);
iota(all(ei),0ll);
iota(all(qi),0ll);
calc(0,(n+K-1)/K,ei,qi);
for(ll i:ans)
{
if(i==inf)
{
cout<<"-1\n";
}
else
{
cout<<i<<"\n";
}
}
cerr<<"\nTime elapsed: "<<1000*clock()/CLOCKS_PER_SEC<<"ms\n";
return 0;
}
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... |