#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 || cnt[v] > n) { /// nu poate exista o suma partiala negativa
return true;
}
if (!inQ[v]) {
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 1 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
724 KB |
Output is correct |
2 |
Correct |
14 ms |
700 KB |
Output is correct |
3 |
Correct |
13 ms |
748 KB |
Output is correct |
4 |
Correct |
13 ms |
680 KB |
Output is correct |
5 |
Correct |
4 ms |
728 KB |
Output is correct |
6 |
Correct |
3 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
724 KB |
Output is correct |
2 |
Correct |
14 ms |
700 KB |
Output is correct |
3 |
Correct |
13 ms |
748 KB |
Output is correct |
4 |
Correct |
13 ms |
680 KB |
Output is correct |
5 |
Correct |
4 ms |
728 KB |
Output is correct |
6 |
Correct |
3 ms |
724 KB |
Output is correct |
7 |
Correct |
12 ms |
724 KB |
Output is correct |
8 |
Correct |
12 ms |
820 KB |
Output is correct |
9 |
Correct |
18 ms |
724 KB |
Output is correct |
10 |
Correct |
10 ms |
724 KB |
Output is correct |
11 |
Correct |
3 ms |
724 KB |
Output is correct |
12 |
Correct |
4 ms |
724 KB |
Output is correct |
13 |
Correct |
11 ms |
724 KB |
Output is correct |
14 |
Correct |
19 ms |
724 KB |
Output is correct |
15 |
Correct |
30 ms |
808 KB |
Output is correct |
16 |
Correct |
136 ms |
816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 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 |
12 ms |
724 KB |
Output is correct |
12 |
Correct |
14 ms |
700 KB |
Output is correct |
13 |
Correct |
13 ms |
748 KB |
Output is correct |
14 |
Correct |
13 ms |
680 KB |
Output is correct |
15 |
Correct |
4 ms |
728 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
12 ms |
724 KB |
Output is correct |
18 |
Correct |
12 ms |
820 KB |
Output is correct |
19 |
Correct |
18 ms |
724 KB |
Output is correct |
20 |
Correct |
10 ms |
724 KB |
Output is correct |
21 |
Correct |
3 ms |
724 KB |
Output is correct |
22 |
Correct |
4 ms |
724 KB |
Output is correct |
23 |
Correct |
11 ms |
724 KB |
Output is correct |
24 |
Correct |
19 ms |
724 KB |
Output is correct |
25 |
Correct |
30 ms |
808 KB |
Output is correct |
26 |
Correct |
136 ms |
816 KB |
Output is correct |
27 |
Correct |
6 ms |
724 KB |
Output is correct |
28 |
Correct |
6 ms |
724 KB |
Output is correct |
29 |
Correct |
8 ms |
808 KB |
Output is correct |
30 |
Correct |
7 ms |
724 KB |
Output is correct |
31 |
Correct |
6 ms |
724 KB |
Output is correct |
32 |
Correct |
6 ms |
808 KB |
Output is correct |
33 |
Correct |
4 ms |
724 KB |
Output is correct |
34 |
Correct |
4 ms |
724 KB |
Output is correct |
35 |
Correct |
6 ms |
816 KB |
Output is correct |
36 |
Correct |
8 ms |
792 KB |
Output is correct |