#include <bits/stdc++.h>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;
const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=50010, LOG=20;
int n, m, k, q, u, v, x, y, t, a, b, ans;
int A[MAXN];
vector<piii> E[MAXN];
struct Node{
int dist[5][5], st=-1;
Node(){
memset(dist, 31, sizeof(dist));
}
} seg[MAXN<<2];
Node Merge(Node x, Node y){
if (x.st==-1) return y;
if (y.st==-1) return x;
Node z;
z.st=x.st;
for (int i=0; i<k; i++) for (int j=0; j<k; j++) for (piii p:E[y.st-1]){
z.dist[i][j]=min(z.dist[i][j], x.dist[i][p.first.first]+p.second+y.dist[p.first.second][j]);
}
return z;
}
void Build(int id, int tl, int tr){
seg[id].st=tl;
if (tr-tl==1){
for (int i=0; i<k; i++) seg[id].dist[i][i]=0;
return ;
}
int mid=(tl+tr)>>1;
Build(id<<1, tl, mid);
Build(id<<1 | 1, mid, tr);
seg[id]=Merge(seg[id<<1], seg[id<<1 | 1]);
// debug2(tl, tr)
// for (int i=0; i<k; i++) for (int j=0; j<k; j++) cerr<<seg[id].dist[i][j]<<" \n"[j==k-1];
}
Node Get(int id, int tl, int tr, int l, int r){
if (r<=tl || tr<=l) return seg[0];
if (l<=tl && tr<=r) return seg[id];
int mid=(tl+tr)>>1;
return Merge(Get(id<<1, tl, mid, l, r), Get(id<<1 | 1, mid, tr, l, r));
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin>>k>>n>>m>>q;
while (m--){
cin>>u>>v>>x;
E[u/k].pb({{u%k, v%k}, x});
}
Build(1, 0, (n+k-1)/k);
while (q--){
cin>>u>>v;
Node tmp=Get(1, 0, (n+k-1)/k, u/k, v/k+1);
ans=tmp.dist[u%k][v%k];
if (ans>5e8) ans=-1;
cout<<ans<<'\n';
}
return 0;
}
/*
5 14 5 5
0 5 9
5 12 10
0 7 7
7 12 8
4 7 10
0 12
0 5
0 7
7 12
0 13
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
24440 KB |
Output is correct |
2 |
Correct |
15 ms |
21888 KB |
Output is correct |
3 |
Correct |
15 ms |
21888 KB |
Output is correct |
4 |
Correct |
16 ms |
21888 KB |
Output is correct |
5 |
Correct |
18 ms |
21888 KB |
Output is correct |
6 |
Correct |
17 ms |
21888 KB |
Output is correct |
7 |
Correct |
17 ms |
21888 KB |
Output is correct |
8 |
Correct |
65 ms |
24440 KB |
Output is correct |
9 |
Correct |
62 ms |
24312 KB |
Output is correct |
10 |
Correct |
36 ms |
22016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
25208 KB |
Output is correct |
2 |
Correct |
16 ms |
21888 KB |
Output is correct |
3 |
Correct |
15 ms |
21888 KB |
Output is correct |
4 |
Correct |
16 ms |
21888 KB |
Output is correct |
5 |
Correct |
15 ms |
21888 KB |
Output is correct |
6 |
Correct |
15 ms |
21888 KB |
Output is correct |
7 |
Correct |
22 ms |
22016 KB |
Output is correct |
8 |
Correct |
24 ms |
22044 KB |
Output is correct |
9 |
Correct |
54 ms |
24320 KB |
Output is correct |
10 |
Correct |
88 ms |
27104 KB |
Output is correct |
11 |
Correct |
70 ms |
25336 KB |
Output is correct |
12 |
Correct |
62 ms |
24824 KB |
Output is correct |
13 |
Correct |
100 ms |
26872 KB |
Output is correct |
14 |
Correct |
57 ms |
25720 KB |
Output is correct |
15 |
Correct |
60 ms |
24568 KB |
Output is correct |
16 |
Correct |
60 ms |
24568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
21888 KB |
Output is correct |
2 |
Correct |
16 ms |
21888 KB |
Output is correct |
3 |
Correct |
16 ms |
21888 KB |
Output is correct |
4 |
Correct |
17 ms |
21888 KB |
Output is correct |
5 |
Correct |
16 ms |
21888 KB |
Output is correct |
6 |
Correct |
16 ms |
21888 KB |
Output is correct |
7 |
Correct |
16 ms |
21888 KB |
Output is correct |
8 |
Correct |
18 ms |
22016 KB |
Output is correct |
9 |
Correct |
17 ms |
22016 KB |
Output is correct |
10 |
Correct |
40 ms |
24184 KB |
Output is correct |
11 |
Correct |
55 ms |
25080 KB |
Output is correct |
12 |
Correct |
76 ms |
27000 KB |
Output is correct |
13 |
Correct |
79 ms |
28024 KB |
Output is correct |
14 |
Correct |
60 ms |
25848 KB |
Output is correct |
15 |
Correct |
46 ms |
24440 KB |
Output is correct |
16 |
Correct |
48 ms |
24568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
21888 KB |
Output is correct |
2 |
Correct |
16 ms |
21888 KB |
Output is correct |
3 |
Correct |
16 ms |
21888 KB |
Output is correct |
4 |
Correct |
17 ms |
21888 KB |
Output is correct |
5 |
Correct |
16 ms |
21888 KB |
Output is correct |
6 |
Correct |
16 ms |
21888 KB |
Output is correct |
7 |
Correct |
16 ms |
21888 KB |
Output is correct |
8 |
Correct |
18 ms |
22016 KB |
Output is correct |
9 |
Correct |
17 ms |
22016 KB |
Output is correct |
10 |
Correct |
40 ms |
24184 KB |
Output is correct |
11 |
Correct |
55 ms |
25080 KB |
Output is correct |
12 |
Correct |
76 ms |
27000 KB |
Output is correct |
13 |
Correct |
79 ms |
28024 KB |
Output is correct |
14 |
Correct |
60 ms |
25848 KB |
Output is correct |
15 |
Correct |
46 ms |
24440 KB |
Output is correct |
16 |
Correct |
48 ms |
24568 KB |
Output is correct |
17 |
Correct |
64 ms |
25208 KB |
Output is correct |
18 |
Correct |
15 ms |
21888 KB |
Output is correct |
19 |
Correct |
16 ms |
21888 KB |
Output is correct |
20 |
Correct |
16 ms |
21888 KB |
Output is correct |
21 |
Correct |
16 ms |
21888 KB |
Output is correct |
22 |
Correct |
15 ms |
21888 KB |
Output is correct |
23 |
Correct |
18 ms |
22016 KB |
Output is correct |
24 |
Correct |
19 ms |
22016 KB |
Output is correct |
25 |
Correct |
34 ms |
22144 KB |
Output is correct |
26 |
Correct |
26 ms |
22144 KB |
Output is correct |
27 |
Correct |
58 ms |
24312 KB |
Output is correct |
28 |
Correct |
77 ms |
27000 KB |
Output is correct |
29 |
Correct |
96 ms |
28152 KB |
Output is correct |
30 |
Correct |
75 ms |
25848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
24440 KB |
Output is correct |
2 |
Correct |
15 ms |
21888 KB |
Output is correct |
3 |
Correct |
15 ms |
21888 KB |
Output is correct |
4 |
Correct |
16 ms |
21888 KB |
Output is correct |
5 |
Correct |
18 ms |
21888 KB |
Output is correct |
6 |
Correct |
17 ms |
21888 KB |
Output is correct |
7 |
Correct |
17 ms |
21888 KB |
Output is correct |
8 |
Correct |
65 ms |
24440 KB |
Output is correct |
9 |
Correct |
62 ms |
24312 KB |
Output is correct |
10 |
Correct |
36 ms |
22016 KB |
Output is correct |
11 |
Correct |
68 ms |
25208 KB |
Output is correct |
12 |
Correct |
16 ms |
21888 KB |
Output is correct |
13 |
Correct |
15 ms |
21888 KB |
Output is correct |
14 |
Correct |
16 ms |
21888 KB |
Output is correct |
15 |
Correct |
15 ms |
21888 KB |
Output is correct |
16 |
Correct |
15 ms |
21888 KB |
Output is correct |
17 |
Correct |
22 ms |
22016 KB |
Output is correct |
18 |
Correct |
24 ms |
22044 KB |
Output is correct |
19 |
Correct |
54 ms |
24320 KB |
Output is correct |
20 |
Correct |
88 ms |
27104 KB |
Output is correct |
21 |
Correct |
70 ms |
25336 KB |
Output is correct |
22 |
Correct |
62 ms |
24824 KB |
Output is correct |
23 |
Correct |
100 ms |
26872 KB |
Output is correct |
24 |
Correct |
57 ms |
25720 KB |
Output is correct |
25 |
Correct |
60 ms |
24568 KB |
Output is correct |
26 |
Correct |
60 ms |
24568 KB |
Output is correct |
27 |
Correct |
16 ms |
21888 KB |
Output is correct |
28 |
Correct |
16 ms |
21888 KB |
Output is correct |
29 |
Correct |
16 ms |
21888 KB |
Output is correct |
30 |
Correct |
17 ms |
21888 KB |
Output is correct |
31 |
Correct |
16 ms |
21888 KB |
Output is correct |
32 |
Correct |
16 ms |
21888 KB |
Output is correct |
33 |
Correct |
16 ms |
21888 KB |
Output is correct |
34 |
Correct |
18 ms |
22016 KB |
Output is correct |
35 |
Correct |
17 ms |
22016 KB |
Output is correct |
36 |
Correct |
40 ms |
24184 KB |
Output is correct |
37 |
Correct |
55 ms |
25080 KB |
Output is correct |
38 |
Correct |
76 ms |
27000 KB |
Output is correct |
39 |
Correct |
79 ms |
28024 KB |
Output is correct |
40 |
Correct |
60 ms |
25848 KB |
Output is correct |
41 |
Correct |
46 ms |
24440 KB |
Output is correct |
42 |
Correct |
48 ms |
24568 KB |
Output is correct |
43 |
Correct |
64 ms |
25208 KB |
Output is correct |
44 |
Correct |
15 ms |
21888 KB |
Output is correct |
45 |
Correct |
16 ms |
21888 KB |
Output is correct |
46 |
Correct |
16 ms |
21888 KB |
Output is correct |
47 |
Correct |
16 ms |
21888 KB |
Output is correct |
48 |
Correct |
15 ms |
21888 KB |
Output is correct |
49 |
Correct |
18 ms |
22016 KB |
Output is correct |
50 |
Correct |
19 ms |
22016 KB |
Output is correct |
51 |
Correct |
34 ms |
22144 KB |
Output is correct |
52 |
Correct |
26 ms |
22144 KB |
Output is correct |
53 |
Correct |
58 ms |
24312 KB |
Output is correct |
54 |
Correct |
77 ms |
27000 KB |
Output is correct |
55 |
Correct |
96 ms |
28152 KB |
Output is correct |
56 |
Correct |
75 ms |
25848 KB |
Output is correct |
57 |
Correct |
159 ms |
28152 KB |
Output is correct |
58 |
Correct |
78 ms |
24440 KB |
Output is correct |
59 |
Correct |
92 ms |
25336 KB |
Output is correct |