#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl;
const ll MAXN = 3e5 + 10;
inline pll operator+ (const pll& a, const pll& b) {
return {a.X + b.X, a.Y + b.Y};
}
inline pll operator- (const pll& a, const pll& b) {
return {a.X - b.X, a.Y - b.Y};
}
int n, k;
ll pref[MAXN], suff[MAXN];
vector<int> vec;
pair<int, pll> seg[MAXN];
namespace fenwick {
pll fen[MAXN];
inline void init() {
fill(fen, fen + MAXN, make_pair(0, 0));
}
inline void update(int ind, pll val) {
for (++ind; ind < MAXN; ind += ind & -ind)
fen[ind] = fen[ind] + val;
}
inline pll query(int ind) {
pll ans = {0, 0};
for (++ind; ind > 0; ind -= ind & -ind)
ans = ans + fen[ind];
return ans;
}
inline int median() {
int l = 1, r = MAXN - 5, tot = query(r).X;
while (l < r) {
int mid = (l + r) >> 1;
if (query(mid).X >= (tot + 1) / 2) r = mid;
else l = mid + 1;
}
return l;
}
inline ll cost() {
int med = median();
pll e1 = query(med), e2 = query(MAXN - 5) - e1;
return vec[med] * e1.X - e1.Y + e2.Y - e2.X * vec[med];
}
}
inline int comp_ind(int x) {
return lower_bound(all(vec), x) - vec.begin();
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> k >> n;
ll tans = 0;
int tn = 0;
for (int i = 1; i <= n; i++) {
char c1, c2;
int a, b;
cin >> c1 >> a >> c2 >> b;
if (c1 == c2) tans += abs(a - b);
else {
seg[++tn] = {a + b, {min(a, b), max(a, b)}};
vec.push_back(a);
vec.push_back(b);
tans++;
}
}
n = tn;
vec.push_back(-1);
sort(all(vec));
vec.resize(unique(all(vec)) - vec.begin());
sort(seg + 1, seg + n + 1);
for (int i = 1; i <= n; i++) {
fenwick::update(comp_ind(seg[i].Y.X), {1, seg[i].Y.X});
fenwick::update(comp_ind(seg[i].Y.Y), {1, seg[i].Y.Y});
pref[i] = fenwick::cost();
}
fenwick::init();
for (int i = n; i > 0; i--) {
fenwick::update(comp_ind(seg[i].Y.X), {1, seg[i].Y.X});
fenwick::update(comp_ind(seg[i].Y.Y), {1, seg[i].Y.Y});
suff[i] = fenwick::cost();
}
if (k == 1) return cout << pref[n] + tans << endl, 0;
ll ans = pref[n];
for (int i = 0; i <= n; i++)
ans = min(ans, pref[i] + suff[i + 1]);
cout << ans + tans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
4 ms |
5076 KB |
Output is correct |
5 |
Correct |
4 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5084 KB |
Output is correct |
7 |
Correct |
4 ms |
5080 KB |
Output is correct |
8 |
Correct |
5 ms |
5076 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
4 ms |
5076 KB |
Output is correct |
11 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
5072 KB |
Output is correct |
4 |
Correct |
4 ms |
5076 KB |
Output is correct |
5 |
Correct |
4 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5024 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
5 ms |
5076 KB |
Output is correct |
12 |
Correct |
91 ms |
10520 KB |
Output is correct |
13 |
Correct |
199 ms |
11884 KB |
Output is correct |
14 |
Correct |
126 ms |
10584 KB |
Output is correct |
15 |
Correct |
103 ms |
9296 KB |
Output is correct |
16 |
Correct |
100 ms |
11424 KB |
Output is correct |
17 |
Correct |
145 ms |
11876 KB |
Output is correct |
18 |
Correct |
131 ms |
11752 KB |
Output is correct |
19 |
Correct |
154 ms |
11820 KB |
Output is correct |
20 |
Correct |
99 ms |
11820 KB |
Output is correct |
21 |
Correct |
142 ms |
11936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4944 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4952 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4944 KB |
Output is correct |
5 |
Correct |
2 ms |
4936 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
4 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
5 ms |
5076 KB |
Output is correct |
14 |
Correct |
4 ms |
5076 KB |
Output is correct |
15 |
Correct |
5 ms |
5076 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
5076 KB |
Output is correct |
20 |
Correct |
4 ms |
5084 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
4 ms |
5088 KB |
Output is correct |
23 |
Correct |
3 ms |
5116 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4980 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5024 KB |
Output is correct |
11 |
Correct |
4 ms |
4944 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5096 KB |
Output is correct |
14 |
Correct |
4 ms |
5076 KB |
Output is correct |
15 |
Correct |
4 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
4 ms |
4952 KB |
Output is correct |
19 |
Correct |
5 ms |
5076 KB |
Output is correct |
20 |
Correct |
5 ms |
5116 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
4 ms |
5080 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5116 KB |
Output is correct |
25 |
Correct |
99 ms |
10584 KB |
Output is correct |
26 |
Correct |
119 ms |
10784 KB |
Output is correct |
27 |
Correct |
199 ms |
11596 KB |
Output is correct |
28 |
Correct |
201 ms |
12084 KB |
Output is correct |
29 |
Correct |
219 ms |
12120 KB |
Output is correct |
30 |
Correct |
98 ms |
8780 KB |
Output is correct |
31 |
Correct |
109 ms |
11492 KB |
Output is correct |
32 |
Correct |
158 ms |
12148 KB |
Output is correct |
33 |
Correct |
146 ms |
11748 KB |
Output is correct |
34 |
Correct |
152 ms |
12180 KB |
Output is correct |
35 |
Correct |
102 ms |
11700 KB |
Output is correct |
36 |
Correct |
150 ms |
11848 KB |
Output is correct |
37 |
Correct |
105 ms |
10584 KB |
Output is correct |