답안 #712289

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
712289 2023-03-18T14:10:00 Z mars4 Toll (BOI17_toll) C++17
100 / 100
152 ms 16176 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;
	}
	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:112:5: warning: unused variable 'z' [-Wunused-variable]
  112 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |     ^
toll.cpp:112:11: warning: unused variable 't' [-Wunused-variable]
  112 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |           ^
toll.cpp:112:13: warning: unused variable 'k' [-Wunused-variable]
  112 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |             ^
toll.cpp:112:15: warning: unused variable 'i' [-Wunused-variable]
  112 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |               ^
toll.cpp:112:17: warning: unused variable 'j' [-Wunused-variable]
  112 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                 ^
toll.cpp:112:23: warning: unused variable 'h' [-Wunused-variable]
  112 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 13772 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 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 96 ms 13740 KB Output is correct
9 Correct 88 ms 13772 KB Output is correct
10 Correct 75 ms 13772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 11980 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 5 ms 1236 KB Output is correct
8 Correct 6 ms 1264 KB Output is correct
9 Correct 53 ms 14636 KB Output is correct
10 Correct 67 ms 13000 KB Output is correct
11 Correct 61 ms 13752 KB Output is correct
12 Correct 52 ms 11884 KB Output is correct
13 Correct 65 ms 9716 KB Output is correct
14 Correct 39 ms 7756 KB Output is correct
15 Correct 39 ms 8588 KB Output is correct
16 Correct 37 ms 8576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 288 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 592 KB Output is correct
9 Correct 4 ms 616 KB Output is correct
10 Correct 60 ms 14040 KB Output is correct
11 Correct 70 ms 12996 KB Output is correct
12 Correct 76 ms 12280 KB Output is correct
13 Correct 79 ms 12524 KB Output is correct
14 Correct 75 ms 11916 KB Output is correct
15 Correct 45 ms 8380 KB Output is correct
16 Correct 47 ms 8376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 288 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 592 KB Output is correct
9 Correct 4 ms 616 KB Output is correct
10 Correct 60 ms 14040 KB Output is correct
11 Correct 70 ms 12996 KB Output is correct
12 Correct 76 ms 12280 KB Output is correct
13 Correct 79 ms 12524 KB Output is correct
14 Correct 75 ms 11916 KB Output is correct
15 Correct 45 ms 8380 KB Output is correct
16 Correct 47 ms 8376 KB Output is correct
17 Correct 83 ms 13192 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 320 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 4 ms 692 KB Output is correct
24 Correct 5 ms 724 KB Output is correct
25 Correct 5 ms 724 KB Output is correct
26 Correct 4 ms 720 KB Output is correct
27 Correct 87 ms 14140 KB Output is correct
28 Correct 99 ms 12472 KB Output is correct
29 Correct 99 ms 12728 KB Output is correct
30 Correct 92 ms 12084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 13772 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 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 96 ms 13740 KB Output is correct
9 Correct 88 ms 13772 KB Output is correct
10 Correct 75 ms 13772 KB Output is correct
11 Correct 57 ms 11980 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 5 ms 1236 KB Output is correct
18 Correct 6 ms 1264 KB Output is correct
19 Correct 53 ms 14636 KB Output is correct
20 Correct 67 ms 13000 KB Output is correct
21 Correct 61 ms 13752 KB Output is correct
22 Correct 52 ms 11884 KB Output is correct
23 Correct 65 ms 9716 KB Output is correct
24 Correct 39 ms 7756 KB Output is correct
25 Correct 39 ms 8588 KB Output is correct
26 Correct 37 ms 8576 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 288 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 2 ms 596 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 2 ms 592 KB Output is correct
35 Correct 4 ms 616 KB Output is correct
36 Correct 60 ms 14040 KB Output is correct
37 Correct 70 ms 12996 KB Output is correct
38 Correct 76 ms 12280 KB Output is correct
39 Correct 79 ms 12524 KB Output is correct
40 Correct 75 ms 11916 KB Output is correct
41 Correct 45 ms 8380 KB Output is correct
42 Correct 47 ms 8376 KB Output is correct
43 Correct 83 ms 13192 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 0 ms 320 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 4 ms 692 KB Output is correct
50 Correct 5 ms 724 KB Output is correct
51 Correct 5 ms 724 KB Output is correct
52 Correct 4 ms 720 KB Output is correct
53 Correct 87 ms 14140 KB Output is correct
54 Correct 99 ms 12472 KB Output is correct
55 Correct 99 ms 12728 KB Output is correct
56 Correct 92 ms 12084 KB Output is correct
57 Correct 152 ms 16176 KB Output is correct
58 Correct 98 ms 14660 KB Output is correct
59 Correct 125 ms 13700 KB Output is correct