답안 #500578

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
500578 2021-12-31T12:40:25 Z mars4 Toll (BOI17_toll) C++17
100 / 100
145 ms 16752 KB
#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

toll.cpp: In function 'int main()':
toll.cpp:165:5: warning: unused variable 'z' [-Wunused-variable]
  165 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |     ^
toll.cpp:165:11: warning: unused variable 't' [-Wunused-variable]
  165 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |           ^
toll.cpp:165:13: warning: unused variable 'k' [-Wunused-variable]
  165 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |             ^
toll.cpp:165:15: warning: unused variable 'i' [-Wunused-variable]
  165 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |               ^
toll.cpp:165:17: warning: unused variable 'j' [-Wunused-variable]
  165 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                 ^
toll.cpp:165:19: warning: unused variable 'l' [-Wunused-variable]
  165 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                   ^
toll.cpp:165:21: warning: unused variable 'd' [-Wunused-variable]
  165 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                     ^
toll.cpp:165:23: warning: unused variable 'h' [-Wunused-variable]
  165 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                       ^
toll.cpp:165:25: warning: unused variable 'r' [-Wunused-variable]
  165 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 4884 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 44 ms 4936 KB Output is correct
9 Correct 43 ms 4796 KB Output is correct
10 Correct 11 ms 1228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 8728 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 3 ms 1100 KB Output is correct
8 Correct 3 ms 1228 KB Output is correct
9 Correct 45 ms 5024 KB Output is correct
10 Correct 100 ms 11940 KB Output is correct
11 Correct 74 ms 8940 KB Output is correct
12 Correct 62 ms 7660 KB Output is correct
13 Correct 110 ms 12560 KB Output is correct
14 Correct 69 ms 7696 KB Output is correct
15 Correct 58 ms 7124 KB Output is correct
16 Correct 58 ms 7168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 3 ms 716 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 43 ms 4196 KB Output is correct
11 Correct 72 ms 7988 KB Output is correct
12 Correct 98 ms 11420 KB Output is correct
13 Correct 115 ms 12368 KB Output is correct
14 Correct 83 ms 9524 KB Output is correct
15 Correct 58 ms 7068 KB Output is correct
16 Correct 58 ms 6940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 3 ms 716 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 43 ms 4196 KB Output is correct
11 Correct 72 ms 7988 KB Output is correct
12 Correct 98 ms 11420 KB Output is correct
13 Correct 115 ms 12368 KB Output is correct
14 Correct 83 ms 9524 KB Output is correct
15 Correct 58 ms 7068 KB Output is correct
16 Correct 58 ms 6940 KB Output is correct
17 Correct 71 ms 8188 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 2 ms 588 KB Output is correct
24 Correct 2 ms 588 KB Output is correct
25 Correct 3 ms 844 KB Output is correct
26 Correct 3 ms 716 KB Output is correct
27 Correct 43 ms 4460 KB Output is correct
28 Correct 101 ms 11512 KB Output is correct
29 Correct 108 ms 12360 KB Output is correct
30 Correct 86 ms 9780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 4884 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 44 ms 4936 KB Output is correct
9 Correct 43 ms 4796 KB Output is correct
10 Correct 11 ms 1228 KB Output is correct
11 Correct 72 ms 8728 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 3 ms 1100 KB Output is correct
18 Correct 3 ms 1228 KB Output is correct
19 Correct 45 ms 5024 KB Output is correct
20 Correct 100 ms 11940 KB Output is correct
21 Correct 74 ms 8940 KB Output is correct
22 Correct 62 ms 7660 KB Output is correct
23 Correct 110 ms 12560 KB Output is correct
24 Correct 69 ms 7696 KB Output is correct
25 Correct 58 ms 7124 KB Output is correct
26 Correct 58 ms 7168 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 460 KB Output is correct
34 Correct 3 ms 716 KB Output is correct
35 Correct 2 ms 588 KB Output is correct
36 Correct 43 ms 4196 KB Output is correct
37 Correct 72 ms 7988 KB Output is correct
38 Correct 98 ms 11420 KB Output is correct
39 Correct 115 ms 12368 KB Output is correct
40 Correct 83 ms 9524 KB Output is correct
41 Correct 58 ms 7068 KB Output is correct
42 Correct 58 ms 6940 KB Output is correct
43 Correct 71 ms 8188 KB Output is correct
44 Correct 0 ms 204 KB Output is correct
45 Correct 0 ms 204 KB Output is correct
46 Correct 0 ms 204 KB Output is correct
47 Correct 0 ms 204 KB Output is correct
48 Correct 0 ms 204 KB Output is correct
49 Correct 2 ms 588 KB Output is correct
50 Correct 2 ms 588 KB Output is correct
51 Correct 3 ms 844 KB Output is correct
52 Correct 3 ms 716 KB Output is correct
53 Correct 43 ms 4460 KB Output is correct
54 Correct 101 ms 11512 KB Output is correct
55 Correct 108 ms 12360 KB Output is correct
56 Correct 86 ms 9780 KB Output is correct
57 Correct 145 ms 16752 KB Output is correct
58 Correct 47 ms 4896 KB Output is correct
59 Correct 74 ms 8944 KB Output is correct