답안 #712287

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
712287 2023-03-18T14:07:19 Z mars4 Toll (BOI17_toll) C++17
7 / 100
254 ms 13852 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 i128            __int128_t
#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 T> ostream& operator<<(ostream& out,ordered_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<vector<vector<ll>>> a;
vector<vector<vector<ll>>> lft;
vector<vector<vector<ll>>> rht;
vector<pair<ll,ll>> query;
vector<ll> ans;
ll K;

vector<vector<ll>> combine(vector<vector<ll>> a,vector<vector<ll>> b)
{
	vector<vector<ll>> res(K,vector<ll>(K,inf));
	forn(i,0,K)
	{
		forn(j,0,K)
		{
			forn(k,0,K)
			{
				res[i][k]=min(res[i][k],a[i][j]+b[j][k]);
			}
		}
	}
	return res;
}

void calc(ll st,ll ed,vector<ll> ind)
{
	if(st==ed or ind.empty())
	{
		return;
	}
	if(ed==st+1)
	{
		for(ll i:ind)
		{
			ans[i]=a[st][query[i].ff%K][query[i].ss%K];
		}
		return;
	}
	ll mid=(st+ed)>>1;
	lft[mid]=a[mid];
	rht[mid+1]=a[mid+1];
	forb(i,mid-1,st)
	{
		lft[i]=combine(a[i],lft[i+1]);
	}
	forn(i,mid+2,ed)
	{
		rht[i]=combine(rht[i-1],a[i]);
	}
	vector<vector<ll>> nind(2);
	for(ll i:ind)
	{
		auto [L,R]=query[i];
		ll l=L/K;
		ll r=R/K;
		if(l<=mid and r>mid+1)
		{
			ans[i]=combine(lft[l],rht[r-1])[L%K][R%K];
		}
		else if(l<=mid and r==mid+1)
		{
			ans[i]=lft[l][L%K][R%K];
		}
		else
		{
			nind[l>mid].push_back(i);
		}
	}
	calc(st,mid,nind[0]);
	calc(mid+1,ed,nind[1]);
}

int main()
{
	fastio();
	ll z,n,m,t,k,i,j,l,d,h,r;
	ll q;
	cin>>K>>n>>m>>q;
	ll N=(n+K-1)/K;
	a=vector<vector<vector<ll>>>(N,vector<vector<ll>>(K,vector<ll>(K,inf)));
	lft=vector<vector<vector<ll>>>(N,vector<vector<ll>>(K,vector<ll>(K,inf)));
	rht=vector<vector<vector<ll>>>(N,vector<vector<ll>>(K,vector<ll>(K,inf)));
	query=vector<pair<ll,ll>>(q);
	ans=vector<ll>(q,inf);
	forn(i,0,m)
	{
		cin>>l>>r>>d;
		a[l/K][l%K][r%K]=d;
	}
	for(auto &[l,r]:query)
	{
		cin>>l>>r;
	}
	vector<ll> ind(q);
	iota(all(ind),(ll)0);
	calc(0,N-1,ind);
	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:120:5: warning: unused variable 'z' [-Wunused-variable]
  120 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |     ^
toll.cpp:120:11: warning: unused variable 't' [-Wunused-variable]
  120 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |           ^
toll.cpp:120:13: warning: unused variable 'k' [-Wunused-variable]
  120 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |             ^
toll.cpp:120:15: warning: unused variable 'i' [-Wunused-variable]
  120 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |               ^
toll.cpp:120:17: warning: unused variable 'j' [-Wunused-variable]
  120 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                 ^
toll.cpp:120:23: warning: unused variable 'h' [-Wunused-variable]
  120 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 141 ms 13852 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 254 ms 13688 KB Output is correct
9 Correct 94 ms 13760 KB Output is correct
10 Correct 76 ms 13752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 12092 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 141 ms 13852 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 254 ms 13688 KB Output is correct
9 Correct 94 ms 13760 KB Output is correct
10 Correct 76 ms 13752 KB Output is correct
11 Correct 62 ms 12092 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Incorrect 0 ms 212 KB Output isn't correct
14 Halted 0 ms 0 KB -