#include <bits/stdc++.h>
using namespace std;
const int kN = 5e3;
const int INF = 1e9;
int n, m, d[1 + kN], cnt[1 + kN];
vector<pair<int, int>> g[1 + kN];
bitset<1 + kN> inQ;
/// Sistem de inecuatii diferentiale peste sumele partiale pe prefix
/// Dupa ce rezolv sistemul pentru sumele partiale obtin simplu si elementele: a[i] = p[i] - p[i - 1]
bool BellmanFord(int s) {
for (int i = 0; i <= n; ++i) {
d[i] = INF;
}
d[s] = 0;
queue<int> q;
q.emplace(s);
inQ[s] = true;
cnt[s] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
inQ[u] = false;
for (const auto &it : g[u]) {
int v, w;
tie(v, w) = it;
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
if (d[v] < 0) {
return true;
}
if (!inQ[v]) {
if (cnt[v] == n - 1) {
return true;
}
q.emplace(v);
inQ[v] = true;
cnt[v] += 1;
}
}
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int l, r, k, v;
cin >> l >> r >> k >> v;
r += 1;
if (v == 0) {
/// sunt cel putin k valori de 0 in intervalul l..r <=> p[r] - p[l - 1] <= (r - l + 1) - k
g[l].emplace_back(r, (r - l) - k);
} else {
/// sunt cel putin (r - l + 1) - k + 1 valori de in intervalul l..r
/// <=> p[r] - p[l - 1] >= (r - l + 1) - k + 1 | *(-1)
/// <=> p[l - 1] - p[r] <= -(r - l + 1) + k - 1
g[r].emplace_back(l, -(r - l) + k - 1);
}
}
for (int i = 1; i <= n; ++i) {
/// p[i - 1] <= p[i] <= p[i - 1] + 1
/// p[i - 1] - p[i] <= 0
g[i].emplace_back(i - 1, 0);
/// p[i] - p[i - 1] <= 1
g[i - 1].emplace_back(i, 1);
}
if (BellmanFord(0)) {
cout << "-1\n";
} else {
for (int i = 1; i <= n; ++i) {
cout << d[i] - d[i - 1] << ' ';
}
cout << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 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 |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
724 KB |
Output is correct |
2 |
Correct |
13 ms |
768 KB |
Output is correct |
3 |
Correct |
13 ms |
724 KB |
Output is correct |
4 |
Correct |
11 ms |
668 KB |
Output is correct |
5 |
Correct |
4 ms |
724 KB |
Output is correct |
6 |
Correct |
4 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
724 KB |
Output is correct |
2 |
Correct |
13 ms |
768 KB |
Output is correct |
3 |
Correct |
13 ms |
724 KB |
Output is correct |
4 |
Correct |
11 ms |
668 KB |
Output is correct |
5 |
Correct |
4 ms |
724 KB |
Output is correct |
6 |
Correct |
4 ms |
852 KB |
Output is correct |
7 |
Correct |
11 ms |
852 KB |
Output is correct |
8 |
Correct |
13 ms |
968 KB |
Output is correct |
9 |
Correct |
22 ms |
1100 KB |
Output is correct |
10 |
Correct |
10 ms |
852 KB |
Output is correct |
11 |
Correct |
4 ms |
864 KB |
Output is correct |
12 |
Correct |
5 ms |
852 KB |
Output is correct |
13 |
Correct |
11 ms |
832 KB |
Output is correct |
14 |
Correct |
19 ms |
944 KB |
Output is correct |
15 |
Correct |
31 ms |
960 KB |
Output is correct |
16 |
Correct |
117 ms |
912 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 |
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 |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
14 ms |
724 KB |
Output is correct |
12 |
Correct |
13 ms |
768 KB |
Output is correct |
13 |
Correct |
13 ms |
724 KB |
Output is correct |
14 |
Correct |
11 ms |
668 KB |
Output is correct |
15 |
Correct |
4 ms |
724 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
11 ms |
852 KB |
Output is correct |
18 |
Correct |
13 ms |
968 KB |
Output is correct |
19 |
Correct |
22 ms |
1100 KB |
Output is correct |
20 |
Correct |
10 ms |
852 KB |
Output is correct |
21 |
Correct |
4 ms |
864 KB |
Output is correct |
22 |
Correct |
5 ms |
852 KB |
Output is correct |
23 |
Correct |
11 ms |
832 KB |
Output is correct |
24 |
Correct |
19 ms |
944 KB |
Output is correct |
25 |
Correct |
31 ms |
960 KB |
Output is correct |
26 |
Correct |
117 ms |
912 KB |
Output is correct |
27 |
Correct |
7 ms |
852 KB |
Output is correct |
28 |
Correct |
7 ms |
980 KB |
Output is correct |
29 |
Correct |
7 ms |
952 KB |
Output is correct |
30 |
Correct |
7 ms |
960 KB |
Output is correct |
31 |
Correct |
7 ms |
852 KB |
Output is correct |
32 |
Correct |
7 ms |
952 KB |
Output is correct |
33 |
Correct |
4 ms |
912 KB |
Output is correct |
34 |
Correct |
4 ms |
820 KB |
Output is correct |
35 |
Correct |
7 ms |
972 KB |
Output is correct |
36 |
Correct |
7 ms |
852 KB |
Output is correct |