Submission #431389

# Submission time Handle Problem Language Result Execution time Memory
431389 2021-06-17T11:33:57 Z errorgorn Long Distance Coach (JOI17_coach) C++17
100 / 100
583 ms 123532 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ii pair<ll,ll>
#define fi first
#define se second
#define endl '\n'

#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define lb lower_bound
#define ub upper_bound

#define rep(x,s,e) for (auto x=s-(s>e);x!=e-(s>e);s<e?x++:x--)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll l,n,m,w,k;
ll dist[200005];
ii arr[200005];
ll pref[200005];

vector<ii> trans[200005];
ll memo[200005];

struct line{
	ll m,c;
	
	ll eval(ll x){
		return m*x+c;
	}
};

struct hull{
	vector<line> stk;
	
	#define ld long double
	
	ld POI(line i,line j){
		return (ld)(i.c-j.c)/(ld)(j.m-i.m);
	}
	
	void add(line i){
		while (sz(stk)>=2 &&
			POI(stk[sz(stk)-2],stk[sz(stk)-1])>POI(stk[sz(stk)-1],i)) stk.pob();
		
		stk.pub(i);
	}
	
	ll eval(ll x){
		int lo=0,hi=sz(stk)-1,mi;
		
		while (hi>lo){
			mi=hi+lo>>1;
			
			if (x<POI(stk[mi],stk[mi+1])) hi=mi;
			else lo=mi+1;
		}
		
		return stk[lo].eval(x);
	}
};

struct node{
	int s,e,m;
	hull val;
	node *l,*r;
	
	node (int _s,int _e){
		s=_s,e=_e,m=s+e>>1;
		
		if (s!=e){
			l=new node(s,m);
			r=new node(m+1,e);
		}
	}
	
	void update(int i,line k){
		val.add(k);
		
		if (s==e) return;
		if (i<=m) l->update(i,k);
		else r->update(i,k);
	}
	
	ll query(int i,int j,ll x){
		if (s==i && e==j) return val.eval(x);
		else if (j<=m) return l->query(i,j,x);
		else if (m<i) return r->query(i,j,x);
		else return min(l->query(i,m,x),r->query(m+1,j,x));
	}
} *root=new node(0,200005);

int main(){
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(false);
	
	cin>>l>>n>>m>>w>>k;
	
	rep(x,1,n+1) cin>>dist[x];
	dist[n+1]=l;
	sort(dist,dist+n+2);
	
	rep(x,0,m) cin>>arr[x].fi>>arr[x].se;
	sort(arr,arr+m);
	arr[m]=ii(1e9,1e9);
	
	rep(x,0,m) pref[x+1]=pref[x]+arr[x].se;
	
	rep(x,0,n+1){
		//cout<<dist[x]<<" "<<dist[x+1]<<endl;
		
		int l,r;
		
		if ((dist[x]/k+1)*k<=dist[x+1]) l=0;
		else l=lb(arr,arr+m,ii(dist[x]%k,-1))-arr;
		r=lb(arr,arr+m+1,ii(dist[x+1]%k,1e9))-arr-1;
		
		if (l>r) continue;
		
		trans[r].pub(ii(l,(dist[x+1]/k)));
		//cout<<l<<" "<<r<<" "<<(dist[x+1]/k+1)<<endl;
	}
	
	memo[0]=(l/k+1)*w;
	root->update(0,line({-0,memo[0]-pref[0]}));
	
	rep(x,0,m){
		memo[x+1]=memo[x]+((l-arr[x].fi)/k+1)*w;
		
		for (auto &it:trans[x]){
			memo[x+1]=min(memo[x+1],
					root->query(it.fi,x,w*it.se)+pref[x+1]+(x+1)*w*it.se);
		}
		
		root->update(x+1,line({-(x+1),memo[x+1]-pref[x+1]}));
	}
	
	cout<<memo[m]<<endl;
}

Compilation message

coach.cpp: In member function 'long long int hull::eval(long long int)':
coach.cpp:59:9: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   59 |    mi=hi+lo>>1;
      |       ~~^~~
coach.cpp: In constructor 'node::node(int, int)':
coach.cpp:75:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   75 |   s=_s,e=_e,m=s+e>>1;
      |               ~^~
# Verdict Execution time Memory Grader output
1 Correct 32 ms 30084 KB Output is correct
2 Correct 30 ms 30020 KB Output is correct
3 Correct 31 ms 30084 KB Output is correct
4 Correct 27 ms 30012 KB Output is correct
5 Correct 26 ms 29972 KB Output is correct
6 Correct 28 ms 30028 KB Output is correct
7 Correct 28 ms 30052 KB Output is correct
8 Correct 29 ms 30048 KB Output is correct
9 Correct 33 ms 30080 KB Output is correct
10 Correct 35 ms 29988 KB Output is correct
11 Correct 30 ms 30072 KB Output is correct
12 Correct 27 ms 30020 KB Output is correct
13 Correct 27 ms 30000 KB Output is correct
14 Correct 27 ms 29944 KB Output is correct
15 Correct 27 ms 30004 KB Output is correct
16 Correct 26 ms 30060 KB Output is correct
17 Correct 27 ms 30028 KB Output is correct
18 Correct 27 ms 30044 KB Output is correct
19 Correct 26 ms 30020 KB Output is correct
20 Correct 28 ms 30020 KB Output is correct
21 Correct 27 ms 30020 KB Output is correct
22 Correct 29 ms 30060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 30084 KB Output is correct
2 Correct 30 ms 30020 KB Output is correct
3 Correct 31 ms 30084 KB Output is correct
4 Correct 27 ms 30012 KB Output is correct
5 Correct 26 ms 29972 KB Output is correct
6 Correct 28 ms 30028 KB Output is correct
7 Correct 28 ms 30052 KB Output is correct
8 Correct 29 ms 30048 KB Output is correct
9 Correct 33 ms 30080 KB Output is correct
10 Correct 35 ms 29988 KB Output is correct
11 Correct 30 ms 30072 KB Output is correct
12 Correct 27 ms 30020 KB Output is correct
13 Correct 27 ms 30000 KB Output is correct
14 Correct 27 ms 29944 KB Output is correct
15 Correct 27 ms 30004 KB Output is correct
16 Correct 26 ms 30060 KB Output is correct
17 Correct 27 ms 30028 KB Output is correct
18 Correct 27 ms 30044 KB Output is correct
19 Correct 26 ms 30020 KB Output is correct
20 Correct 28 ms 30020 KB Output is correct
21 Correct 27 ms 30020 KB Output is correct
22 Correct 29 ms 30060 KB Output is correct
23 Correct 26 ms 30020 KB Output is correct
24 Correct 27 ms 30024 KB Output is correct
25 Correct 28 ms 30028 KB Output is correct
26 Correct 27 ms 30016 KB Output is correct
27 Correct 26 ms 30028 KB Output is correct
28 Correct 26 ms 30036 KB Output is correct
29 Correct 33 ms 30068 KB Output is correct
30 Correct 31 ms 30012 KB Output is correct
31 Correct 32 ms 30068 KB Output is correct
32 Correct 33 ms 30008 KB Output is correct
33 Correct 29 ms 30088 KB Output is correct
34 Correct 27 ms 30084 KB Output is correct
35 Correct 27 ms 30060 KB Output is correct
36 Correct 26 ms 30120 KB Output is correct
37 Correct 27 ms 30020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 30084 KB Output is correct
2 Correct 30 ms 30020 KB Output is correct
3 Correct 31 ms 30084 KB Output is correct
4 Correct 27 ms 30012 KB Output is correct
5 Correct 26 ms 29972 KB Output is correct
6 Correct 28 ms 30028 KB Output is correct
7 Correct 28 ms 30052 KB Output is correct
8 Correct 29 ms 30048 KB Output is correct
9 Correct 33 ms 30080 KB Output is correct
10 Correct 35 ms 29988 KB Output is correct
11 Correct 30 ms 30072 KB Output is correct
12 Correct 27 ms 30020 KB Output is correct
13 Correct 27 ms 30000 KB Output is correct
14 Correct 27 ms 29944 KB Output is correct
15 Correct 27 ms 30004 KB Output is correct
16 Correct 26 ms 30060 KB Output is correct
17 Correct 27 ms 30028 KB Output is correct
18 Correct 27 ms 30044 KB Output is correct
19 Correct 26 ms 30020 KB Output is correct
20 Correct 28 ms 30020 KB Output is correct
21 Correct 27 ms 30020 KB Output is correct
22 Correct 29 ms 30060 KB Output is correct
23 Correct 26 ms 30020 KB Output is correct
24 Correct 27 ms 30024 KB Output is correct
25 Correct 28 ms 30028 KB Output is correct
26 Correct 27 ms 30016 KB Output is correct
27 Correct 26 ms 30028 KB Output is correct
28 Correct 26 ms 30036 KB Output is correct
29 Correct 33 ms 30068 KB Output is correct
30 Correct 31 ms 30012 KB Output is correct
31 Correct 32 ms 30068 KB Output is correct
32 Correct 33 ms 30008 KB Output is correct
33 Correct 29 ms 30088 KB Output is correct
34 Correct 27 ms 30084 KB Output is correct
35 Correct 27 ms 30060 KB Output is correct
36 Correct 26 ms 30120 KB Output is correct
37 Correct 27 ms 30020 KB Output is correct
38 Correct 30 ms 30436 KB Output is correct
39 Correct 32 ms 30360 KB Output is correct
40 Correct 31 ms 30440 KB Output is correct
41 Correct 29 ms 30568 KB Output is correct
42 Correct 29 ms 30404 KB Output is correct
43 Correct 30 ms 30268 KB Output is correct
44 Correct 30 ms 30280 KB Output is correct
45 Correct 29 ms 30384 KB Output is correct
46 Correct 32 ms 30976 KB Output is correct
47 Correct 30 ms 30340 KB Output is correct
48 Correct 37 ms 30416 KB Output is correct
49 Correct 38 ms 30528 KB Output is correct
50 Correct 31 ms 30344 KB Output is correct
51 Correct 37 ms 30404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 30084 KB Output is correct
2 Correct 30 ms 30020 KB Output is correct
3 Correct 31 ms 30084 KB Output is correct
4 Correct 27 ms 30012 KB Output is correct
5 Correct 26 ms 29972 KB Output is correct
6 Correct 28 ms 30028 KB Output is correct
7 Correct 28 ms 30052 KB Output is correct
8 Correct 29 ms 30048 KB Output is correct
9 Correct 33 ms 30080 KB Output is correct
10 Correct 35 ms 29988 KB Output is correct
11 Correct 30 ms 30072 KB Output is correct
12 Correct 27 ms 30020 KB Output is correct
13 Correct 27 ms 30000 KB Output is correct
14 Correct 27 ms 29944 KB Output is correct
15 Correct 27 ms 30004 KB Output is correct
16 Correct 26 ms 30060 KB Output is correct
17 Correct 27 ms 30028 KB Output is correct
18 Correct 27 ms 30044 KB Output is correct
19 Correct 26 ms 30020 KB Output is correct
20 Correct 28 ms 30020 KB Output is correct
21 Correct 27 ms 30020 KB Output is correct
22 Correct 29 ms 30060 KB Output is correct
23 Correct 26 ms 30020 KB Output is correct
24 Correct 27 ms 30024 KB Output is correct
25 Correct 28 ms 30028 KB Output is correct
26 Correct 27 ms 30016 KB Output is correct
27 Correct 26 ms 30028 KB Output is correct
28 Correct 26 ms 30036 KB Output is correct
29 Correct 33 ms 30068 KB Output is correct
30 Correct 31 ms 30012 KB Output is correct
31 Correct 32 ms 30068 KB Output is correct
32 Correct 33 ms 30008 KB Output is correct
33 Correct 29 ms 30088 KB Output is correct
34 Correct 27 ms 30084 KB Output is correct
35 Correct 27 ms 30060 KB Output is correct
36 Correct 26 ms 30120 KB Output is correct
37 Correct 27 ms 30020 KB Output is correct
38 Correct 30 ms 30436 KB Output is correct
39 Correct 32 ms 30360 KB Output is correct
40 Correct 31 ms 30440 KB Output is correct
41 Correct 29 ms 30568 KB Output is correct
42 Correct 29 ms 30404 KB Output is correct
43 Correct 30 ms 30268 KB Output is correct
44 Correct 30 ms 30280 KB Output is correct
45 Correct 29 ms 30384 KB Output is correct
46 Correct 32 ms 30976 KB Output is correct
47 Correct 30 ms 30340 KB Output is correct
48 Correct 37 ms 30416 KB Output is correct
49 Correct 38 ms 30528 KB Output is correct
50 Correct 31 ms 30344 KB Output is correct
51 Correct 37 ms 30404 KB Output is correct
52 Correct 583 ms 64824 KB Output is correct
53 Correct 469 ms 71800 KB Output is correct
54 Correct 337 ms 81104 KB Output is correct
55 Correct 288 ms 73880 KB Output is correct
56 Correct 312 ms 65108 KB Output is correct
57 Correct 359 ms 69688 KB Output is correct
58 Correct 289 ms 63932 KB Output is correct
59 Correct 323 ms 73144 KB Output is correct
60 Correct 325 ms 78740 KB Output is correct
61 Correct 323 ms 75872 KB Output is correct
62 Correct 349 ms 72156 KB Output is correct
63 Correct 507 ms 123532 KB Output is correct
64 Correct 255 ms 64588 KB Output is correct
65 Correct 457 ms 70816 KB Output is correct
66 Correct 487 ms 70828 KB Output is correct
67 Correct 465 ms 71120 KB Output is correct
68 Correct 498 ms 71648 KB Output is correct
69 Correct 503 ms 70284 KB Output is correct
70 Correct 517 ms 70224 KB Output is correct
71 Correct 534 ms 70236 KB Output is correct
72 Correct 509 ms 70244 KB Output is correct
73 Correct 514 ms 70228 KB Output is correct
74 Correct 512 ms 69896 KB Output is correct
75 Correct 491 ms 69456 KB Output is correct
76 Correct 483 ms 69652 KB Output is correct
77 Correct 303 ms 93064 KB Output is correct
78 Correct 317 ms 67860 KB Output is correct
79 Correct 526 ms 69920 KB Output is correct
80 Correct 500 ms 69980 KB Output is correct