#include<bits/stdc++.h>
using namespace std;
int k,n,m,o;
constexpr int inf=1000000000;
struct Mat{
vector<vector<int>>a;
Mat(){
a.assign(k,vector<int>(k,inf));
}
Mat(const vector<vector<int>>&b):a(b){}
friend Mat operator*(const Mat&a,const Mat&b){
Mat c=Mat();
for(int i=0;i<k;i++)for(int j=0;j<k;j++)for(int l=0;l<k;l++)c.a[i][j]=min(c.a[i][j],a.a[i][l]+b.a[l][j]);
return c;
}
static const Mat e(){
Mat c=Mat();
for(int i=0;i<k;i++)c.a[i][i]=0;
return c;
}
};
struct segtree{
private:
int n;
vector<Mat>seg;
public:
segtree(int n):n(n){seg.assign(2*n,Mat::e());}
segtree(const vector<Mat>&v){
n=v.size();
seg.assign(2*n,Mat::e());
for(int i=0;i<n;i++)seg[i+n]=v[i];
for(int i=n-1;i>0;i--)seg[i]=seg[i<<1]*seg[i<<1|1];
}
Mat prod(int l,int r)const{
Mat sml=Mat::e(),smr=Mat::e();
for(l+=n,r+=n;l<r;l>>=1,r>>=1){
if(l&1)sml=sml*seg[l++];
if(r&1)smr=seg[--r]*smr;
}
return sml*smr;
}
};
int main(){
cin>>k>>n>>m>>o;
vector<Mat>mat(n/k);
for(int i=0;i<m;i++){
int a,b,c;cin>>a>>b>>c;
mat[a/k].a[a%k][b%k]=min(mat[a/k].a[a%k][b%k],c);
}
segtree seg(mat);
while(o--){
int a,b;cin>>a>>b;
auto res=seg.prod(a/k,b/k);
//for(int i=0;i<k;i++)for(int j=0;j<k;j++)cout<<res.a[i][j]<<" \n"[j==k-1];
int ans=res.a[a%k][b%k];
if(ans==inf)ans=-1;
cout<<ans<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
14184 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
4 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
468 KB |
Output is correct |
7 |
Correct |
7 ms |
468 KB |
Output is correct |
8 |
Correct |
96 ms |
14164 KB |
Output is correct |
9 |
Correct |
98 ms |
14108 KB |
Output is correct |
10 |
Correct |
71 ms |
13360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
13076 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
27 ms |
724 KB |
Output is correct |
8 |
Correct |
30 ms |
596 KB |
Output is correct |
9 |
Correct |
115 ms |
14108 KB |
Output is correct |
10 |
Correct |
162 ms |
12336 KB |
Output is correct |
11 |
Correct |
138 ms |
13220 KB |
Output is correct |
12 |
Correct |
152 ms |
11388 KB |
Output is correct |
13 |
Correct |
173 ms |
8208 KB |
Output is correct |
14 |
Correct |
87 ms |
7524 KB |
Output is correct |
15 |
Correct |
91 ms |
6988 KB |
Output is correct |
16 |
Correct |
100 ms |
7016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
4 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
66 ms |
13936 KB |
Output is correct |
11 |
Correct |
106 ms |
12948 KB |
Output is correct |
12 |
Correct |
132 ms |
12236 KB |
Output is correct |
13 |
Correct |
138 ms |
12488 KB |
Output is correct |
14 |
Correct |
112 ms |
11788 KB |
Output is correct |
15 |
Correct |
86 ms |
6860 KB |
Output is correct |
16 |
Correct |
76 ms |
6972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
4 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
66 ms |
13936 KB |
Output is correct |
11 |
Correct |
106 ms |
12948 KB |
Output is correct |
12 |
Correct |
132 ms |
12236 KB |
Output is correct |
13 |
Correct |
138 ms |
12488 KB |
Output is correct |
14 |
Correct |
112 ms |
11788 KB |
Output is correct |
15 |
Correct |
86 ms |
6860 KB |
Output is correct |
16 |
Correct |
76 ms |
6972 KB |
Output is correct |
17 |
Correct |
103 ms |
12948 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
296 KB |
Output is correct |
23 |
Correct |
10 ms |
612 KB |
Output is correct |
24 |
Correct |
10 ms |
564 KB |
Output is correct |
25 |
Correct |
17 ms |
568 KB |
Output is correct |
26 |
Correct |
15 ms |
572 KB |
Output is correct |
27 |
Correct |
71 ms |
14056 KB |
Output is correct |
28 |
Correct |
148 ms |
12244 KB |
Output is correct |
29 |
Correct |
154 ms |
12500 KB |
Output is correct |
30 |
Correct |
138 ms |
11932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
14184 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
4 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
468 KB |
Output is correct |
7 |
Correct |
7 ms |
468 KB |
Output is correct |
8 |
Correct |
96 ms |
14164 KB |
Output is correct |
9 |
Correct |
98 ms |
14108 KB |
Output is correct |
10 |
Correct |
71 ms |
13360 KB |
Output is correct |
11 |
Correct |
132 ms |
13076 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
27 ms |
724 KB |
Output is correct |
18 |
Correct |
30 ms |
596 KB |
Output is correct |
19 |
Correct |
115 ms |
14108 KB |
Output is correct |
20 |
Correct |
162 ms |
12336 KB |
Output is correct |
21 |
Correct |
138 ms |
13220 KB |
Output is correct |
22 |
Correct |
152 ms |
11388 KB |
Output is correct |
23 |
Correct |
173 ms |
8208 KB |
Output is correct |
24 |
Correct |
87 ms |
7524 KB |
Output is correct |
25 |
Correct |
91 ms |
6988 KB |
Output is correct |
26 |
Correct |
100 ms |
7016 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
300 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
468 KB |
Output is correct |
34 |
Correct |
4 ms |
468 KB |
Output is correct |
35 |
Correct |
3 ms |
468 KB |
Output is correct |
36 |
Correct |
66 ms |
13936 KB |
Output is correct |
37 |
Correct |
106 ms |
12948 KB |
Output is correct |
38 |
Correct |
132 ms |
12236 KB |
Output is correct |
39 |
Correct |
138 ms |
12488 KB |
Output is correct |
40 |
Correct |
112 ms |
11788 KB |
Output is correct |
41 |
Correct |
86 ms |
6860 KB |
Output is correct |
42 |
Correct |
76 ms |
6972 KB |
Output is correct |
43 |
Correct |
103 ms |
12948 KB |
Output is correct |
44 |
Correct |
1 ms |
304 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
296 KB |
Output is correct |
49 |
Correct |
10 ms |
612 KB |
Output is correct |
50 |
Correct |
10 ms |
564 KB |
Output is correct |
51 |
Correct |
17 ms |
568 KB |
Output is correct |
52 |
Correct |
15 ms |
572 KB |
Output is correct |
53 |
Correct |
71 ms |
14056 KB |
Output is correct |
54 |
Correct |
148 ms |
12244 KB |
Output is correct |
55 |
Correct |
154 ms |
12500 KB |
Output is correct |
56 |
Correct |
138 ms |
11932 KB |
Output is correct |
57 |
Correct |
251 ms |
13240 KB |
Output is correct |
58 |
Correct |
98 ms |
14140 KB |
Output is correct |
59 |
Correct |
138 ms |
13132 KB |
Output is correct |