#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define F first
#define S second
ll MOD = 1000000007;
ll INF = 1000000000;
ll power(ll base, ll n){
if (n == 0) return 1;
if (n == 1) return base;
ll halfn = power(base, n/2);
if (n % 2 == 0) return (halfn * halfn) % MOD;
return (((halfn * halfn) % MOD) * base) % MOD;
}
ll inverse(ll n){
return power(n, MOD-2);
}
ll add(ll a, ll b){
return (a+b) % MOD;
}
ll mul(ll a, ll b){
return (a*b) % MOD;
}
ll gcd(ll a, ll b){
if (a == 0) return b;
if (a == 1) return 1;
return gcd(b%a, a);
}
int k;
struct piece{
int dist[5][5];
void init(){
ms(dist, -1, sizeof(dist));
}
void upd(int a, int b, int val){
if (dist[a][b] == -1) dist[a][b] = val;
else dist[a][b] = min(dist[a][b], val);
}
};
piece comb(piece p1, piece p2){
piece ans; ans.init();
FOR(i, 0, k){
FOR(j, 0, k){
FOR(l, 0, k){
if (p1.dist[i][l] == -1 || p2.dist[l][j] == -1) continue;
ans.upd(i, j, p1.dist[i][l] + p2.dist[l][j]);
}
}
}
return ans;
}
int main(){
ios::sync_with_stdio(false);
int n, m, o; cin >> k >> n >> m >> o;
int B = (n-1)/k+1;
piece g[B][19];
FOR(i, 0, B){
FOR(j, 0, 19) g[i][j].init();
}
FOR(i, 0, m){
int a, b, t; cin >> a >> b >> t;
g[a/k][0].upd(a%k, b%k, t);
}
FOR(i, 1, 19){
FOR(j, 0, B-(1<<i)){
g[j][i] = comb(g[j][i-1], g[j+(1<<(i-1))][i-1]);
}
}
while (o--){
int a, b; cin >> a >> b;
piece ans; ans.init();
int found = -1;
int len = b/k - a/k;
int cnt = 0; int pos = a/k;
while (len > 0){
if (len % 2 == 1){
if (found == -1){
ans = g[pos][cnt]; found = 1;
}
else ans = comb(ans, g[pos][cnt]);
pos += (1<<cnt);
}
len /= 2;
cnt++;
}
cout << ans.dist[a%k][b%k] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
94148 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
5 ms |
2236 KB |
Output is correct |
6 |
Correct |
4 ms |
2116 KB |
Output is correct |
7 |
Correct |
5 ms |
2124 KB |
Output is correct |
8 |
Correct |
184 ms |
94124 KB |
Output is correct |
9 |
Correct |
185 ms |
94148 KB |
Output is correct |
10 |
Correct |
166 ms |
93296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
48400 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
22 ms |
2252 KB |
Output is correct |
8 |
Correct |
22 ms |
1372 KB |
Output is correct |
9 |
Correct |
201 ms |
94128 KB |
Output is correct |
10 |
Correct |
134 ms |
33568 KB |
Output is correct |
11 |
Correct |
138 ms |
48420 KB |
Output is correct |
12 |
Correct |
108 ms |
32540 KB |
Output is correct |
13 |
Correct |
97 ms |
13892 KB |
Output is correct |
14 |
Correct |
77 ms |
20276 KB |
Output is correct |
15 |
Correct |
74 ms |
12592 KB |
Output is correct |
16 |
Correct |
77 ms |
12612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
3 ms |
2124 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
3 ms |
716 KB |
Output is correct |
9 |
Correct |
3 ms |
716 KB |
Output is correct |
10 |
Correct |
162 ms |
94112 KB |
Output is correct |
11 |
Correct |
105 ms |
48296 KB |
Output is correct |
12 |
Correct |
111 ms |
33452 KB |
Output is correct |
13 |
Correct |
112 ms |
33580 KB |
Output is correct |
14 |
Correct |
97 ms |
33056 KB |
Output is correct |
15 |
Correct |
63 ms |
12612 KB |
Output is correct |
16 |
Correct |
66 ms |
12592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
3 ms |
2124 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
3 ms |
716 KB |
Output is correct |
9 |
Correct |
3 ms |
716 KB |
Output is correct |
10 |
Correct |
162 ms |
94112 KB |
Output is correct |
11 |
Correct |
105 ms |
48296 KB |
Output is correct |
12 |
Correct |
111 ms |
33452 KB |
Output is correct |
13 |
Correct |
112 ms |
33580 KB |
Output is correct |
14 |
Correct |
97 ms |
33056 KB |
Output is correct |
15 |
Correct |
63 ms |
12612 KB |
Output is correct |
16 |
Correct |
66 ms |
12592 KB |
Output is correct |
17 |
Correct |
115 ms |
48292 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
8 ms |
2124 KB |
Output is correct |
24 |
Correct |
8 ms |
1228 KB |
Output is correct |
25 |
Correct |
11 ms |
716 KB |
Output is correct |
26 |
Correct |
10 ms |
844 KB |
Output is correct |
27 |
Correct |
177 ms |
94020 KB |
Output is correct |
28 |
Correct |
115 ms |
33484 KB |
Output is correct |
29 |
Correct |
111 ms |
33696 KB |
Output is correct |
30 |
Correct |
106 ms |
33224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
94148 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
5 ms |
2236 KB |
Output is correct |
6 |
Correct |
4 ms |
2116 KB |
Output is correct |
7 |
Correct |
5 ms |
2124 KB |
Output is correct |
8 |
Correct |
184 ms |
94124 KB |
Output is correct |
9 |
Correct |
185 ms |
94148 KB |
Output is correct |
10 |
Correct |
166 ms |
93296 KB |
Output is correct |
11 |
Correct |
127 ms |
48400 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
22 ms |
2252 KB |
Output is correct |
18 |
Correct |
22 ms |
1372 KB |
Output is correct |
19 |
Correct |
201 ms |
94128 KB |
Output is correct |
20 |
Correct |
134 ms |
33568 KB |
Output is correct |
21 |
Correct |
138 ms |
48420 KB |
Output is correct |
22 |
Correct |
108 ms |
32540 KB |
Output is correct |
23 |
Correct |
97 ms |
13892 KB |
Output is correct |
24 |
Correct |
77 ms |
20276 KB |
Output is correct |
25 |
Correct |
74 ms |
12592 KB |
Output is correct |
26 |
Correct |
77 ms |
12612 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
3 ms |
2124 KB |
Output is correct |
33 |
Correct |
2 ms |
1228 KB |
Output is correct |
34 |
Correct |
3 ms |
716 KB |
Output is correct |
35 |
Correct |
3 ms |
716 KB |
Output is correct |
36 |
Correct |
162 ms |
94112 KB |
Output is correct |
37 |
Correct |
105 ms |
48296 KB |
Output is correct |
38 |
Correct |
111 ms |
33452 KB |
Output is correct |
39 |
Correct |
112 ms |
33580 KB |
Output is correct |
40 |
Correct |
97 ms |
33056 KB |
Output is correct |
41 |
Correct |
63 ms |
12612 KB |
Output is correct |
42 |
Correct |
66 ms |
12592 KB |
Output is correct |
43 |
Correct |
115 ms |
48292 KB |
Output is correct |
44 |
Correct |
1 ms |
204 KB |
Output is correct |
45 |
Correct |
1 ms |
312 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
8 ms |
2124 KB |
Output is correct |
50 |
Correct |
8 ms |
1228 KB |
Output is correct |
51 |
Correct |
11 ms |
716 KB |
Output is correct |
52 |
Correct |
10 ms |
844 KB |
Output is correct |
53 |
Correct |
177 ms |
94020 KB |
Output is correct |
54 |
Correct |
115 ms |
33484 KB |
Output is correct |
55 |
Correct |
111 ms |
33696 KB |
Output is correct |
56 |
Correct |
106 ms |
33224 KB |
Output is correct |
57 |
Correct |
158 ms |
26820 KB |
Output is correct |
58 |
Correct |
183 ms |
94276 KB |
Output is correct |
59 |
Correct |
139 ms |
48544 KB |
Output is correct |