#include<bits/stdc++.h>
#define task "C"
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
using namespace std;
const int MAXN = 1e5 + 5;
const ll INF = 1e18 + 5;
int n, k;
struct TRoad
{
char h, o;
ll ph, po;
}a[MAXN];
ll more = 0;
ll Cal(int p)
{
ll res = 0;
for (int i = 1; i <= n; i++)
{
if (a[i].h == a[i].o)
{
res += abs(a[i].ph - a[i].po);
} else
{
res += abs(a[i].ph - p) + abs(a[i].po - p) + 1;
}
}
return res;
}
void Input()
{
cin >> k >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i].h >> a[i].ph >> a[i].o >> a[i].po;
}
if (k == 1)
{
int l = 0, r = 1e9;
while (l <= r)
{
int mid = (l + r) / 2;
if (mid == 1e9)
break;
if (Cal(mid) >= Cal(mid + 1))
l = mid + 1;
else
r = mid - 1;
}
cout << Cal(l);
exit(0);
}
vector<int> cs;
for (int i = 1; i <= n; i++)
{
if (a[i].h == a[i].o)
{
more += abs(a[i].ph - a[i].po);
} else
{
cs.pb(i);
}
}
for (int i = 0; i < cs.size(); i++)
{
a[i + 1] = a[cs[i]];
}
n = cs.size();
}
int truoc[MAXN];
vector<ll> num;
int dif;
void NenSo()
{
for (int i = 1; i <= n; i++)
{
num.pb(a[i].ph);
num.pb(a[i].po);
}
sort(num.begin(), num.end());
num.erase(unique(num.begin(), num.end()), num.end());
for (int i = 1; i <= n; i++)
{
a[i].ph = lower_bound(num.begin(), num.end(), a[i].ph) - num.begin() + 1;
a[i].po = lower_bound(num.begin(), num.end(), a[i].po) - num.begin() + 1;
}
dif = num.size();
}
struct BIT
{
vector<ll> fe;
int sz;
void Init(int _sz)
{
sz = _sz;
fe.clear();
fe.resize(sz + 1);
}
void add(int p, ll val)
{
for (; p <= sz; p += p & -p)
{
fe[p] += val;
}
}
ll get(int r)
{
ll res = 0;
for (; r; r -= r & -r)
{
res += fe[r];
}
return res;
}
ll get(int l, int r)
{
return get(r) - get(l - 1);
}
};
ll prefsum[MAXN];
void Solve()
{
vector<int> cs(n);
iota(cs.begin(), cs.end(), 1);
sort(cs.begin(), cs.end(), [](int i, int j)
{
return a[i].po + a[i].ph < a[j].po + a[j].ph;
});
NenSo();
ll res = (n == 1 ? abs(num[a[1].po - 1] - num[a[1].ph - 1]) + 1 : INF);
if (n == 0)
res = 0;
BIT bit[2];
bit[0].Init(dif);
bit[1].Init(dif);
for (int i = 0; i + 1 < cs.size(); i++)
{
int p = cs[i];
//cout << num[a[p].ph - 1] << ' ' << num[a[p].po - 1] << '\n';
bit[0].add(a[p].po, 1);
bit[0].add(a[p].ph, 1);
bit[1].add(a[p].po, num[a[p].po - 1]);
bit[1].add(a[p].ph, num[a[p].ph - 1]);
int l = 1, r = dif;
while (l <= r)
{
int mid = (l + r) / 2;
if (bit[0].get(1, mid) >= i + 1)
r = mid - 1;
else
l = mid + 1;
}
prefsum[i] = -bit[1].get(1, l) + num[l - 1] * bit[0].get(1, l)
+ bit[1].get(l, dif) - num[l - 1] * bit[0].get(l, dif) + i + 1;
//cout << prefsum[i] << '\n';
}
bit[0].Init(dif);
bit[1].Init(dif);
for (int i = (int)cs.size() - 1; i >= 1; i--)
{
int p = cs[i];
bit[0].add(a[p].po, 1);
bit[0].add(a[p].ph, 1);
bit[1].add(a[p].po, num[a[p].po - 1]);
bit[1].add(a[p].ph, num[a[p].ph - 1]);
int l = 1, r = dif;
while (l <= r)
{
int mid = (l + r) / 2;
if (bit[0].get(1, mid) >= (int)cs.size() - i)
r = mid - 1;
else
l = mid + 1;
}
res = min(res, prefsum[i - 1] + -bit[1].get(1, l) + num[l - 1] * bit[0].get(1, l)
+ bit[1].get(l, dif) - num[l - 1] * bit[0].get(l, dif) + (int)cs.size() - i);
}
//cout << more << '\n';
cout << res + more;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(task".INP","r"))
{
freopen(task".INP","r",stdin);
freopen(task".OUT","w",stdout);
}
Input();
Solve();
}
Compilation message
bridge.cpp: In function 'void Input()':
bridge.cpp:72:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = 0; i < cs.size(); i++)
| ~~^~~~~~~~~~~
bridge.cpp: In function 'void Solve()':
bridge.cpp:154:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
154 | for (int i = 0; i + 1 < cs.size(); i++)
| ~~~~~~^~~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:207:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
207 | freopen(task".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:208:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
208 | freopen(task".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
368 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
356 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
23 ms |
3332 KB |
Output is correct |
13 |
Correct |
35 ms |
4300 KB |
Output is correct |
14 |
Correct |
25 ms |
3348 KB |
Output is correct |
15 |
Correct |
19 ms |
2632 KB |
Output is correct |
16 |
Correct |
31 ms |
3660 KB |
Output is correct |
17 |
Correct |
35 ms |
3736 KB |
Output is correct |
18 |
Correct |
33 ms |
3384 KB |
Output is correct |
19 |
Correct |
34 ms |
3916 KB |
Output is correct |
20 |
Correct |
34 ms |
3384 KB |
Output is correct |
21 |
Correct |
36 ms |
3584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
424 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
352 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
31 ms |
6280 KB |
Output is correct |
26 |
Correct |
63 ms |
5904 KB |
Output is correct |
27 |
Correct |
158 ms |
8940 KB |
Output is correct |
28 |
Correct |
163 ms |
9436 KB |
Output is correct |
29 |
Correct |
180 ms |
9448 KB |
Output is correct |
30 |
Correct |
103 ms |
5184 KB |
Output is correct |
31 |
Correct |
43 ms |
5992 KB |
Output is correct |
32 |
Correct |
105 ms |
9320 KB |
Output is correct |
33 |
Correct |
109 ms |
9432 KB |
Output is correct |
34 |
Correct |
163 ms |
9248 KB |
Output is correct |
35 |
Correct |
40 ms |
6264 KB |
Output is correct |
36 |
Correct |
147 ms |
9324 KB |
Output is correct |
37 |
Correct |
27 ms |
5936 KB |
Output is correct |