#include <bits/stdc++.h>
using namespace std;
#define forw(i,a,b) for(ll i=a;i<=b;i++)
#define forb(i,a,b) for(ll i=a;i>=b;i--)
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(),a.end()
#define getbit(mask,i) ((mask>>i)&1)
#define getnum(i) (1<<(i))
#define minimize(a,b) ()
typedef long long int ll;
const ll maxN=1e6+5;
const ll mod=1e9+7;
const ll oo=1e18;
ll k,n,m,q,d[maxN][17][6],tmp[6],ans[6];
void solve()
{
cin>>k>>n>>m>>q;
// init
// d[u][i][v] : position i ; go through 2^i blocks ; destination k*(u/k+2^i) + v
forw(u,0,n-1)
forw(i,0,16)
forw(v,0,k-1)
d[u][i][v]=oo;
// read
forw(i,1,m)
{
int u,v,val;cin>>u>>v>>val;
d[u][0][v%k]=val;
}
// sparse
forb(u,n-1,0)
{
forw(i,1,16)
forw(pos,0,k-1)
forw(v,0,k-1)
d[u][i][v]=min(d[u][i][v],d[u][i-1][pos]+d[pos+k*(u/k+getnum(i-1))][i-1][v]);
}
// query
while (q--)
{
int l,r; cin>>l>>r;
int blocks=r/k - l/k; blocks--;
forw(i,0,k-1) ans[i]=d[l][0][i];
int cnt=l/k+1;
forw(i,0,16)
if (blocks & getnum(i))
{
forw(u,0,k-1) tmp[u]=oo;
forw(u,0,k-1)
forw(v,0,k-1)
tmp[v]=min(tmp[v],ans[u]+d[u+cnt*k][i][v]);
forw(u,0,k-1) ans[u]=tmp[u];
cnt+=getnum(i);
}
if (ans[r%k]==oo) cout<<-1<<"\n";
else cout<<ans[r%k]<<"\n";
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
//freopen("bruh.inp","r",stdin);
//freopen("bruh.out","w",stdout);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
40280 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
1108 KB |
Output is correct |
6 |
Correct |
2 ms |
1108 KB |
Output is correct |
7 |
Correct |
2 ms |
1108 KB |
Output is correct |
8 |
Correct |
50 ms |
40284 KB |
Output is correct |
9 |
Correct |
49 ms |
40232 KB |
Output is correct |
10 |
Correct |
36 ms |
40276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
40360 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
6 ms |
1172 KB |
Output is correct |
8 |
Correct |
5 ms |
1148 KB |
Output is correct |
9 |
Correct |
55 ms |
40284 KB |
Output is correct |
10 |
Correct |
86 ms |
40364 KB |
Output is correct |
11 |
Correct |
84 ms |
40380 KB |
Output is correct |
12 |
Correct |
74 ms |
40396 KB |
Output is correct |
13 |
Correct |
89 ms |
24372 KB |
Output is correct |
14 |
Correct |
56 ms |
24404 KB |
Output is correct |
15 |
Correct |
70 ms |
24448 KB |
Output is correct |
16 |
Correct |
65 ms |
24400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
2 ms |
1108 KB |
Output is correct |
8 |
Correct |
4 ms |
1108 KB |
Output is correct |
9 |
Correct |
2 ms |
1108 KB |
Output is correct |
10 |
Correct |
45 ms |
40264 KB |
Output is correct |
11 |
Correct |
63 ms |
40328 KB |
Output is correct |
12 |
Correct |
83 ms |
40388 KB |
Output is correct |
13 |
Correct |
98 ms |
40328 KB |
Output is correct |
14 |
Correct |
81 ms |
40352 KB |
Output is correct |
15 |
Correct |
65 ms |
24384 KB |
Output is correct |
16 |
Correct |
76 ms |
24384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
2 ms |
1108 KB |
Output is correct |
8 |
Correct |
4 ms |
1108 KB |
Output is correct |
9 |
Correct |
2 ms |
1108 KB |
Output is correct |
10 |
Correct |
45 ms |
40264 KB |
Output is correct |
11 |
Correct |
63 ms |
40328 KB |
Output is correct |
12 |
Correct |
83 ms |
40388 KB |
Output is correct |
13 |
Correct |
98 ms |
40328 KB |
Output is correct |
14 |
Correct |
81 ms |
40352 KB |
Output is correct |
15 |
Correct |
65 ms |
24384 KB |
Output is correct |
16 |
Correct |
76 ms |
24384 KB |
Output is correct |
17 |
Correct |
68 ms |
40348 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
1 ms |
328 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
4 ms |
1108 KB |
Output is correct |
24 |
Correct |
4 ms |
1108 KB |
Output is correct |
25 |
Correct |
6 ms |
1272 KB |
Output is correct |
26 |
Correct |
4 ms |
1236 KB |
Output is correct |
27 |
Correct |
57 ms |
41124 KB |
Output is correct |
28 |
Correct |
97 ms |
42580 KB |
Output is correct |
29 |
Correct |
92 ms |
42828 KB |
Output is correct |
30 |
Correct |
87 ms |
42360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
40280 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
1108 KB |
Output is correct |
6 |
Correct |
2 ms |
1108 KB |
Output is correct |
7 |
Correct |
2 ms |
1108 KB |
Output is correct |
8 |
Correct |
50 ms |
40284 KB |
Output is correct |
9 |
Correct |
49 ms |
40232 KB |
Output is correct |
10 |
Correct |
36 ms |
40276 KB |
Output is correct |
11 |
Correct |
73 ms |
40360 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
6 ms |
1172 KB |
Output is correct |
18 |
Correct |
5 ms |
1148 KB |
Output is correct |
19 |
Correct |
55 ms |
40284 KB |
Output is correct |
20 |
Correct |
86 ms |
40364 KB |
Output is correct |
21 |
Correct |
84 ms |
40380 KB |
Output is correct |
22 |
Correct |
74 ms |
40396 KB |
Output is correct |
23 |
Correct |
89 ms |
24372 KB |
Output is correct |
24 |
Correct |
56 ms |
24404 KB |
Output is correct |
25 |
Correct |
70 ms |
24448 KB |
Output is correct |
26 |
Correct |
65 ms |
24400 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
1108 KB |
Output is correct |
33 |
Correct |
2 ms |
1108 KB |
Output is correct |
34 |
Correct |
4 ms |
1108 KB |
Output is correct |
35 |
Correct |
2 ms |
1108 KB |
Output is correct |
36 |
Correct |
45 ms |
40264 KB |
Output is correct |
37 |
Correct |
63 ms |
40328 KB |
Output is correct |
38 |
Correct |
83 ms |
40388 KB |
Output is correct |
39 |
Correct |
98 ms |
40328 KB |
Output is correct |
40 |
Correct |
81 ms |
40352 KB |
Output is correct |
41 |
Correct |
65 ms |
24384 KB |
Output is correct |
42 |
Correct |
76 ms |
24384 KB |
Output is correct |
43 |
Correct |
68 ms |
40348 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
328 KB |
Output is correct |
46 |
Correct |
1 ms |
320 KB |
Output is correct |
47 |
Correct |
1 ms |
328 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
4 ms |
1108 KB |
Output is correct |
50 |
Correct |
4 ms |
1108 KB |
Output is correct |
51 |
Correct |
6 ms |
1272 KB |
Output is correct |
52 |
Correct |
4 ms |
1236 KB |
Output is correct |
53 |
Correct |
57 ms |
41124 KB |
Output is correct |
54 |
Correct |
97 ms |
42580 KB |
Output is correct |
55 |
Correct |
92 ms |
42828 KB |
Output is correct |
56 |
Correct |
87 ms |
42360 KB |
Output is correct |
57 |
Correct |
150 ms |
43724 KB |
Output is correct |
58 |
Correct |
55 ms |
41204 KB |
Output is correct |
59 |
Correct |
78 ms |
42136 KB |
Output is correct |