#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
inline namespace
{
template <typename T, typename Cmp = less<T>>
using OrderedSet = tree<T, null_type, Cmp, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T = int>
struct PURS
{
PURS(int _n) : n(_n), tree(n + 1, 0) {}
PURS(const vector<T> &vec)
: PURS((int)vec.size())
{
for (int i = 0; i < n; i++)
add(i, vec[i]);
}
void add(int k, T x)
{
for (int p = k + 1; p <= n; p += p & -p)
tree[p] += x;
}
T sum(int l = 0) const { return sum(l, n); }
T sum(int l, int r) const { return acc(r) - acc(l); }
T get(int k) const { return sum(k, k + 1); }
void set(int k, T x) { add(k, x - get(k)); }
int size() const { return n; }
private:
int n;
vector<T> tree;
T acc(int k) const
{
T ret = 0;
for (int p = k; p; p -= p & -p)
ret += tree[p];
return ret;
}
};
} // namespace
void solve()
{
int n, k;
cin >> k >> n;
long res = 0;
if (k == 1)
{
vector<long> x;
for (int i = 0; i < n; i++)
{
char p, q;
long s, t;
cin >> p >> s >> q >> t;
if (p == q)
res += abs(s - t);
else
res += 1,
x.push_back(s),
x.push_back(t);
}
sort(x.begin(), x.end());
int m = (int)x.size();
long u = x[(m - 1) / 2];
for (long s : x)
res += abs(u - s);
}
else if (k == 2)
{
vector<long> vals;
vector<array<long, 2>> u;
for (int i = 0; i < n; i++)
{
char p, q;
long s, t;
cin >> p >> s >> q >> t;
if (s < t)
swap(s, t);
if (p == q)
res += s - t;
else
{
u.push_back({s, t});
vals.push_back(s),
vals.push_back(t);
res += 1;
}
}
sort(vals.begin(), vals.end());
vals.erase(unique(vals.begin(), vals.end()), vals.end());
int z = (int)vals.size();
auto idx = [&](long x) -> int
{ return (int)(lower_bound(vals.begin(), vals.end(), x) - vals.begin()); };
int m = (int)u.size();
sort(
u.begin(), u.end(),
[&](const auto &lhs, const auto &rhs) -> bool
{ return lhs[0] + lhs[1] < rhs[0] + rhs[1]; });
vector<long> pref(m + 1), suff(m + 1);
pref[0] = suff[m] = 0;
{
PURS<long> purs(z);
OrderedSet<pair<int, int>> oset;
for (int i = 0; i < m; i++)
{
for (long x : u[i])
{
int l = idx(x);
purs.add(l, x);
oset.insert(pair(l, (int)oset.size()));
}
int j = oset.find_by_order(i)->first;
pref[i + 1] = purs.sum(j + 1) - vals[j] * (2 * (i + 1) - (int)oset.order_of_key(pair(j, 0)) - (int)oset.order_of_key(pair(j + 1, 0))) - purs.sum(0, j);
}
}
{
PURS<long> purs(z);
OrderedSet<pair<int, int>> oset;
for (int i = m - 1; i >= 0; i--)
{
for (long x : u[i])
{
int l = idx(x);
purs.add(l, x);
oset.insert(pair(l, (int)oset.size()));
}
int j = oset.find_by_order(m - i - 1)->first;
suff[i] = purs.sum(j + 1) - vals[j] * (2 * (m - i) - (int)oset.order_of_key(pair(j, 0)) - (int)oset.order_of_key(pair(j + 1, 0))) - purs.sum(0, j);
}
}
long dist = LONG_MAX;
for (int i = 0; i <= m; i++)
dist = min(dist, pref[i] + suff[i]);
res += dist;
}
cout << res << '\n';
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(NULL);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
320 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
19 ms |
3152 KB |
Output is correct |
13 |
Correct |
42 ms |
4312 KB |
Output is correct |
14 |
Correct |
33 ms |
3536 KB |
Output is correct |
15 |
Correct |
24 ms |
2632 KB |
Output is correct |
16 |
Correct |
29 ms |
3656 KB |
Output is correct |
17 |
Correct |
32 ms |
4300 KB |
Output is correct |
18 |
Correct |
37 ms |
3912 KB |
Output is correct |
19 |
Correct |
41 ms |
4228 KB |
Output is correct |
20 |
Correct |
38 ms |
3920 KB |
Output is correct |
21 |
Correct |
37 ms |
4028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 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 |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
320 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
212 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 |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
360 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
448 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
460 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 |
2 ms |
492 KB |
Output is correct |
20 |
Correct |
3 ms |
460 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
3 ms |
456 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 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 |
212 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 |
340 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 |
320 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
3 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
3 ms |
500 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
3 ms |
468 KB |
Output is correct |
25 |
Correct |
301 ms |
18388 KB |
Output is correct |
26 |
Correct |
326 ms |
18424 KB |
Output is correct |
27 |
Correct |
469 ms |
20652 KB |
Output is correct |
28 |
Correct |
484 ms |
21404 KB |
Output is correct |
29 |
Correct |
479 ms |
21424 KB |
Output is correct |
30 |
Correct |
191 ms |
11460 KB |
Output is correct |
31 |
Correct |
299 ms |
19252 KB |
Output is correct |
32 |
Correct |
370 ms |
21408 KB |
Output is correct |
33 |
Correct |
218 ms |
20952 KB |
Output is correct |
34 |
Correct |
329 ms |
21348 KB |
Output is correct |
35 |
Correct |
284 ms |
19300 KB |
Output is correct |
36 |
Correct |
379 ms |
21320 KB |
Output is correct |
37 |
Correct |
303 ms |
18288 KB |
Output is correct |