#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
struct Person{
int l, r;
};
int f(int x, Person p){
if(x < p.l) return (p.l - x);
if(x > p.r) return (x - p.r);
return 0;
}
const int MAXN = 2e5 + 11;
int xs[MAXN], d[MAXN], k;
int ci(int x){
return lower_bound(xs, xs + k, x) - xs;
}
struct BIT{
int bit[MAXN + 3] = {0}, s = 0;
void upd(int p, int v){
p += 2;
for(int i = p; i <= MAXN; i += i & -i){
bit[i] += v;
}
s += v;
}
int qry(int p){
int ret = 0;
p += 2;
for(int i = p; i; i -= i & -i){
ret += bit[i];
}
return ret;
}
int bs(){
int t = qry(MAXN);
assert(t % 2 == 0);
int idx = 0, val = 0;
for(int j = 19; j >= 0; j--){
if(idx + (1 << j) <= MAXN && val + bit[idx + (1 << j)] <= t / 2 - 1){
val += bit[idx + (1 << j)]; idx += (1 << j);
}
}
idx--;
return idx;
}
};
struct DS{
BIT bit0, bit1; int rsum = 0;
void upd(int p, int v, bool from_l){
bit0.upd(p, v);
bit1.upd(p, v * xs[p]);
rsum += v * (from_l ? xs[p] : 0);
}
int qry(){
int x = bit0.bs();
int t = bit0.qry(MAXN) / 2;
int c = bit0.qry(x - 1);
int lsum = bit1.qry(x - 1) + xs[x] * (t - c);
return rsum - lsum;
}
} dsl, dsr;
int32_t main() {
int K, n;
cin >> K >> n;
vector<Person> P;
int C = 0;
for(int i = 0; i < n; i++){
char p, q; int s, t;
cin >> p >> s >> q >> t;
C += abs(s - t) + (p != q);
if (p != q) {
if(s > t) swap(s, t);
P.push_back({s, t});
}
}
vector<int> x;
for(auto p : P) {
x.push_back(p.l);
x.push_back(p.r);
}
x.push_back(0);
sort(x.begin(), x.end());
x.resize(unique(x.begin(), x.end()) - x.begin());
for(auto& i : x){
i <<= 1;
}
for(auto& p : P){
p.l <<= 1, p.r <<= 1;
}
for(int i = 0; i < x.size(); i++){
xs[i] = x[i];
}
for(int i = 0; i + 1 < x.size(); i++){
d[i] = xs[i + 1] - xs[i];
}
int ans = 1LL << 60;
k = x.size();
if (K == 1) {
for(auto p : P){
dsl.upd(ci(p.l), 1, 1);
dsl.upd(ci(p.r), 1, 0);
}
cout << C + dsl.qry() << endl;
} else {
if(k == 0){
cout << C << endl;
return 0;
}
for(auto p : P){
dsr.upd(ci(p.l), 1, 1);
dsr.upd(ci(p.r), 1, 0);
}
sort(P.begin(), P.end(), [](Person a, Person b){
return (a.l + a.r) / 2 < (b.l + b.r) / 2;
});
int ans = dsl.qry() + dsr.qry();
for(int i = 0; i < P.size(); i++){
dsl.upd(ci(P[i].l), 1, 1);
dsl.upd(ci(P[i].r), 1, 0);
dsr.upd(ci(P[i].l), -1, 1);
dsr.upd(ci(P[i].r), -1, 0);
ans = min(ans, dsl.qry() + dsr.qry());
}
cout << C + ans << endl;
}
}
Compilation message
bridge.cpp: In function 'int32_t main()':
bridge.cpp:105:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for(int i = 0; i < x.size(); i++){
| ~~^~~~~~~~~~
bridge.cpp:109:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for(int i = 0; i + 1 < x.size(); i++){
| ~~~~~~^~~~~~~~~~
bridge.cpp:138:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<Person>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
138 | for(int i = 0; i < P.size(); i++){
| ~~^~~~~~~~~~
bridge.cpp:112:6: warning: unused variable 'ans' [-Wunused-variable]
112 | int ans = 1LL << 60;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
480 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
440 KB |
Output is correct |
4 |
Correct |
2 ms |
444 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
59 ms |
5688 KB |
Output is correct |
13 |
Correct |
152 ms |
12052 KB |
Output is correct |
14 |
Correct |
89 ms |
5948 KB |
Output is correct |
15 |
Correct |
82 ms |
7308 KB |
Output is correct |
16 |
Correct |
86 ms |
6588 KB |
Output is correct |
17 |
Correct |
127 ms |
12088 KB |
Output is correct |
18 |
Correct |
106 ms |
11660 KB |
Output is correct |
19 |
Correct |
135 ms |
11856 KB |
Output is correct |
20 |
Correct |
94 ms |
6760 KB |
Output is correct |
21 |
Correct |
116 ms |
11856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
432 KB |
Output is correct |
6 |
Correct |
1 ms |
436 KB |
Output is correct |
7 |
Correct |
1 ms |
440 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
432 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
436 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
448 KB |
Output is correct |
15 |
Correct |
3 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
436 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
392 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
432 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
436 KB |
Output is correct |
18 |
Correct |
2 ms |
440 KB |
Output is correct |
19 |
Correct |
2 ms |
444 KB |
Output is correct |
20 |
Correct |
3 ms |
596 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
3 ms |
572 KB |
Output is correct |
25 |
Correct |
102 ms |
5660 KB |
Output is correct |
26 |
Correct |
117 ms |
5852 KB |
Output is correct |
27 |
Correct |
237 ms |
13900 KB |
Output is correct |
28 |
Correct |
242 ms |
15248 KB |
Output is correct |
29 |
Correct |
245 ms |
15236 KB |
Output is correct |
30 |
Correct |
145 ms |
8392 KB |
Output is correct |
31 |
Correct |
125 ms |
6588 KB |
Output is correct |
32 |
Correct |
176 ms |
15240 KB |
Output is correct |
33 |
Correct |
170 ms |
14860 KB |
Output is correct |
34 |
Correct |
199 ms |
14816 KB |
Output is correct |
35 |
Correct |
134 ms |
6712 KB |
Output is correct |
36 |
Correct |
177 ms |
14920 KB |
Output is correct |
37 |
Correct |
105 ms |
5628 KB |
Output is correct |