Submission #500575

# Submission time Handle Problem Language Result Execution time Memory
500575 2021-12-31T12:27:36 Z mars4 Toll (BOI17_toll) C++17
7 / 100
381 ms 17312 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<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;
	vector<ll> elft,erht,emid;
	vector<ll> qlft,qrht,qmid;
	vector<map<ll,ll>> mm(K);
	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);
		}
	}
	vector<map<ll,ll>> mp(K);
	forn(i,(mid+1)*K,(mid+2)*K)
	{
		mp[i%K][i]=0;
	}
	for(ll i:erht)
	{
		auto [a,b,w]=edges[i];
		forn(j,0,K)
		{
			if(mp[j].count(a))
			{
				mp[j][b]=mp[j][a]+w;
			}
		}
	}
	vector<ll> rev=elft;
	reverse(all(rev));
	forn(i,mid*K,(mid+1)*K)
	{
		mp[i%K][i]=0;
	}
	for(ll i:rev)
	{
		auto [a,b,w]=edges[i];
		forn(j,0,K)
		{
			if(mp[j].count(b))
			{
				mp[j][a]=mp[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)
				{
					if(mp[j].count(a) and mm[j].count(k) and mp[k].count(b))
					{
						ans[i]=min(ans[i],mp[j][a]+mm[j][k]+mp[k][b]);
					}
				}
			}
		}
		if(a/K==mid and b/K!=mid+1)
		{
			forn(k,0,K)
			{
				if(mm[a%K].count(k) and mp[k].count(b))
				{
					ans[i]=min(ans[i],mm[a%K][k]+mp[k][b]);
				}
			}
		}
		if(a/K!=mid and b/K==mid+1)
		{
			forn(j,0,K)
			{
				if(mp[j].count(a) and mm[j].count(b%K))
				{
					ans[i]=min(ans[i],mp[j][a]+mm[j][b%K]);
				}
			}
		}
		if(a/K==mid and b/K==mid+1)
		{
			if(mm[a%K].count(b%K))
			{
				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);
	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:176:5: warning: unused variable 'z' [-Wunused-variable]
  176 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |     ^
toll.cpp:176:11: warning: unused variable 't' [-Wunused-variable]
  176 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |           ^
toll.cpp:176:13: warning: unused variable 'k' [-Wunused-variable]
  176 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |             ^
toll.cpp:176:15: warning: unused variable 'i' [-Wunused-variable]
  176 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |               ^
toll.cpp:176:17: warning: unused variable 'j' [-Wunused-variable]
  176 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                 ^
toll.cpp:176:19: warning: unused variable 'l' [-Wunused-variable]
  176 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                   ^
toll.cpp:176:21: warning: unused variable 'd' [-Wunused-variable]
  176 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                     ^
toll.cpp:176:23: warning: unused variable 'h' [-Wunused-variable]
  176 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                       ^
toll.cpp:176:25: warning: unused variable 'r' [-Wunused-variable]
  176 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                         ^
# Verdict Execution time Memory Grader output
1 Correct 147 ms 10740 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 308 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 456 KB Output is correct
8 Correct 93 ms 6268 KB Output is correct
9 Correct 59 ms 5296 KB Output is correct
10 Correct 11 ms 1100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 381 ms 17312 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 147 ms 10740 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 308 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 456 KB Output is correct
8 Correct 93 ms 6268 KB Output is correct
9 Correct 59 ms 5296 KB Output is correct
10 Correct 11 ms 1100 KB Output is correct
11 Incorrect 381 ms 17312 KB Output isn't correct
12 Halted 0 ms 0 KB -