Submission #124790

# Submission time Handle Problem Language Result Execution time Memory
124790 2019-07-04T02:02:21 Z model_code Toll (BOI17_toll) C++17
100 / 100
276 ms 20216 KB
#include<stdio.h>
#include<vector>
#include<iostream>
#define llint long long 

using namespace std;

const llint bigVal = ((long long) 100000)*((long long) 100001);
int K;

// shortest path matrix
struct spm {
	vector<vector<llint> > M;
	
	spm(int dim, llint initVal, llint diagVal) {
		M = vector<vector<llint> > (dim, vector<llint> (dim, initVal));
		for(int i = 0; i < dim; ++i) M[i][i] = diagVal;
	}
	
	inline int dim() {return M.size();}
};

// overload + to (min,+) product for spm's.
spm operator + (spm L, spm R) {
	int dim = L.dim();
	spm ans(dim, 0, 0);
	for(int i = 0; i < dim; ++i) {
		for(int j = 0; j < dim; ++j) {
			llint cur = L.M[i][0] + R.M[0][j];
			for(int k = 1; k < dim; ++k) {
				llint newVal = L.M[i][k] + R.M[k][j];
				cur = cur < newVal ? cur : newVal;
			}
			ans.M[i][j] = cur;
		}
	}
	return ans;
}

// binary segment tree for spms without update capability. 
struct bst {
	vector<spm> a;
	int N;
	int offs;
	
	bst(vector<spm> initArray) {
		int n = initArray.size();
		N = 2;
		while(N < 2*n+4) N *= 2;
		offs = N/2 + 1;
		a = vector<spm> (N, spm(K, bigVal, bigVal));
		for(int i = 0; i < n; ++i) a[i+offs] = initArray[i];
		for(int i = offs-2; i > 0; --i) a[i] = a[2*i] + a[(2*i)+1];
	}
	
	spm sum(int i, int j) {
		int L = i + offs - 1;
		int R = j + offs + 1;
		spm Lans(K,bigVal,(llint)0);
		spm Rans(K,bigVal,(llint)0);
		while(true) {
			bool Lright = L%2 == 0;
			bool Rleft = R%2 == 1;
			L /= 2; 
			R /= 2;
			if(L==R) break;
			if(Lright) Lans = Lans + a[2*L+1];
			if(Rleft) Rans = a[2*R] + Rans;
		}
		return Lans + Rans;
	}
};

int main() {
	
	int N,M,O;
		
	// read input
	scanf("%d%d%d%d",&K,&N,&M,&O);
	vector<spm> initArray(N/K, spm(K, bigVal, bigVal));
	for(int i = 0; i < M; ++i) {
		int a,b,t;
		scanf("%d%d%d",&a,&b,&t);
		initArray[a/K].M[a%K][b%K] = t;
	}

	// make bst
	bst T(initArray);
	
	// answer queries
	for(int i = 0; i < O; ++i) {
		int a,b;
		scanf("%d%d",&a,&b);
		if(a/K >= b/K) {
			printf("-1\n");
		} else {
			spm ans = T.sum(a/K, b/K-1);
			printf("%lld\n", (ans.M[a%K][b%K] < bigVal ? ans.M[a%K][b%K] : -1));
		}
	}
	
	return 0;
}

Compilation message

toll.cpp: In function 'int main()':
toll.cpp:79:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d%d",&K,&N,&M,&O);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:83:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d",&a,&b,&t);
   ~~~~~^~~~~~~~~~~~~~~~~~~
toll.cpp:93:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&a,&b);
   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 147 ms 20216 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 6 ms 632 KB Output is correct
6 Correct 6 ms 632 KB Output is correct
7 Correct 6 ms 632 KB Output is correct
8 Correct 140 ms 20216 KB Output is correct
9 Correct 144 ms 20216 KB Output is correct
10 Correct 119 ms 20216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 226 ms 17532 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 47 ms 760 KB Output is correct
8 Correct 60 ms 760 KB Output is correct
9 Correct 158 ms 20216 KB Output is correct
10 Correct 257 ms 19708 KB Output is correct
11 Correct 205 ms 17528 KB Output is correct
12 Correct 229 ms 19792 KB Output is correct
13 Correct 150 ms 11256 KB Output is correct
14 Correct 104 ms 10716 KB Output is correct
15 Correct 113 ms 11256 KB Output is correct
16 Correct 117 ms 11256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 4 ms 632 KB Output is correct
7 Correct 4 ms 632 KB Output is correct
8 Correct 5 ms 632 KB Output is correct
9 Correct 5 ms 632 KB Output is correct
10 Correct 95 ms 20216 KB Output is correct
11 Correct 117 ms 17528 KB Output is correct
12 Correct 123 ms 19704 KB Output is correct
13 Correct 148 ms 19708 KB Output is correct
14 Correct 127 ms 19664 KB Output is correct
15 Correct 62 ms 11256 KB Output is correct
16 Correct 64 ms 11284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 4 ms 632 KB Output is correct
7 Correct 4 ms 632 KB Output is correct
8 Correct 5 ms 632 KB Output is correct
9 Correct 5 ms 632 KB Output is correct
10 Correct 95 ms 20216 KB Output is correct
11 Correct 117 ms 17528 KB Output is correct
12 Correct 123 ms 19704 KB Output is correct
13 Correct 148 ms 19708 KB Output is correct
14 Correct 127 ms 19664 KB Output is correct
15 Correct 62 ms 11256 KB Output is correct
16 Correct 64 ms 11284 KB Output is correct
17 Correct 115 ms 17528 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 256 KB Output is correct
22 Correct 2 ms 256 KB Output is correct
23 Correct 13 ms 632 KB Output is correct
24 Correct 16 ms 632 KB Output is correct
25 Correct 26 ms 760 KB Output is correct
26 Correct 22 ms 632 KB Output is correct
27 Correct 102 ms 20216 KB Output is correct
28 Correct 153 ms 19668 KB Output is correct
29 Correct 160 ms 19704 KB Output is correct
30 Correct 142 ms 19716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 147 ms 20216 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 6 ms 632 KB Output is correct
6 Correct 6 ms 632 KB Output is correct
7 Correct 6 ms 632 KB Output is correct
8 Correct 140 ms 20216 KB Output is correct
9 Correct 144 ms 20216 KB Output is correct
10 Correct 119 ms 20216 KB Output is correct
11 Correct 226 ms 17532 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 47 ms 760 KB Output is correct
18 Correct 60 ms 760 KB Output is correct
19 Correct 158 ms 20216 KB Output is correct
20 Correct 257 ms 19708 KB Output is correct
21 Correct 205 ms 17528 KB Output is correct
22 Correct 229 ms 19792 KB Output is correct
23 Correct 150 ms 11256 KB Output is correct
24 Correct 104 ms 10716 KB Output is correct
25 Correct 113 ms 11256 KB Output is correct
26 Correct 117 ms 11256 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 256 KB Output is correct
29 Correct 2 ms 256 KB Output is correct
30 Correct 2 ms 256 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 4 ms 632 KB Output is correct
33 Correct 4 ms 632 KB Output is correct
34 Correct 5 ms 632 KB Output is correct
35 Correct 5 ms 632 KB Output is correct
36 Correct 95 ms 20216 KB Output is correct
37 Correct 117 ms 17528 KB Output is correct
38 Correct 123 ms 19704 KB Output is correct
39 Correct 148 ms 19708 KB Output is correct
40 Correct 127 ms 19664 KB Output is correct
41 Correct 62 ms 11256 KB Output is correct
42 Correct 64 ms 11284 KB Output is correct
43 Correct 115 ms 17528 KB Output is correct
44 Correct 2 ms 256 KB Output is correct
45 Correct 2 ms 376 KB Output is correct
46 Correct 2 ms 376 KB Output is correct
47 Correct 2 ms 256 KB Output is correct
48 Correct 2 ms 256 KB Output is correct
49 Correct 13 ms 632 KB Output is correct
50 Correct 16 ms 632 KB Output is correct
51 Correct 26 ms 760 KB Output is correct
52 Correct 22 ms 632 KB Output is correct
53 Correct 102 ms 20216 KB Output is correct
54 Correct 153 ms 19668 KB Output is correct
55 Correct 160 ms 19704 KB Output is correct
56 Correct 142 ms 19716 KB Output is correct
57 Correct 276 ms 18852 KB Output is correct
58 Correct 145 ms 20216 KB Output is correct
59 Correct 182 ms 17524 KB Output is correct