#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=5e4, mxQ=1e4;
int n, m, k, q, ans[mxQ];
ar<int, 2> qry[mxQ];
vector<ar<int, 2>> bef[mxN], aft[mxN];
int tl[mxN][5], tr[mxN][5];
void smin(int& x, int y) {
if (x==-1||y<x)
x=y;
}
void solve(int l, int r, vector<int>& v) {
if (v.empty())
return;
assert(l^r);
int mid=(l+r)/2;
for (int i=mid*k; i<(mid+1)*k; ++i) {
memset(tl[i], -1, sizeof(tl[i]));
memset(tr[i], -1, sizeof(tr[i]));
tl[i][i%k]=tr[i][i%k]=0;
}
for (int i=mid*k-1; i>=l*k; --i) {
memset(tl[i], -1, sizeof(tl[i]));
for (int j=0; j<k; ++j)
for (ar<int, 2> a : aft[i])
if (tl[a[0]][j]^-1)
smin(tl[i][j], tl[a[0]][j]+a[1]);
}
for (int i=(mid+1)*k; i<min(n, (r+1)*k); ++i) {
memset(tr[i], -1, sizeof(tr[i]));
for (int j=0; j<k; ++j)
for (ar<int, 2> a : bef[i])
if (tr[a[0]][j]^-1)
smin(tr[i][j], tr[a[0]][j]+a[1]);
}
vector<int> vl, vr;
for (int i : v) {
int ql=qry[i][0], qr=qry[i][1];
assert(l*k<=ql&&qr<(r+1)*k);
if (qr/k<mid)
vl.push_back(i);
else if (ql/k>mid)
vr.push_back(i);
else {
ans[i]=-1;
for (int j=0; j<k; ++j)
if (tl[ql][j]^-1&&tr[qr][j]^-1)
smin(ans[i], tl[ql][j]+tr[qr][j]);
}
}
vector<int>().swap(v);
solve(l, mid-1, vl);
solve(mid+1, r, vr);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> k >> n >> m >> q;
for (int i=0; i<m; ++i) {
int u, v, w;
cin >> u >> v >> w;
aft[u].push_back({v, w});
bef[v].push_back({u, w});
}
vector<int> inds;
for (int i=0; i<q; ++i) {
cin >> qry[i][0] >> qry[i][1];
if (qry[i][0]/k==qry[i][1]/k)
ans[i]=-1;
else
inds.push_back(i);
}
solve(0, (n-1)/k, inds);
for (int i=0; i<q; ++i)
cout << ans[i] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
7980 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2684 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
3 ms |
2816 KB |
Output is correct |
6 |
Correct |
3 ms |
2764 KB |
Output is correct |
7 |
Correct |
3 ms |
2820 KB |
Output is correct |
8 |
Correct |
37 ms |
8840 KB |
Output is correct |
9 |
Correct |
37 ms |
8644 KB |
Output is correct |
10 |
Correct |
8 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
7492 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
6 ms |
2924 KB |
Output is correct |
8 |
Correct |
6 ms |
3020 KB |
Output is correct |
9 |
Correct |
34 ms |
7396 KB |
Output is correct |
10 |
Correct |
122 ms |
9356 KB |
Output is correct |
11 |
Correct |
77 ms |
7744 KB |
Output is correct |
12 |
Correct |
57 ms |
7532 KB |
Output is correct |
13 |
Correct |
106 ms |
9208 KB |
Output is correct |
14 |
Correct |
60 ms |
7104 KB |
Output is correct |
15 |
Correct |
49 ms |
6464 KB |
Output is correct |
16 |
Correct |
46 ms |
6456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2592 KB |
Output is correct |
6 |
Correct |
3 ms |
2764 KB |
Output is correct |
7 |
Correct |
3 ms |
2820 KB |
Output is correct |
8 |
Correct |
5 ms |
2952 KB |
Output is correct |
9 |
Correct |
4 ms |
2764 KB |
Output is correct |
10 |
Correct |
39 ms |
8556 KB |
Output is correct |
11 |
Correct |
71 ms |
9120 KB |
Output is correct |
12 |
Correct |
115 ms |
11588 KB |
Output is correct |
13 |
Correct |
119 ms |
12228 KB |
Output is correct |
14 |
Correct |
84 ms |
10460 KB |
Output is correct |
15 |
Correct |
48 ms |
7488 KB |
Output is correct |
16 |
Correct |
60 ms |
7460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2592 KB |
Output is correct |
6 |
Correct |
3 ms |
2764 KB |
Output is correct |
7 |
Correct |
3 ms |
2820 KB |
Output is correct |
8 |
Correct |
5 ms |
2952 KB |
Output is correct |
9 |
Correct |
4 ms |
2764 KB |
Output is correct |
10 |
Correct |
39 ms |
8556 KB |
Output is correct |
11 |
Correct |
71 ms |
9120 KB |
Output is correct |
12 |
Correct |
115 ms |
11588 KB |
Output is correct |
13 |
Correct |
119 ms |
12228 KB |
Output is correct |
14 |
Correct |
84 ms |
10460 KB |
Output is correct |
15 |
Correct |
48 ms |
7488 KB |
Output is correct |
16 |
Correct |
60 ms |
7460 KB |
Output is correct |
17 |
Correct |
74 ms |
9268 KB |
Output is correct |
18 |
Correct |
2 ms |
2636 KB |
Output is correct |
19 |
Correct |
2 ms |
2636 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2636 KB |
Output is correct |
22 |
Correct |
2 ms |
2684 KB |
Output is correct |
23 |
Correct |
3 ms |
2892 KB |
Output is correct |
24 |
Correct |
4 ms |
2892 KB |
Output is correct |
25 |
Correct |
6 ms |
3012 KB |
Output is correct |
26 |
Correct |
5 ms |
2892 KB |
Output is correct |
27 |
Correct |
37 ms |
8568 KB |
Output is correct |
28 |
Correct |
148 ms |
11808 KB |
Output is correct |
29 |
Correct |
124 ms |
12524 KB |
Output is correct |
30 |
Correct |
84 ms |
10644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
7980 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2684 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
3 ms |
2816 KB |
Output is correct |
6 |
Correct |
3 ms |
2764 KB |
Output is correct |
7 |
Correct |
3 ms |
2820 KB |
Output is correct |
8 |
Correct |
37 ms |
8840 KB |
Output is correct |
9 |
Correct |
37 ms |
8644 KB |
Output is correct |
10 |
Correct |
8 ms |
4940 KB |
Output is correct |
11 |
Correct |
65 ms |
7492 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
17 |
Correct |
6 ms |
2924 KB |
Output is correct |
18 |
Correct |
6 ms |
3020 KB |
Output is correct |
19 |
Correct |
34 ms |
7396 KB |
Output is correct |
20 |
Correct |
122 ms |
9356 KB |
Output is correct |
21 |
Correct |
77 ms |
7744 KB |
Output is correct |
22 |
Correct |
57 ms |
7532 KB |
Output is correct |
23 |
Correct |
106 ms |
9208 KB |
Output is correct |
24 |
Correct |
60 ms |
7104 KB |
Output is correct |
25 |
Correct |
49 ms |
6464 KB |
Output is correct |
26 |
Correct |
46 ms |
6456 KB |
Output is correct |
27 |
Correct |
3 ms |
2636 KB |
Output is correct |
28 |
Correct |
3 ms |
2636 KB |
Output is correct |
29 |
Correct |
2 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
2636 KB |
Output is correct |
31 |
Correct |
2 ms |
2592 KB |
Output is correct |
32 |
Correct |
3 ms |
2764 KB |
Output is correct |
33 |
Correct |
3 ms |
2820 KB |
Output is correct |
34 |
Correct |
5 ms |
2952 KB |
Output is correct |
35 |
Correct |
4 ms |
2764 KB |
Output is correct |
36 |
Correct |
39 ms |
8556 KB |
Output is correct |
37 |
Correct |
71 ms |
9120 KB |
Output is correct |
38 |
Correct |
115 ms |
11588 KB |
Output is correct |
39 |
Correct |
119 ms |
12228 KB |
Output is correct |
40 |
Correct |
84 ms |
10460 KB |
Output is correct |
41 |
Correct |
48 ms |
7488 KB |
Output is correct |
42 |
Correct |
60 ms |
7460 KB |
Output is correct |
43 |
Correct |
74 ms |
9268 KB |
Output is correct |
44 |
Correct |
2 ms |
2636 KB |
Output is correct |
45 |
Correct |
2 ms |
2636 KB |
Output is correct |
46 |
Correct |
2 ms |
2636 KB |
Output is correct |
47 |
Correct |
2 ms |
2636 KB |
Output is correct |
48 |
Correct |
2 ms |
2684 KB |
Output is correct |
49 |
Correct |
3 ms |
2892 KB |
Output is correct |
50 |
Correct |
4 ms |
2892 KB |
Output is correct |
51 |
Correct |
6 ms |
3012 KB |
Output is correct |
52 |
Correct |
5 ms |
2892 KB |
Output is correct |
53 |
Correct |
37 ms |
8568 KB |
Output is correct |
54 |
Correct |
148 ms |
11808 KB |
Output is correct |
55 |
Correct |
124 ms |
12524 KB |
Output is correct |
56 |
Correct |
84 ms |
10644 KB |
Output is correct |
57 |
Correct |
192 ms |
13512 KB |
Output is correct |
58 |
Correct |
43 ms |
8828 KB |
Output is correct |
59 |
Correct |
72 ms |
9668 KB |
Output is correct |