#include <bits/stdc++.h>
#define int long long
using namespace std;
struct fenwick {
int size;
vector<long long> tree;
fenwick(int N) : size(N), tree(N + 1) {}
void update(int i, int value) {
for (i++; i <= size; i += i & -i) {
tree[i] += value;
}
}
long long query(int r) {
long long ans = 0;
for (; r > 0; r -= r & -r) {
ans += tree[r];
}
return ans;
}
int lower_bound(long long value) {
int pos = 0;
for (int l = 20; l >= 0; l--) {
if (pos + (1 << l) <= size && tree[pos + (1 << l)] < value) {
pos += (1 << l);
value -= tree[pos];
}
}
return pos;
}
};
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
int K, N;
cin >> K >> N;
vector<char> P(N), Q(N);
vector<int> S(N), T(N), G;
long long ans = 0;
vector<array<int, 2>> X;
for (int i = 0; i < N; i++) {
cin >> P[i] >> S[i] >> Q[i] >> T[i];
if (P[i] > Q[i]) {
swap(S[i], T[i]);
}
if (P[i] == Q[i]) {
ans += abs(S[i] - T[i]);
} else {
ans++;
X.push_back({S[i], T[i]});
G.push_back(S[i]);
G.push_back(T[i]);
}
}
int M = (int)X.size();
sort(G.begin(), G.end());
G.resize(unique(G.begin(), G.end()) - G.begin());
vector<int> Y = G;
for (int &v : Y) {
v = lower_bound(G.begin(), G.end(), v) - G.begin();
}
vector<array<int, 2>> C(M);
for (int i = 0; i < M; i++) {
C[i][0] = lower_bound(G.begin(), G.end(), X[i][0]) - G.begin();
C[i][1] = lower_bound(G.begin(), G.end(), X[i][1]) - G.begin();
}
vector<int> ord(M);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int i, int j) {
return X[i][0] + X[i][1] < X[j][0] + X[j][1];
});
fenwick FA(2 * M), FB(2 * M);
vector<long long> pref(M + 1);
long long sum = 0;
for (int i = 0; i < M; i++) {
for (int j = 0; j < 2; j++) {
FA.update(C[ord[i]][j], 1);
FB.update(C[ord[i]][j], X[ord[i]][j]);
sum += X[ord[i]][j];
}
int Ymed = FA.lower_bound(i + 1);
int med = G[lower_bound(Y.begin(), Y.end(), Ymed) - Y.begin()];
int cnt = FA.query(Ymed), Sless = FB.query(Ymed);
pref[i + 1] = 1LL * med * cnt - Sless + sum - Sless - 1LL * med * ((i + 1) * 2 - cnt);
}
vector<long long> suff(M + 1);
FA = FB = fenwick(M * 2);
sum = 0;
for (int i = M - 1; i >= 0; i--) {
for (int j = 0; j < 2; j++) {
FA.update(C[ord[i]][j], 1);
FB.update(C[ord[i]][j], X[ord[i]][j]);
sum += X[ord[i]][j];
}
int Ymed = FA.lower_bound(M - i);
int med = G[lower_bound(Y.begin(), Y.end(), Ymed) - Y.begin()];
int cnt = FA.query(Ymed), Sless = FB.query(Ymed);
suff[i] = 1LL * med * cnt - Sless + sum - Sless - 1LL * med * ((M - i) * 2 - cnt);
}
long long res = LLONG_MAX;
for (int i = 0; i <= M; i++) {
if (i == 0 || K != 1) {
res = min(res, pref[i] + suff[i]);
}
}
cout << ans + res << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
544 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
110 ms |
14740 KB |
Output is correct |
13 |
Correct |
136 ms |
17736 KB |
Output is correct |
14 |
Correct |
90 ms |
13824 KB |
Output is correct |
15 |
Correct |
71 ms |
10680 KB |
Output is correct |
16 |
Correct |
108 ms |
15404 KB |
Output is correct |
17 |
Correct |
90 ms |
17680 KB |
Output is correct |
18 |
Correct |
84 ms |
17332 KB |
Output is correct |
19 |
Correct |
125 ms |
17584 KB |
Output is correct |
20 |
Correct |
108 ms |
15800 KB |
Output is correct |
21 |
Correct |
104 ms |
17588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
2 ms |
556 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
2 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
856 KB |
Output is correct |
25 |
Correct |
111 ms |
14516 KB |
Output is correct |
26 |
Correct |
93 ms |
14776 KB |
Output is correct |
27 |
Correct |
142 ms |
17016 KB |
Output is correct |
28 |
Correct |
139 ms |
17740 KB |
Output is correct |
29 |
Correct |
141 ms |
17684 KB |
Output is correct |
30 |
Correct |
65 ms |
9664 KB |
Output is correct |
31 |
Correct |
103 ms |
15592 KB |
Output is correct |
32 |
Correct |
102 ms |
17660 KB |
Output is correct |
33 |
Correct |
89 ms |
17328 KB |
Output is correct |
34 |
Correct |
132 ms |
17752 KB |
Output is correct |
35 |
Correct |
110 ms |
15756 KB |
Output is correct |
36 |
Correct |
110 ms |
17580 KB |
Output is correct |
37 |
Correct |
101 ms |
14712 KB |
Output is correct |