#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0)
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC target("popcnt")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MAX=2e5+10,P=1e9+7;
const ll INF=0x3f3f3f3f,oo=0x3f3f3f3f3f3f3f3f;
const ldb eps=1e-6;
const ldb PI=acos(-1.0);
const int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
template<typename T>
using vvector = vector<vector<T>>;
int main() {
speed;
int n,m,k,q;
cin>>k>>n>>m>>q;
vvector<ll> G(n,vector<ll>(k,oo));
for (int i=0;i<m;i++) {
ll a,b,w;
cin>>a>>b>>w;
G[a][b%k]=min(G[a][b%k],w);
}
ll spt[n/k+10][k][k][20];
for (int i=n-1;i>=0;i--) {
int ik=i/k;
int ir=i%k;
for (int j=0;j<k;j++) spt[ik][ir][j][0]=G[i][j];
for (int l=1;l<20;l++) {
for (int j=0;j<k;j++) {
spt[ik][ir][j][l]=oo;
if (ik+(1<<l)<=(n-1)/k) {
for (int mid=0;mid<k;mid++) {
spt[ik][ir][j][l]=min(spt[ik][ir][j][l],spt[ik][ir][mid][l-1]+spt[ik+(1<<(l-1))][mid][j][l-1]);
// cout<<mid<<" "<<spt[ik][ir][mid][l-1]<<" "<<spt[ik+(1<<(l-1))][mid][j][l-1]<<" mid spt\n";
}
// cout<<ik<<" "<<ir<<" "<<j<<" "<<l<<" "<<spt[ik][ir][j][l]<<" ik ir j l spt\n";
}
}
}
}
while (q--) {
int a,b;
cin>>a>>b;
int ia=a/k;
int ra=a%k;
int ib=b/k;
int rb=b%k;
int x=ia;
ll md[k];
for (int i=0;i<k;i++) md[i]=oo;
md[ra]=0;
for (int l=19;l>=0;l--) {
// cout<<l<<" l\n";
if (ib-x>=(1<<l)) {
ll nmd[k];
memset(nmd,0x3f,sizeof(nmd));
for (int i=0;i<k;i++) {
for (int j=0;j<k;j++) {
nmd[j]=min(nmd[j],md[i]+spt[x][i][j][l]);
}
}
x+=(1<<l);
for (int i=0;i< k;i++) md[i]=nmd[i];
// cout<<x<<" x\n";
// for (int i=0;i<k;i++) cout<<md[i]<<" ";
// cout<<"\n";
}
}
cout<<(md[rb]>=oo?-1:md[rb])<<"\n";
}
return 0;
}
/*
3 10 9 1
0 3 1
3 6 2
6 9 3
0 4 2
4 7 1
7 9 4
0 5 4
5 8 3
8 9 2
3 9
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
11088 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
26 ms |
10832 KB |
Output is correct |
9 |
Correct |
27 ms |
10940 KB |
Output is correct |
10 |
Correct |
20 ms |
10832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
18768 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
4 ms |
736 KB |
Output is correct |
8 |
Correct |
4 ms |
848 KB |
Output is correct |
9 |
Correct |
24 ms |
11764 KB |
Output is correct |
10 |
Correct |
60 ms |
28776 KB |
Output is correct |
11 |
Correct |
46 ms |
20528 KB |
Output is correct |
12 |
Correct |
49 ms |
27976 KB |
Output is correct |
13 |
Correct |
60 ms |
28500 KB |
Output is correct |
14 |
Correct |
35 ms |
17480 KB |
Output is correct |
15 |
Correct |
48 ms |
27208 KB |
Output is correct |
16 |
Correct |
48 ms |
27212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
732 KB |
Output is correct |
8 |
Correct |
2 ms |
1360 KB |
Output is correct |
9 |
Correct |
2 ms |
984 KB |
Output is correct |
10 |
Correct |
22 ms |
11600 KB |
Output is correct |
11 |
Correct |
38 ms |
20336 KB |
Output is correct |
12 |
Correct |
55 ms |
28744 KB |
Output is correct |
13 |
Correct |
58 ms |
29036 KB |
Output is correct |
14 |
Correct |
52 ms |
28268 KB |
Output is correct |
15 |
Correct |
47 ms |
27216 KB |
Output is correct |
16 |
Correct |
47 ms |
27208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
732 KB |
Output is correct |
8 |
Correct |
2 ms |
1360 KB |
Output is correct |
9 |
Correct |
2 ms |
984 KB |
Output is correct |
10 |
Correct |
22 ms |
11600 KB |
Output is correct |
11 |
Correct |
38 ms |
20336 KB |
Output is correct |
12 |
Correct |
55 ms |
28744 KB |
Output is correct |
13 |
Correct |
58 ms |
29036 KB |
Output is correct |
14 |
Correct |
52 ms |
28268 KB |
Output is correct |
15 |
Correct |
47 ms |
27216 KB |
Output is correct |
16 |
Correct |
47 ms |
27208 KB |
Output is correct |
17 |
Correct |
45 ms |
20336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
848 KB |
Output is correct |
25 |
Correct |
3 ms |
1360 KB |
Output is correct |
26 |
Correct |
3 ms |
1104 KB |
Output is correct |
27 |
Correct |
25 ms |
11632 KB |
Output is correct |
28 |
Correct |
63 ms |
28744 KB |
Output is correct |
29 |
Correct |
68 ms |
29012 KB |
Output is correct |
30 |
Correct |
64 ms |
28524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
11088 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
26 ms |
10832 KB |
Output is correct |
9 |
Correct |
27 ms |
10940 KB |
Output is correct |
10 |
Correct |
20 ms |
10832 KB |
Output is correct |
11 |
Correct |
41 ms |
18768 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
4 ms |
736 KB |
Output is correct |
18 |
Correct |
4 ms |
848 KB |
Output is correct |
19 |
Correct |
24 ms |
11764 KB |
Output is correct |
20 |
Correct |
60 ms |
28776 KB |
Output is correct |
21 |
Correct |
46 ms |
20528 KB |
Output is correct |
22 |
Correct |
49 ms |
27976 KB |
Output is correct |
23 |
Correct |
60 ms |
28500 KB |
Output is correct |
24 |
Correct |
35 ms |
17480 KB |
Output is correct |
25 |
Correct |
48 ms |
27208 KB |
Output is correct |
26 |
Correct |
48 ms |
27212 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
732 KB |
Output is correct |
34 |
Correct |
2 ms |
1360 KB |
Output is correct |
35 |
Correct |
2 ms |
984 KB |
Output is correct |
36 |
Correct |
22 ms |
11600 KB |
Output is correct |
37 |
Correct |
38 ms |
20336 KB |
Output is correct |
38 |
Correct |
55 ms |
28744 KB |
Output is correct |
39 |
Correct |
58 ms |
29036 KB |
Output is correct |
40 |
Correct |
52 ms |
28268 KB |
Output is correct |
41 |
Correct |
47 ms |
27216 KB |
Output is correct |
42 |
Correct |
47 ms |
27208 KB |
Output is correct |
43 |
Correct |
45 ms |
20336 KB |
Output is correct |
44 |
Correct |
1 ms |
336 KB |
Output is correct |
45 |
Correct |
1 ms |
336 KB |
Output is correct |
46 |
Correct |
1 ms |
336 KB |
Output is correct |
47 |
Correct |
1 ms |
336 KB |
Output is correct |
48 |
Correct |
1 ms |
336 KB |
Output is correct |
49 |
Correct |
2 ms |
592 KB |
Output is correct |
50 |
Correct |
2 ms |
848 KB |
Output is correct |
51 |
Correct |
3 ms |
1360 KB |
Output is correct |
52 |
Correct |
3 ms |
1104 KB |
Output is correct |
53 |
Correct |
25 ms |
11632 KB |
Output is correct |
54 |
Correct |
63 ms |
28744 KB |
Output is correct |
55 |
Correct |
68 ms |
29012 KB |
Output is correct |
56 |
Correct |
64 ms |
28524 KB |
Output is correct |
57 |
Correct |
99 ms |
38484 KB |
Output is correct |
58 |
Correct |
26 ms |
11856 KB |
Output is correct |
59 |
Correct |
45 ms |
20552 KB |
Output is correct |