#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 u ; 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)
{
if (u+getnum(i)>=n) break;
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 |
58 ms |
40236 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
48 ms |
40240 KB |
Output is correct |
9 |
Correct |
47 ms |
40268 KB |
Output is correct |
10 |
Correct |
34 ms |
40252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
40224 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
340 KB |
Output is correct |
7 |
Correct |
4 ms |
1108 KB |
Output is correct |
8 |
Correct |
5 ms |
1220 KB |
Output is correct |
9 |
Correct |
45 ms |
40240 KB |
Output is correct |
10 |
Correct |
81 ms |
40244 KB |
Output is correct |
11 |
Correct |
67 ms |
40240 KB |
Output is correct |
12 |
Correct |
64 ms |
40268 KB |
Output is correct |
13 |
Correct |
70 ms |
24268 KB |
Output is correct |
14 |
Correct |
46 ms |
24276 KB |
Output is correct |
15 |
Correct |
53 ms |
24260 KB |
Output is correct |
16 |
Correct |
51 ms |
24300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
2 ms |
1108 KB |
Output is correct |
9 |
Correct |
2 ms |
1108 KB |
Output is correct |
10 |
Correct |
41 ms |
40232 KB |
Output is correct |
11 |
Correct |
55 ms |
40228 KB |
Output is correct |
12 |
Correct |
72 ms |
40240 KB |
Output is correct |
13 |
Correct |
82 ms |
40244 KB |
Output is correct |
14 |
Correct |
73 ms |
40488 KB |
Output is correct |
15 |
Correct |
50 ms |
24276 KB |
Output is correct |
16 |
Correct |
52 ms |
24188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
2 ms |
1108 KB |
Output is correct |
9 |
Correct |
2 ms |
1108 KB |
Output is correct |
10 |
Correct |
41 ms |
40232 KB |
Output is correct |
11 |
Correct |
55 ms |
40228 KB |
Output is correct |
12 |
Correct |
72 ms |
40240 KB |
Output is correct |
13 |
Correct |
82 ms |
40244 KB |
Output is correct |
14 |
Correct |
73 ms |
40488 KB |
Output is correct |
15 |
Correct |
50 ms |
24276 KB |
Output is correct |
16 |
Correct |
52 ms |
24188 KB |
Output is correct |
17 |
Correct |
60 ms |
40276 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
1108 KB |
Output is correct |
24 |
Correct |
2 ms |
1108 KB |
Output is correct |
25 |
Correct |
3 ms |
1108 KB |
Output is correct |
26 |
Correct |
5 ms |
1108 KB |
Output is correct |
27 |
Correct |
50 ms |
40148 KB |
Output is correct |
28 |
Correct |
80 ms |
40232 KB |
Output is correct |
29 |
Correct |
82 ms |
40232 KB |
Output is correct |
30 |
Correct |
75 ms |
40244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
40236 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
48 ms |
40240 KB |
Output is correct |
9 |
Correct |
47 ms |
40268 KB |
Output is correct |
10 |
Correct |
34 ms |
40252 KB |
Output is correct |
11 |
Correct |
67 ms |
40224 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
4 ms |
1108 KB |
Output is correct |
18 |
Correct |
5 ms |
1220 KB |
Output is correct |
19 |
Correct |
45 ms |
40240 KB |
Output is correct |
20 |
Correct |
81 ms |
40244 KB |
Output is correct |
21 |
Correct |
67 ms |
40240 KB |
Output is correct |
22 |
Correct |
64 ms |
40268 KB |
Output is correct |
23 |
Correct |
70 ms |
24268 KB |
Output is correct |
24 |
Correct |
46 ms |
24276 KB |
Output is correct |
25 |
Correct |
53 ms |
24260 KB |
Output is correct |
26 |
Correct |
51 ms |
24300 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 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 |
1 ms |
1108 KB |
Output is correct |
34 |
Correct |
2 ms |
1108 KB |
Output is correct |
35 |
Correct |
2 ms |
1108 KB |
Output is correct |
36 |
Correct |
41 ms |
40232 KB |
Output is correct |
37 |
Correct |
55 ms |
40228 KB |
Output is correct |
38 |
Correct |
72 ms |
40240 KB |
Output is correct |
39 |
Correct |
82 ms |
40244 KB |
Output is correct |
40 |
Correct |
73 ms |
40488 KB |
Output is correct |
41 |
Correct |
50 ms |
24276 KB |
Output is correct |
42 |
Correct |
52 ms |
24188 KB |
Output is correct |
43 |
Correct |
60 ms |
40276 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
2 ms |
1108 KB |
Output is correct |
50 |
Correct |
2 ms |
1108 KB |
Output is correct |
51 |
Correct |
3 ms |
1108 KB |
Output is correct |
52 |
Correct |
5 ms |
1108 KB |
Output is correct |
53 |
Correct |
50 ms |
40148 KB |
Output is correct |
54 |
Correct |
80 ms |
40232 KB |
Output is correct |
55 |
Correct |
82 ms |
40232 KB |
Output is correct |
56 |
Correct |
75 ms |
40244 KB |
Output is correct |
57 |
Correct |
116 ms |
40360 KB |
Output is correct |
58 |
Correct |
51 ms |
40232 KB |
Output is correct |
59 |
Correct |
66 ms |
40244 KB |
Output is correct |