#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44
/*const int MOD = (int) 1e9 + 7;
inline int lgput(int a, int b) {
int ans = 1;
while(b > 0) {
if(b & 1) ans = (1LL * ans * a) % MOD;
b >>= 1;
a = (1LL * a * a) % MOD;
}
return ans;
}
inline void mod(int &x) {
if(x >= MOD)
x -= MOD;
}
inline void add(int &x, int y) {
x += y;
mod(x);
}
inline void sub(int &x, int y) {
x += MOD - y;
mod(x);
}
inline void mul(int &x, int y) {
x = (1LL * x * y) % MOD;
}*/
/*int fact[], invfact[];
inline void prec(int n) {
fact[0] = 1;
for(int i = 1; i <= n; i++) {
fact[i] = (1LL * fact[i - 1] * i) % MOD;
}
invfact[n] = lgput(fact[n], MOD - 2);
for(int i = n - 1; i >= 0; i--) {
invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
}
}
inline int comb(int n, int k) {
if(n < k) return 0;
return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}*/
using namespace std;
const int MAXN = 50000;
const int INF = 1e9;
vector < pair <int, int> > gl[MAXN + 1], gr[MAXN + 1];
vector < pair <int, int> > qry[MAXN + 1];
int a[MAXN + 1], b[MAXN + 1];
int dpl[MAXN + 1], dpr[MAXN + 1];
int sol[MAXN + 1], k;
void divide(int l, int r) {
if(l == r) {
return ;
}
int mid = (l + r) / 2;
for(int nod = max(mid - k, l); nod <= min(mid + k, r); nod++) {
dpr[nod] = 0;
for(int i = nod + 1; i <= r; i++) {
dpr[i] = INF;
for(auto it : gr[i]) {
if(it.first >= nod) {
dpr[i] = min(dpr[i], dpr[it.first] + it.second);
}
}
}
dpl[nod] = 0;
for(int i = nod - 1; i >= l; i--) {
dpl[i] = INF;
for(auto it : gl[i]) {
if(it.first <= nod) {
dpl[i] = min(dpl[i], dpl[it.first] + it.second);
}
}
}
for(int i = l; i <= nod; i++) {
int pos = (int) qry[i].size() - 1;
while(pos >= 0 && qry[i][pos].first >= nod) {
sol[qry[i][pos].second] = min(sol[qry[i][pos].second], dpl[i] + dpr[qry[i][pos].first]);
pos--;
}
}
}
for(int i = l; i <= mid; i++) {
while(qry[i].size() && qry[i].back().first > mid) {
qry[i].pop_back();
}
}
divide(l, mid);
divide(mid + 1, r);
}
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int i, n, m, o;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> k >> n >> m >> o;
for(i = 1; i <= m; i++) {
int x, y, z;
cin >> x >> y >> z;
x++, y++;
gr[y].push_back({x, z});
gl[x].push_back({y, z});
}
for(i = 1; i <= o; i++) {
cin >> a[i] >> b[i];
a[i]++, b[i]++;
qry[a[i]].push_back({b[i], i});
sol[i] = INF;
}
for(i = 1; i <= n; i++) {
sort(qry[i].begin(), qry[i].end());
}
divide(1, n);
for(i = 1; i <= o; i++) {
if(sol[i] == INF) {
sol[i] = -1;
}
cout << sol[i] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
8776 KB |
Output is correct |
2 |
Correct |
5 ms |
3944 KB |
Output is correct |
3 |
Correct |
5 ms |
3916 KB |
Output is correct |
4 |
Correct |
5 ms |
3832 KB |
Output is correct |
5 |
Correct |
6 ms |
3960 KB |
Output is correct |
6 |
Correct |
6 ms |
4084 KB |
Output is correct |
7 |
Correct |
6 ms |
3960 KB |
Output is correct |
8 |
Correct |
58 ms |
8776 KB |
Output is correct |
9 |
Correct |
57 ms |
8568 KB |
Output is correct |
10 |
Correct |
18 ms |
4728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
9324 KB |
Output is correct |
2 |
Correct |
5 ms |
3832 KB |
Output is correct |
3 |
Correct |
5 ms |
3836 KB |
Output is correct |
4 |
Correct |
5 ms |
3960 KB |
Output is correct |
5 |
Correct |
5 ms |
3804 KB |
Output is correct |
6 |
Correct |
5 ms |
3832 KB |
Output is correct |
7 |
Correct |
9 ms |
4344 KB |
Output is correct |
8 |
Correct |
10 ms |
4520 KB |
Output is correct |
9 |
Correct |
53 ms |
8652 KB |
Output is correct |
10 |
Correct |
167 ms |
11828 KB |
Output is correct |
11 |
Correct |
104 ms |
9496 KB |
Output is correct |
12 |
Correct |
110 ms |
8824 KB |
Output is correct |
13 |
Correct |
181 ms |
12016 KB |
Output is correct |
14 |
Correct |
94 ms |
8924 KB |
Output is correct |
15 |
Correct |
110 ms |
8052 KB |
Output is correct |
16 |
Correct |
107 ms |
8056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3832 KB |
Output is correct |
2 |
Correct |
5 ms |
3836 KB |
Output is correct |
3 |
Correct |
5 ms |
3960 KB |
Output is correct |
4 |
Correct |
5 ms |
3832 KB |
Output is correct |
5 |
Correct |
5 ms |
3960 KB |
Output is correct |
6 |
Correct |
6 ms |
3960 KB |
Output is correct |
7 |
Correct |
6 ms |
3960 KB |
Output is correct |
8 |
Correct |
9 ms |
4088 KB |
Output is correct |
9 |
Correct |
8 ms |
4088 KB |
Output is correct |
10 |
Correct |
54 ms |
8220 KB |
Output is correct |
11 |
Correct |
100 ms |
8952 KB |
Output is correct |
12 |
Correct |
165 ms |
11384 KB |
Output is correct |
13 |
Correct |
178 ms |
12024 KB |
Output is correct |
14 |
Correct |
154 ms |
10336 KB |
Output is correct |
15 |
Correct |
111 ms |
7928 KB |
Output is correct |
16 |
Correct |
109 ms |
7928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3832 KB |
Output is correct |
2 |
Correct |
5 ms |
3836 KB |
Output is correct |
3 |
Correct |
5 ms |
3960 KB |
Output is correct |
4 |
Correct |
5 ms |
3832 KB |
Output is correct |
5 |
Correct |
5 ms |
3960 KB |
Output is correct |
6 |
Correct |
6 ms |
3960 KB |
Output is correct |
7 |
Correct |
6 ms |
3960 KB |
Output is correct |
8 |
Correct |
9 ms |
4088 KB |
Output is correct |
9 |
Correct |
8 ms |
4088 KB |
Output is correct |
10 |
Correct |
54 ms |
8220 KB |
Output is correct |
11 |
Correct |
100 ms |
8952 KB |
Output is correct |
12 |
Correct |
165 ms |
11384 KB |
Output is correct |
13 |
Correct |
178 ms |
12024 KB |
Output is correct |
14 |
Correct |
154 ms |
10336 KB |
Output is correct |
15 |
Correct |
111 ms |
7928 KB |
Output is correct |
16 |
Correct |
109 ms |
7928 KB |
Output is correct |
17 |
Correct |
102 ms |
9052 KB |
Output is correct |
18 |
Correct |
5 ms |
3960 KB |
Output is correct |
19 |
Correct |
5 ms |
3876 KB |
Output is correct |
20 |
Correct |
5 ms |
3832 KB |
Output is correct |
21 |
Correct |
5 ms |
3832 KB |
Output is correct |
22 |
Correct |
6 ms |
3960 KB |
Output is correct |
23 |
Correct |
7 ms |
4088 KB |
Output is correct |
24 |
Correct |
8 ms |
4088 KB |
Output is correct |
25 |
Correct |
11 ms |
4216 KB |
Output is correct |
26 |
Correct |
11 ms |
4088 KB |
Output is correct |
27 |
Correct |
51 ms |
8440 KB |
Output is correct |
28 |
Correct |
183 ms |
11384 KB |
Output is correct |
29 |
Correct |
179 ms |
12160 KB |
Output is correct |
30 |
Correct |
153 ms |
10276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
8776 KB |
Output is correct |
2 |
Correct |
5 ms |
3944 KB |
Output is correct |
3 |
Correct |
5 ms |
3916 KB |
Output is correct |
4 |
Correct |
5 ms |
3832 KB |
Output is correct |
5 |
Correct |
6 ms |
3960 KB |
Output is correct |
6 |
Correct |
6 ms |
4084 KB |
Output is correct |
7 |
Correct |
6 ms |
3960 KB |
Output is correct |
8 |
Correct |
58 ms |
8776 KB |
Output is correct |
9 |
Correct |
57 ms |
8568 KB |
Output is correct |
10 |
Correct |
18 ms |
4728 KB |
Output is correct |
11 |
Correct |
104 ms |
9324 KB |
Output is correct |
12 |
Correct |
5 ms |
3832 KB |
Output is correct |
13 |
Correct |
5 ms |
3836 KB |
Output is correct |
14 |
Correct |
5 ms |
3960 KB |
Output is correct |
15 |
Correct |
5 ms |
3804 KB |
Output is correct |
16 |
Correct |
5 ms |
3832 KB |
Output is correct |
17 |
Correct |
9 ms |
4344 KB |
Output is correct |
18 |
Correct |
10 ms |
4520 KB |
Output is correct |
19 |
Correct |
53 ms |
8652 KB |
Output is correct |
20 |
Correct |
167 ms |
11828 KB |
Output is correct |
21 |
Correct |
104 ms |
9496 KB |
Output is correct |
22 |
Correct |
110 ms |
8824 KB |
Output is correct |
23 |
Correct |
181 ms |
12016 KB |
Output is correct |
24 |
Correct |
94 ms |
8924 KB |
Output is correct |
25 |
Correct |
110 ms |
8052 KB |
Output is correct |
26 |
Correct |
107 ms |
8056 KB |
Output is correct |
27 |
Correct |
5 ms |
3832 KB |
Output is correct |
28 |
Correct |
5 ms |
3836 KB |
Output is correct |
29 |
Correct |
5 ms |
3960 KB |
Output is correct |
30 |
Correct |
5 ms |
3832 KB |
Output is correct |
31 |
Correct |
5 ms |
3960 KB |
Output is correct |
32 |
Correct |
6 ms |
3960 KB |
Output is correct |
33 |
Correct |
6 ms |
3960 KB |
Output is correct |
34 |
Correct |
9 ms |
4088 KB |
Output is correct |
35 |
Correct |
8 ms |
4088 KB |
Output is correct |
36 |
Correct |
54 ms |
8220 KB |
Output is correct |
37 |
Correct |
100 ms |
8952 KB |
Output is correct |
38 |
Correct |
165 ms |
11384 KB |
Output is correct |
39 |
Correct |
178 ms |
12024 KB |
Output is correct |
40 |
Correct |
154 ms |
10336 KB |
Output is correct |
41 |
Correct |
111 ms |
7928 KB |
Output is correct |
42 |
Correct |
109 ms |
7928 KB |
Output is correct |
43 |
Correct |
102 ms |
9052 KB |
Output is correct |
44 |
Correct |
5 ms |
3960 KB |
Output is correct |
45 |
Correct |
5 ms |
3876 KB |
Output is correct |
46 |
Correct |
5 ms |
3832 KB |
Output is correct |
47 |
Correct |
5 ms |
3832 KB |
Output is correct |
48 |
Correct |
6 ms |
3960 KB |
Output is correct |
49 |
Correct |
7 ms |
4088 KB |
Output is correct |
50 |
Correct |
8 ms |
4088 KB |
Output is correct |
51 |
Correct |
11 ms |
4216 KB |
Output is correct |
52 |
Correct |
11 ms |
4088 KB |
Output is correct |
53 |
Correct |
51 ms |
8440 KB |
Output is correct |
54 |
Correct |
183 ms |
11384 KB |
Output is correct |
55 |
Correct |
179 ms |
12160 KB |
Output is correct |
56 |
Correct |
153 ms |
10276 KB |
Output is correct |
57 |
Correct |
247 ms |
13176 KB |
Output is correct |
58 |
Correct |
60 ms |
8672 KB |
Output is correct |
59 |
Correct |
135 ms |
9552 KB |
Output is correct |