#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
struct fenwick {
int n;
vector<int> t;
explicit fenwick(int _n) : n(_n) {
t.resize(n + 1, 0);
}
void update(int v, int delta) {
for (; v <= n; v += v & (-v)) {
t[v] += delta;
}
}
void add_on_segment(int l, int r, int delta) {
l++, r++;
update(l, delta);
update(r + 1, -delta);
}
int get_value(int v) {
v++;
int result = 0;
for (; v > 0; v -= v & (-v)) {
result += t[v];
}
return result;
}
};
signed main() {
#ifdef PC
freopen("input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m;
cin >> n >> m;
vector<vector<int>> have(n);
for (int i = 0; i < m; i++) {
int from, to, cnt;
cin >> from >> to >> cnt;
from--, to--;
if (from > to) {
swap(from, to);
}
if (from < to) {
have[from].push_back(to - 1);
}
}
int l = -1, r = m;
while (r - l > 1) {
int mid = (l + r) / 2;
bool can = false;
for (int cnt_flip = 0; cnt_flip <= mid; cnt_flip++) {
multiset<pair<int, int>> segments;
fenwick cnt(n);
bool ok = true;
int already_flipped = 0;
for (int i = 0; i + 1 < n; i++) {
for (auto right_border : have[i]) {
cnt.add_on_segment(i, right_border, 1);
segments.insert({right_border, i});
}
int need_flip = max(0, (cnt.get_value(i) + (cnt_flip - already_flipped) - mid + 1) / 2);
if (already_flipped + need_flip > cnt_flip) {
ok = false;
break;
}
if (SZ(segments) < need_flip) {
ok = false;
break;
}
for (int j = 0; j < need_flip; j++) {
auto [right_border, left_border] = *segments.rbegin();
segments.erase(--segments.end());
cnt.update(1, 1);
cnt.update(left_border + 1, -2);
cnt.update(right_border + 2, 2);
}
while (!segments.empty() && segments.begin()->first < i + 1) {
segments.erase(segments.begin());
}
already_flipped += need_flip;
}
if (ok) {
for (int i = 0; i < n; i++) {
if (cnt.get_value(i) > mid) {
ok = false;
break;
}
}
if (ok) {
can = true;
break;
}
}
}
if (can) {
r = mid;
} else {
l = mid;
}
}
cout << r << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
29 ms |
340 KB |
Output is correct |
17 |
Correct |
27 ms |
340 KB |
Output is correct |
18 |
Correct |
25 ms |
336 KB |
Output is correct |
19 |
Correct |
32 ms |
212 KB |
Output is correct |
20 |
Correct |
28 ms |
212 KB |
Output is correct |
21 |
Correct |
33 ms |
212 KB |
Output is correct |
22 |
Correct |
27 ms |
212 KB |
Output is correct |
23 |
Correct |
29 ms |
212 KB |
Output is correct |
24 |
Correct |
29 ms |
340 KB |
Output is correct |
25 |
Correct |
25 ms |
212 KB |
Output is correct |
26 |
Correct |
24 ms |
320 KB |
Output is correct |
27 |
Correct |
21 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
29 ms |
340 KB |
Output is correct |
17 |
Correct |
27 ms |
340 KB |
Output is correct |
18 |
Correct |
25 ms |
336 KB |
Output is correct |
19 |
Correct |
32 ms |
212 KB |
Output is correct |
20 |
Correct |
28 ms |
212 KB |
Output is correct |
21 |
Correct |
33 ms |
212 KB |
Output is correct |
22 |
Correct |
27 ms |
212 KB |
Output is correct |
23 |
Correct |
29 ms |
212 KB |
Output is correct |
24 |
Correct |
29 ms |
340 KB |
Output is correct |
25 |
Correct |
25 ms |
212 KB |
Output is correct |
26 |
Correct |
24 ms |
320 KB |
Output is correct |
27 |
Correct |
21 ms |
336 KB |
Output is correct |
28 |
Correct |
4653 ms |
500 KB |
Output is correct |
29 |
Correct |
5130 ms |
516 KB |
Output is correct |
30 |
Correct |
4751 ms |
500 KB |
Output is correct |
31 |
Correct |
5542 ms |
592 KB |
Output is correct |
32 |
Correct |
5282 ms |
532 KB |
Output is correct |
33 |
Correct |
4617 ms |
544 KB |
Output is correct |
34 |
Correct |
4657 ms |
540 KB |
Output is correct |
35 |
Correct |
3997 ms |
548 KB |
Output is correct |
36 |
Correct |
4299 ms |
548 KB |
Output is correct |
37 |
Correct |
3417 ms |
588 KB |
Output is correct |
38 |
Correct |
7 ms |
468 KB |
Output is correct |
39 |
Correct |
512 ms |
544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
29 ms |
340 KB |
Output is correct |
17 |
Correct |
27 ms |
340 KB |
Output is correct |
18 |
Correct |
25 ms |
336 KB |
Output is correct |
19 |
Correct |
32 ms |
212 KB |
Output is correct |
20 |
Correct |
28 ms |
212 KB |
Output is correct |
21 |
Correct |
33 ms |
212 KB |
Output is correct |
22 |
Correct |
27 ms |
212 KB |
Output is correct |
23 |
Correct |
29 ms |
212 KB |
Output is correct |
24 |
Correct |
29 ms |
340 KB |
Output is correct |
25 |
Correct |
25 ms |
212 KB |
Output is correct |
26 |
Correct |
24 ms |
320 KB |
Output is correct |
27 |
Correct |
21 ms |
336 KB |
Output is correct |
28 |
Correct |
4653 ms |
500 KB |
Output is correct |
29 |
Correct |
5130 ms |
516 KB |
Output is correct |
30 |
Correct |
4751 ms |
500 KB |
Output is correct |
31 |
Correct |
5542 ms |
592 KB |
Output is correct |
32 |
Correct |
5282 ms |
532 KB |
Output is correct |
33 |
Correct |
4617 ms |
544 KB |
Output is correct |
34 |
Correct |
4657 ms |
540 KB |
Output is correct |
35 |
Correct |
3997 ms |
548 KB |
Output is correct |
36 |
Correct |
4299 ms |
548 KB |
Output is correct |
37 |
Correct |
3417 ms |
588 KB |
Output is correct |
38 |
Correct |
7 ms |
468 KB |
Output is correct |
39 |
Correct |
512 ms |
544 KB |
Output is correct |
40 |
Execution timed out |
6069 ms |
11676 KB |
Time limit exceeded |
41 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
29 ms |
340 KB |
Output is correct |
17 |
Correct |
27 ms |
340 KB |
Output is correct |
18 |
Correct |
25 ms |
336 KB |
Output is correct |
19 |
Correct |
32 ms |
212 KB |
Output is correct |
20 |
Correct |
28 ms |
212 KB |
Output is correct |
21 |
Correct |
33 ms |
212 KB |
Output is correct |
22 |
Correct |
27 ms |
212 KB |
Output is correct |
23 |
Correct |
29 ms |
212 KB |
Output is correct |
24 |
Correct |
29 ms |
340 KB |
Output is correct |
25 |
Correct |
25 ms |
212 KB |
Output is correct |
26 |
Correct |
24 ms |
320 KB |
Output is correct |
27 |
Correct |
21 ms |
336 KB |
Output is correct |
28 |
Correct |
4653 ms |
500 KB |
Output is correct |
29 |
Correct |
5130 ms |
516 KB |
Output is correct |
30 |
Correct |
4751 ms |
500 KB |
Output is correct |
31 |
Correct |
5542 ms |
592 KB |
Output is correct |
32 |
Correct |
5282 ms |
532 KB |
Output is correct |
33 |
Correct |
4617 ms |
544 KB |
Output is correct |
34 |
Correct |
4657 ms |
540 KB |
Output is correct |
35 |
Correct |
3997 ms |
548 KB |
Output is correct |
36 |
Correct |
4299 ms |
548 KB |
Output is correct |
37 |
Correct |
3417 ms |
588 KB |
Output is correct |
38 |
Correct |
7 ms |
468 KB |
Output is correct |
39 |
Correct |
512 ms |
544 KB |
Output is correct |
40 |
Execution timed out |
6069 ms |
11676 KB |
Time limit exceeded |
41 |
Halted |
0 ms |
0 KB |
- |