#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
struct Seg{
int n;
vector<int> tr, lazy;
Seg(int m){
n = m + 4;
tr.resize(n * 4 + 4);
lazy.resize(n * 4 + 4);
}
void push(int x, int s, int e, int ps, int pe, int val){
if(pe < s || ps > e || ps > pe) return;
if(ps <= s && pe >= e){
tr[x] += val;
lazy[x] += val;
return;
}
if(lazy[x]){
tr[x * 2] += lazy[x], lazy[x * 2] += lazy[x];
tr[x * 2 + 1] += lazy[x], lazy[x * 2 + 1] += lazy[x];
lazy[x] = 0;
}
int m = s + e >> 1;
push(x * 2, s, m, ps, pe, val);
push(x * 2 + 1, m + 1, e, ps, pe, val);
tr[x] = max(tr[x * 2], tr[x * 2 + 1]);
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int L, n;
cin >> L >> n;
int test = 0;
if(test){
L = 200000;
n = 100000;
}
Seg tr(L + 4);
vector<array<int, 4>> a(n), b;
long long alladd = 0;
int mn = 8;
for(int i = 0; i < n; ++i){
cin >> a[i][0] >> a[i][1] >> a[i][2];
--a[i][0], --a[i][1];
if(test){
a[i][2] = (long long)rand() * rand() % (int)1e9;
a[i][0] = rand() % L;
a[i][1] = rand() % L;
}
if(a[i][0] > a[i][1]) swap(a[i][0], a[i][1]);
int sz = min(mn, a[i][2]);
if(sz % 2 != a[i][2] % 2) ++sz;
assert(sz <= a[i][2]);
alladd += (a[i][2] - sz) / 2;
tr.push(1, 0, L - 1, a[i][0], a[i][1] - 1, sz);
for(int j = 1; j < sz; ++j){
a.push_back(a[i]);
}
}
n = (int)a.size();
auto flip = [&](int id)->void{
if(a[id][3] == 1){
tr.push(1, 0, L - 1, a[id][1], L - 1, -1);
tr.push(1, 0, L - 1, 0, a[id][0] - 1, -1);
tr.push(1, 0, L - 1, a[id][0], a[id][1] - 1, 1);
}
else{
tr.push(1, 0, L - 1, a[id][1], L - 1, 1);
tr.push(1, 0, L - 1, 0, a[id][0] - 1, 1);
tr.push(1, 0, L - 1, a[id][0], a[id][1] - 1, -1);
}
a[id][3] ^= 1;
};
int ans = (int)1e9;
// cout << n << endl;
// for(auto&i:a){
// cout << i[0] << ' ' << i[1] << ' ' << i[2] << ' ' << i[3] << endl;
// }
// cout << alladd << endl;
auto doit = [&]{
for(int i = 0; i < 1; ++i){
for(int j = i - 1; j < n; ++j){
if(j >= 0) flip(j);
if(n <= 300 || j == 0){
int nans = tr.tr[1];
while(true){
int now = nans;
for(int i = 0; i < n; ++i){
flip(i);
int prv = now;
now = min(now, tr.tr[1]);
if(now == prv) flip(i);
}
if(now == nans) break;
nans = now;
}
ans = min(ans, nans);
}
}
}
};
sort(a.begin(), a.end(), [&](array<int, 4>&x, array<int, 4>&y){return x[0] + x[1] > y[0] + y[1];});
doit();
cout << ans + alladd << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
216 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
216 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
216 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
216 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
216 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |