/*input
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
*/
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
#define REP(i,n) for(int i=0;i<n;i++)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
#define pii pair<int,int>
#define pb push_back
#define f first
#define ll long long
#define s second
const int INF=0x3f3f3f3f;
const ll INF64=4e18;
struct node{
ll dist[5][5];
}seg[4*maxn];
inline node merge(node a,node b){
node c;
REP(i,5) REP(j,5) c.dist[i][j]=INF64;
REP(k,5){
REP(i,5){
REP(j,5){
MNTO(c.dist[i][j],a.dist[i][k]+b.dist[k][j]);
}
}
}
return c;
}
inline void upd(int idx,int l,int r,int p,int i,int j,int x){
if(r<p or l>p) return;
if(l==r){
seg[idx].dist[i][j]=x;
return;
}
int mid=(l+r)/2;
upd(idx*2,l,mid,p,i,j,x);
upd(idx*2+1,mid+1,r,p,i,j,x);
//seg[idx]=merge(seg[idx*2],seg[idx*2+1]);
}
inline void mrg(int idx,int l,int r){
if(l==r) return;
int mid=(l+r)/2;
mrg(idx*2,l,mid);
mrg(idx*2+1,mid+1,r);
seg[idx]=merge(seg[idx*2],seg[idx*2+1]);
}
inline node query(int idx,int l,int r,int ql,int qr){
//node c;
//REP(i,5) REP(j,5) c.dist[i][j]=0;
//if(r<ql or l>qr) return c;
if(ql<=l and r<=qr) return seg[idx];
int mid=(l+r)/2;
if(mid<ql) return query(idx*2+1,mid+1,r,ql,qr);
if(mid>=qr) return query(idx*2,l,mid,ql,qr);
return merge(query(idx*2,l,mid,ql,qr),query(idx*2+1,mid+1,r,ql,qr));
}
int main(){
ios::sync_with_stdio(false),cin.tie(0);
int k,n,m,q;
cin>>k>>n>>m>>q;
REP(i,4*n) REP(j,5) REP(k,5) seg[i].dist[j][k]=INF64;
REP(i,m){
int a,b,c;
cin>>a>>b>>c;
upd(1,0,(n/k),a/k,a%k,b%k,c);
}
mrg(1,0,(n/k));
REP(i,q){
int a,b;
cin>>a>>b;
if((a/k)>=(b/k)){
cout<<"-1\n";
continue;
}
node z=query(1,0,(n/k),a/k,b/k-1);
ll x=z.dist[a%k][b%k];
if(x==INF64) x=-1;
cout<<x<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
40468 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
4 ms |
1100 KB |
Output is correct |
6 |
Correct |
4 ms |
1100 KB |
Output is correct |
7 |
Correct |
4 ms |
1100 KB |
Output is correct |
8 |
Correct |
97 ms |
40400 KB |
Output is correct |
9 |
Correct |
102 ms |
40340 KB |
Output is correct |
10 |
Correct |
72 ms |
39580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
41092 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
15 ms |
1208 KB |
Output is correct |
8 |
Correct |
14 ms |
1356 KB |
Output is correct |
9 |
Correct |
80 ms |
40336 KB |
Output is correct |
10 |
Correct |
111 ms |
41796 KB |
Output is correct |
11 |
Correct |
96 ms |
41160 KB |
Output is correct |
12 |
Correct |
79 ms |
40720 KB |
Output is correct |
13 |
Correct |
86 ms |
26308 KB |
Output is correct |
14 |
Correct |
61 ms |
25196 KB |
Output is correct |
15 |
Correct |
53 ms |
24936 KB |
Output is correct |
16 |
Correct |
56 ms |
24912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
1100 KB |
Output is correct |
7 |
Correct |
2 ms |
1100 KB |
Output is correct |
8 |
Correct |
3 ms |
1100 KB |
Output is correct |
9 |
Correct |
2 ms |
1108 KB |
Output is correct |
10 |
Correct |
59 ms |
40252 KB |
Output is correct |
11 |
Correct |
92 ms |
40984 KB |
Output is correct |
12 |
Correct |
89 ms |
41612 KB |
Output is correct |
13 |
Correct |
94 ms |
41860 KB |
Output is correct |
14 |
Correct |
75 ms |
41140 KB |
Output is correct |
15 |
Correct |
48 ms |
24928 KB |
Output is correct |
16 |
Correct |
47 ms |
24976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
1100 KB |
Output is correct |
7 |
Correct |
2 ms |
1100 KB |
Output is correct |
8 |
Correct |
3 ms |
1100 KB |
Output is correct |
9 |
Correct |
2 ms |
1108 KB |
Output is correct |
10 |
Correct |
59 ms |
40252 KB |
Output is correct |
11 |
Correct |
92 ms |
40984 KB |
Output is correct |
12 |
Correct |
89 ms |
41612 KB |
Output is correct |
13 |
Correct |
94 ms |
41860 KB |
Output is correct |
14 |
Correct |
75 ms |
41140 KB |
Output is correct |
15 |
Correct |
48 ms |
24928 KB |
Output is correct |
16 |
Correct |
47 ms |
24976 KB |
Output is correct |
17 |
Correct |
83 ms |
40972 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
8 ms |
1100 KB |
Output is correct |
24 |
Correct |
8 ms |
1180 KB |
Output is correct |
25 |
Correct |
8 ms |
1100 KB |
Output is correct |
26 |
Correct |
8 ms |
1100 KB |
Output is correct |
27 |
Correct |
72 ms |
40280 KB |
Output is correct |
28 |
Correct |
104 ms |
41600 KB |
Output is correct |
29 |
Correct |
107 ms |
41860 KB |
Output is correct |
30 |
Correct |
88 ms |
41200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
40468 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
4 ms |
1100 KB |
Output is correct |
6 |
Correct |
4 ms |
1100 KB |
Output is correct |
7 |
Correct |
4 ms |
1100 KB |
Output is correct |
8 |
Correct |
97 ms |
40400 KB |
Output is correct |
9 |
Correct |
102 ms |
40340 KB |
Output is correct |
10 |
Correct |
72 ms |
39580 KB |
Output is correct |
11 |
Correct |
95 ms |
41092 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
15 ms |
1208 KB |
Output is correct |
18 |
Correct |
14 ms |
1356 KB |
Output is correct |
19 |
Correct |
80 ms |
40336 KB |
Output is correct |
20 |
Correct |
111 ms |
41796 KB |
Output is correct |
21 |
Correct |
96 ms |
41160 KB |
Output is correct |
22 |
Correct |
79 ms |
40720 KB |
Output is correct |
23 |
Correct |
86 ms |
26308 KB |
Output is correct |
24 |
Correct |
61 ms |
25196 KB |
Output is correct |
25 |
Correct |
53 ms |
24936 KB |
Output is correct |
26 |
Correct |
56 ms |
24912 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
2 ms |
1100 KB |
Output is correct |
33 |
Correct |
2 ms |
1100 KB |
Output is correct |
34 |
Correct |
3 ms |
1100 KB |
Output is correct |
35 |
Correct |
2 ms |
1108 KB |
Output is correct |
36 |
Correct |
59 ms |
40252 KB |
Output is correct |
37 |
Correct |
92 ms |
40984 KB |
Output is correct |
38 |
Correct |
89 ms |
41612 KB |
Output is correct |
39 |
Correct |
94 ms |
41860 KB |
Output is correct |
40 |
Correct |
75 ms |
41140 KB |
Output is correct |
41 |
Correct |
48 ms |
24928 KB |
Output is correct |
42 |
Correct |
47 ms |
24976 KB |
Output is correct |
43 |
Correct |
83 ms |
40972 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
0 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
0 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
8 ms |
1100 KB |
Output is correct |
50 |
Correct |
8 ms |
1180 KB |
Output is correct |
51 |
Correct |
8 ms |
1100 KB |
Output is correct |
52 |
Correct |
8 ms |
1100 KB |
Output is correct |
53 |
Correct |
72 ms |
40280 KB |
Output is correct |
54 |
Correct |
104 ms |
41600 KB |
Output is correct |
55 |
Correct |
107 ms |
41860 KB |
Output is correct |
56 |
Correct |
88 ms |
41200 KB |
Output is correct |
57 |
Correct |
158 ms |
42768 KB |
Output is correct |
58 |
Correct |
99 ms |
40364 KB |
Output is correct |
59 |
Correct |
112 ms |
41244 KB |
Output is correct |