Submission #315593

# Submission time Handle Problem Language Result Execution time Memory
315593 2020-10-23T09:29:45 Z nafis_shifat Toll (BOI17_toll) C++14
100 / 100
499 ms 21940 KB
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=5e4+5;
const ll inf=1e15;
vector<int> adj[mxn];
vector<int> tps[mxn];
int l[5 * mxn],r[5 * mxn],w[5 * mxn];
int k,m,n;
ll d[mxn][6][6];

ll res[mxn];
struct query {
	int a,b;
	int ind;
	query(int x,int y,int z) : a(x),b(y),ind(z) {};
};
void solve(int b, int e, vector<query> q) {
	if(q.size() == 0) return;
	if(b == e) {
		for(auto i : q) res[i.ind] = -1;
		return;
	}
	int mid = b + e >> 1;
	for(int i = 0; i < k; i++) {
		for(int j = 0; j < k; j++) {
			d[mid][i][j] = i == j ? 0 : inf;
		}
	}

	for(int i = mid - 1; i >= b; i--) {
		for(int j = 0; j < k; j++) {

			for(int x = 0; x < k; x++) d[i][j][x] = inf;
			int u = i * k + j;
			for(int z : adj[u]) {
				int v = r[z];
				for(int x = 0; x < k; x++) {
					d[i][j][x] = min(d[i][j][x], d[i+1][v%k][x] + w[z]); 
				}
			}
		}
	}

	for(int i = mid + 1; i <= e; i++) {
		for(int j = 0; j < k; j++) {
			for(int x = 0; x < k; x++) d[i][j][x] = inf;
			int u = i * k + j;
			for(int z : tps[u]) {
				int v = l[z];
				for(int x = 0; x < k; x++) {
					d[i][j][x] = min(d[i][j][x], d[i-1][v%k][x] + w[z]); 
				}
			}
		}
	}

	vector<query> tm[2];


	for(auto i : q) {
		int x = i.a / k;
		int y = i.b / k;

		if(x<=mid && y >= mid) {
			ll ans = inf;
			for(int j = 0; j < k; j++) {
				ans = min(ans,d[x][i.a % k][j]+d[y][i.b % k][j]);
			}

			res[i.ind] = ans >= inf ? -1 : ans;
			continue;
		}

		tm[x > mid].push_back(i);
	}

	solve(b,mid,tm[0]);
	solve(mid+1,e,tm[1]);





}

int main() {
	int o;
	cin>>k>>n>>m>>o;

	for(int i = 1; i <= m; i++) {
		cin>>l[i]>>r[i]>>w[i];
		adj[l[i]].push_back(i);
		tps[r[i]].push_back(i);
	}

	vector<query> qs;

	for(int i = 1; i <= o; i++) {
		int a,b;
		cin>>a>>b;
		qs.push_back(query(a,b,i));
	}


	solve(0,(n + k - 1) / k,qs);

	for(int i = 1; i <= o; i++) {
		cout<<res[i]<<endl;
	}





	
}

Compilation message

toll.cpp: In function 'void solve(int, int, std::vector<query>)':
toll.cpp:25:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   25 |  int mid = b + e >> 1;
      |            ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 152 ms 21048 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 7 ms 3072 KB Output is correct
6 Correct 7 ms 3072 KB Output is correct
7 Correct 7 ms 3072 KB Output is correct
8 Correct 153 ms 21876 KB Output is correct
9 Correct 150 ms 21664 KB Output is correct
10 Correct 46 ms 17344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 228 ms 14588 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Correct 33 ms 3776 KB Output is correct
8 Correct 35 ms 3644 KB Output is correct
9 Correct 142 ms 21940 KB Output is correct
10 Correct 312 ms 14904 KB Output is correct
11 Correct 236 ms 16312 KB Output is correct
12 Correct 187 ms 12984 KB Output is correct
13 Correct 304 ms 12152 KB Output is correct
14 Correct 183 ms 10156 KB Output is correct
15 Correct 159 ms 8568 KB Output is correct
16 Correct 159 ms 8568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 4 ms 3072 KB Output is correct
7 Correct 5 ms 2944 KB Output is correct
8 Correct 10 ms 2944 KB Output is correct
9 Correct 8 ms 2816 KB Output is correct
10 Correct 113 ms 20472 KB Output is correct
11 Correct 206 ms 15480 KB Output is correct
12 Correct 291 ms 14328 KB Output is correct
13 Correct 320 ms 14840 KB Output is correct
14 Correct 246 ms 13672 KB Output is correct
15 Correct 149 ms 8312 KB Output is correct
16 Correct 152 ms 8440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 4 ms 3072 KB Output is correct
7 Correct 5 ms 2944 KB Output is correct
8 Correct 10 ms 2944 KB Output is correct
9 Correct 8 ms 2816 KB Output is correct
10 Correct 113 ms 20472 KB Output is correct
11 Correct 206 ms 15480 KB Output is correct
12 Correct 291 ms 14328 KB Output is correct
13 Correct 320 ms 14840 KB Output is correct
14 Correct 246 ms 13672 KB Output is correct
15 Correct 149 ms 8312 KB Output is correct
16 Correct 152 ms 8440 KB Output is correct
17 Correct 220 ms 15700 KB Output is correct
18 Correct 2 ms 2688 KB Output is correct
19 Correct 2 ms 2688 KB Output is correct
20 Correct 2 ms 2688 KB Output is correct
21 Correct 2 ms 2688 KB Output is correct
22 Correct 2 ms 2688 KB Output is correct
23 Correct 13 ms 3200 KB Output is correct
24 Correct 14 ms 3200 KB Output is correct
25 Correct 17 ms 3200 KB Output is correct
26 Correct 16 ms 3072 KB Output is correct
27 Correct 128 ms 21496 KB Output is correct
28 Correct 312 ms 14456 KB Output is correct
29 Correct 347 ms 15096 KB Output is correct
30 Correct 266 ms 14032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 152 ms 21048 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 7 ms 3072 KB Output is correct
6 Correct 7 ms 3072 KB Output is correct
7 Correct 7 ms 3072 KB Output is correct
8 Correct 153 ms 21876 KB Output is correct
9 Correct 150 ms 21664 KB Output is correct
10 Correct 46 ms 17344 KB Output is correct
11 Correct 228 ms 14588 KB Output is correct
12 Correct 2 ms 2688 KB Output is correct
13 Correct 2 ms 2688 KB Output is correct
14 Correct 2 ms 2688 KB Output is correct
15 Correct 2 ms 2688 KB Output is correct
16 Correct 2 ms 2688 KB Output is correct
17 Correct 33 ms 3776 KB Output is correct
18 Correct 35 ms 3644 KB Output is correct
19 Correct 142 ms 21940 KB Output is correct
20 Correct 312 ms 14904 KB Output is correct
21 Correct 236 ms 16312 KB Output is correct
22 Correct 187 ms 12984 KB Output is correct
23 Correct 304 ms 12152 KB Output is correct
24 Correct 183 ms 10156 KB Output is correct
25 Correct 159 ms 8568 KB Output is correct
26 Correct 159 ms 8568 KB Output is correct
27 Correct 2 ms 2688 KB Output is correct
28 Correct 2 ms 2688 KB Output is correct
29 Correct 2 ms 2688 KB Output is correct
30 Correct 2 ms 2688 KB Output is correct
31 Correct 2 ms 2688 KB Output is correct
32 Correct 4 ms 3072 KB Output is correct
33 Correct 5 ms 2944 KB Output is correct
34 Correct 10 ms 2944 KB Output is correct
35 Correct 8 ms 2816 KB Output is correct
36 Correct 113 ms 20472 KB Output is correct
37 Correct 206 ms 15480 KB Output is correct
38 Correct 291 ms 14328 KB Output is correct
39 Correct 320 ms 14840 KB Output is correct
40 Correct 246 ms 13672 KB Output is correct
41 Correct 149 ms 8312 KB Output is correct
42 Correct 152 ms 8440 KB Output is correct
43 Correct 220 ms 15700 KB Output is correct
44 Correct 2 ms 2688 KB Output is correct
45 Correct 2 ms 2688 KB Output is correct
46 Correct 2 ms 2688 KB Output is correct
47 Correct 2 ms 2688 KB Output is correct
48 Correct 2 ms 2688 KB Output is correct
49 Correct 13 ms 3200 KB Output is correct
50 Correct 14 ms 3200 KB Output is correct
51 Correct 17 ms 3200 KB Output is correct
52 Correct 16 ms 3072 KB Output is correct
53 Correct 128 ms 21496 KB Output is correct
54 Correct 312 ms 14456 KB Output is correct
55 Correct 347 ms 15096 KB Output is correct
56 Correct 266 ms 14032 KB Output is correct
57 Correct 499 ms 15416 KB Output is correct
58 Correct 158 ms 21940 KB Output is correct
59 Correct 272 ms 16256 KB Output is correct