#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> bool cmin(T &i, T j) { return i > j ? i=j,true:false; }
template<class T> bool cmax(T &i, T j) { return i < j ? i=j,true:false; }
mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
int rng() {
return abs((int)mrand());
}
// Current challenge: finish CSES problemset!!
constexpr int nax = 50500;
constexpr ll inf = 1e18;
ll c[nax][5][5];
int n,M,q,k;
int L[nax],R[nax]; // queries
ll ans[nax];
ll lef[nax][5][5],rig[nax][5][5];
void solve(int l,int r,vector<int> &v) {
if (l == r) {
return;
}
for (int i=l;i<=r;i++) {
for (int j=0;j<k;j++) {
for (int z=0;z<k;z++) {
lef[i][j][z] = rig[i][j][z] = inf;
}
}
}
int m = (l + r) / 2;
for (int i=0;i<k;i++) lef[m][i][i] = 0;
for (int w=0;w<k;w++) {
for (int i=m-1;i>=l;i--) {
for (int j=0;j<k;j++) {
for (int z=0;z<k;z++) {
cmin(lef[i][j][w],lef[i+1][z][w]+c[i][j][z]);
}
}
}
}
for (int i=0;i<k;i++) rig[m+1][i][i] = 0;
for (int w=0;w<k;w++) {
for (int i=m+1;i<r;i++) {
for (int j=0;j<k;j++) {
for (int z=0;z<k;z++) {
cmin(rig[i+1][w][z],rig[i][w][j]+c[i][j][z]);
}
}
}
}
vector<vector<int>> vv(2);
for (int &idx: v) {
int lg = L[idx] / k, rg = R[idx] / k;
int lk = L[idx] % k, rk = R[idx] % k;
if (lg <= m && m < rg) {
ll res = inf;
for (int j=0;j<k;j++) {
for (int z=0;z<k;z++) {
cmin(res,lef[lg][lk][j]+c[m][j][z]+rig[rg][z][rk]);
}
}
ans[idx] = res;
} else {
vv[lg > m].push_back(idx);
}
}
solve(l,m,vv[0]);
solve(m+1,r,vv[1]);
}
void solve() {
cin >> k >> n >> M >> q;
memset(c,0x3f,sizeof(c));
memset(ans,0x3f,sizeof(ans));
for (int x,y;M--;) {
cin >> x >> y;
cin >> c[x/k][x%k][y%k];
}
for (int i=0;i<q;i++) {
cin >> L[i] >> R[i];
}
vector<int> v(q);
iota(v.begin(),v.end(),0);
solve(0,(n-1)/k,v);
for (int i=0;i<q;i++) {
cout << (ans[i] > (int)1e9 ? -1 : ans[i]) << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int _t = 1;
// cin >> _t;
for (int i=1;i<=_t;i++) {
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
31316 KB |
Output is correct |
2 |
Correct |
4 ms |
10588 KB |
Output is correct |
3 |
Correct |
4 ms |
10588 KB |
Output is correct |
4 |
Correct |
4 ms |
10588 KB |
Output is correct |
5 |
Correct |
5 ms |
11032 KB |
Output is correct |
6 |
Correct |
5 ms |
11032 KB |
Output is correct |
7 |
Correct |
4 ms |
11352 KB |
Output is correct |
8 |
Correct |
33 ms |
31320 KB |
Output is correct |
9 |
Correct |
34 ms |
31316 KB |
Output is correct |
10 |
Correct |
20 ms |
30556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
22368 KB |
Output is correct |
2 |
Correct |
4 ms |
10584 KB |
Output is correct |
3 |
Correct |
5 ms |
10584 KB |
Output is correct |
4 |
Correct |
4 ms |
10588 KB |
Output is correct |
5 |
Correct |
4 ms |
10584 KB |
Output is correct |
6 |
Correct |
4 ms |
10840 KB |
Output is correct |
7 |
Correct |
6 ms |
11356 KB |
Output is correct |
8 |
Correct |
7 ms |
11100 KB |
Output is correct |
9 |
Correct |
30 ms |
31324 KB |
Output is correct |
10 |
Correct |
49 ms |
19540 KB |
Output is correct |
11 |
Correct |
39 ms |
22332 KB |
Output is correct |
12 |
Correct |
31 ms |
18524 KB |
Output is correct |
13 |
Correct |
47 ms |
15444 KB |
Output is correct |
14 |
Correct |
29 ms |
15952 KB |
Output is correct |
15 |
Correct |
37 ms |
14364 KB |
Output is correct |
16 |
Correct |
36 ms |
14172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
10588 KB |
Output is correct |
2 |
Correct |
4 ms |
10584 KB |
Output is correct |
3 |
Correct |
4 ms |
10704 KB |
Output is correct |
4 |
Correct |
4 ms |
10588 KB |
Output is correct |
5 |
Correct |
4 ms |
10588 KB |
Output is correct |
6 |
Correct |
4 ms |
11100 KB |
Output is correct |
7 |
Correct |
4 ms |
10844 KB |
Output is correct |
8 |
Correct |
5 ms |
10844 KB |
Output is correct |
9 |
Correct |
5 ms |
10840 KB |
Output is correct |
10 |
Correct |
27 ms |
30960 KB |
Output is correct |
11 |
Correct |
35 ms |
22052 KB |
Output is correct |
12 |
Correct |
49 ms |
19280 KB |
Output is correct |
13 |
Correct |
50 ms |
19540 KB |
Output is correct |
14 |
Correct |
41 ms |
19036 KB |
Output is correct |
15 |
Correct |
40 ms |
14180 KB |
Output is correct |
16 |
Correct |
43 ms |
14164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
10588 KB |
Output is correct |
2 |
Correct |
4 ms |
10584 KB |
Output is correct |
3 |
Correct |
4 ms |
10704 KB |
Output is correct |
4 |
Correct |
4 ms |
10588 KB |
Output is correct |
5 |
Correct |
4 ms |
10588 KB |
Output is correct |
6 |
Correct |
4 ms |
11100 KB |
Output is correct |
7 |
Correct |
4 ms |
10844 KB |
Output is correct |
8 |
Correct |
5 ms |
10844 KB |
Output is correct |
9 |
Correct |
5 ms |
10840 KB |
Output is correct |
10 |
Correct |
27 ms |
30960 KB |
Output is correct |
11 |
Correct |
35 ms |
22052 KB |
Output is correct |
12 |
Correct |
49 ms |
19280 KB |
Output is correct |
13 |
Correct |
50 ms |
19540 KB |
Output is correct |
14 |
Correct |
41 ms |
19036 KB |
Output is correct |
15 |
Correct |
40 ms |
14180 KB |
Output is correct |
16 |
Correct |
43 ms |
14164 KB |
Output is correct |
17 |
Correct |
36 ms |
22108 KB |
Output is correct |
18 |
Correct |
5 ms |
10584 KB |
Output is correct |
19 |
Correct |
5 ms |
10588 KB |
Output is correct |
20 |
Correct |
4 ms |
10588 KB |
Output is correct |
21 |
Correct |
4 ms |
10588 KB |
Output is correct |
22 |
Correct |
4 ms |
10588 KB |
Output is correct |
23 |
Correct |
5 ms |
11024 KB |
Output is correct |
24 |
Correct |
5 ms |
10844 KB |
Output is correct |
25 |
Correct |
8 ms |
10964 KB |
Output is correct |
26 |
Correct |
5 ms |
10844 KB |
Output is correct |
27 |
Correct |
28 ms |
30944 KB |
Output is correct |
28 |
Correct |
46 ms |
19296 KB |
Output is correct |
29 |
Correct |
47 ms |
19544 KB |
Output is correct |
30 |
Correct |
46 ms |
19036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
31316 KB |
Output is correct |
2 |
Correct |
4 ms |
10588 KB |
Output is correct |
3 |
Correct |
4 ms |
10588 KB |
Output is correct |
4 |
Correct |
4 ms |
10588 KB |
Output is correct |
5 |
Correct |
5 ms |
11032 KB |
Output is correct |
6 |
Correct |
5 ms |
11032 KB |
Output is correct |
7 |
Correct |
4 ms |
11352 KB |
Output is correct |
8 |
Correct |
33 ms |
31320 KB |
Output is correct |
9 |
Correct |
34 ms |
31316 KB |
Output is correct |
10 |
Correct |
20 ms |
30556 KB |
Output is correct |
11 |
Correct |
41 ms |
22368 KB |
Output is correct |
12 |
Correct |
4 ms |
10584 KB |
Output is correct |
13 |
Correct |
5 ms |
10584 KB |
Output is correct |
14 |
Correct |
4 ms |
10588 KB |
Output is correct |
15 |
Correct |
4 ms |
10584 KB |
Output is correct |
16 |
Correct |
4 ms |
10840 KB |
Output is correct |
17 |
Correct |
6 ms |
11356 KB |
Output is correct |
18 |
Correct |
7 ms |
11100 KB |
Output is correct |
19 |
Correct |
30 ms |
31324 KB |
Output is correct |
20 |
Correct |
49 ms |
19540 KB |
Output is correct |
21 |
Correct |
39 ms |
22332 KB |
Output is correct |
22 |
Correct |
31 ms |
18524 KB |
Output is correct |
23 |
Correct |
47 ms |
15444 KB |
Output is correct |
24 |
Correct |
29 ms |
15952 KB |
Output is correct |
25 |
Correct |
37 ms |
14364 KB |
Output is correct |
26 |
Correct |
36 ms |
14172 KB |
Output is correct |
27 |
Correct |
4 ms |
10588 KB |
Output is correct |
28 |
Correct |
4 ms |
10584 KB |
Output is correct |
29 |
Correct |
4 ms |
10704 KB |
Output is correct |
30 |
Correct |
4 ms |
10588 KB |
Output is correct |
31 |
Correct |
4 ms |
10588 KB |
Output is correct |
32 |
Correct |
4 ms |
11100 KB |
Output is correct |
33 |
Correct |
4 ms |
10844 KB |
Output is correct |
34 |
Correct |
5 ms |
10844 KB |
Output is correct |
35 |
Correct |
5 ms |
10840 KB |
Output is correct |
36 |
Correct |
27 ms |
30960 KB |
Output is correct |
37 |
Correct |
35 ms |
22052 KB |
Output is correct |
38 |
Correct |
49 ms |
19280 KB |
Output is correct |
39 |
Correct |
50 ms |
19540 KB |
Output is correct |
40 |
Correct |
41 ms |
19036 KB |
Output is correct |
41 |
Correct |
40 ms |
14180 KB |
Output is correct |
42 |
Correct |
43 ms |
14164 KB |
Output is correct |
43 |
Correct |
36 ms |
22108 KB |
Output is correct |
44 |
Correct |
5 ms |
10584 KB |
Output is correct |
45 |
Correct |
5 ms |
10588 KB |
Output is correct |
46 |
Correct |
4 ms |
10588 KB |
Output is correct |
47 |
Correct |
4 ms |
10588 KB |
Output is correct |
48 |
Correct |
4 ms |
10588 KB |
Output is correct |
49 |
Correct |
5 ms |
11024 KB |
Output is correct |
50 |
Correct |
5 ms |
10844 KB |
Output is correct |
51 |
Correct |
8 ms |
10964 KB |
Output is correct |
52 |
Correct |
5 ms |
10844 KB |
Output is correct |
53 |
Correct |
28 ms |
30944 KB |
Output is correct |
54 |
Correct |
46 ms |
19296 KB |
Output is correct |
55 |
Correct |
47 ms |
19544 KB |
Output is correct |
56 |
Correct |
46 ms |
19036 KB |
Output is correct |
57 |
Correct |
62 ms |
19092 KB |
Output is correct |
58 |
Correct |
30 ms |
31312 KB |
Output is correct |
59 |
Correct |
38 ms |
22432 KB |
Output is correct |