This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#ifdef WIN_32
#define getchar_unlocked _getchar_nolock
#endif
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int K, N, tmpans = LLONG_MAX, ans, idx, _cnt, I[100005], S[100005], T[100005], leftval[100005], ft[200005], ft2[200005];
char P, Q;
vector<int> vec;
inline int ls(int x) { return x & -x; }
void upd(int p, int v) {
for (; p <= 2 * N; p += ls(p)) ft[p] += v, ft2[p]++;
}
int qry(int p) {
int r = 0;
for (; p; p -= ls(p)) r += ft[p];
return r;
}
int cnt(int p) {
int r = 0;
for (; p; p -= ls(p)) r += ft2[p];
return r;
}
struct slope {
ordered_set ch;
int m;
slope(int _m) : m(_m) {}
};
int disc(int x) {
return lower_bound(vec.begin(), vec.end(), x) - vec.begin() + 1;
}
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> K >> N;
for (int i = 1; i <= N; i++) {
cin >> P >> S[i] >> Q >> T[i];
S[i]++;
T[i]++;
if (P == Q) {
ans += llabs(S[i] - T[i]);
continue;
}
if (S[i] > T[i]) swap(S[i], T[i]);
I[++idx] = i;
}
N = idx;
sort(I + 1, I + 1 + N, [](const auto &a, const auto &b) {
return S[a] + T[a] < S[b] + T[b];
});
ans += idx;
for (int i = 1; i <= N; i++) vec.pb(S[I[i]]), vec.pb(T[I[i]]);
sort(vec.begin(), vec.end());
slope *run = new slope(0);
for (int i = 1; i <= N; i++) {
upd(disc(T[I[i]]), T[I[i]]);
upd(disc(S[I[i]]), S[I[i]]);
run->ch.insert(mp(T[I[i]], ++_cnt));
run->ch.insert(mp(T[I[i]], ++_cnt));
run->ch.insert(mp(S[I[i]], ++_cnt));
run->ch.insert(mp(S[I[i]], ++_cnt));
run->m += 2;
int val = run->ch.find_by_order(run->ch.size() - run->m)->first, curr = 0;
curr = (qry(2 * N) - qry(disc(val) - 1)) - val * (cnt(2 * N) - cnt(disc(val) - 1));
curr += cnt(disc(val) - 1) * val - qry(disc(val) - 1);
leftval[i] = curr;
}
if (K == 1) return cout << leftval[N] + ans << '\n', 0;
memset(ft, 0, sizeof ft);
memset(ft2, 0, sizeof ft2);
tmpans = leftval[N];
run = new slope(0);
for (int i = N; i >= 1; i--) {
upd(disc(T[I[i]]), T[I[i]]);
upd(disc(S[I[i]]), S[I[i]]);
run->ch.insert(mp(T[I[i]], ++_cnt));
run->ch.insert(mp(T[I[i]], ++_cnt));
run->ch.insert(mp(S[I[i]], ++_cnt));
run->ch.insert(mp(S[I[i]], ++_cnt));
run->m += 2;
int val = run->ch.find_by_order(run->ch.size() - run->m)->first, curr = 0;
curr = (qry(2 * N) - qry(disc(val) - 1)) - val * (cnt(2 * N) - cnt(disc(val) - 1));
curr += cnt(disc(val) - 1) * val - qry(disc(val) - 1);
tmpans = min(tmpans, leftval[i - 1] + curr);
}
cout << ans + tmpans << '\n';
}
Compilation message (stderr)
bridge.cpp:59:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
59 | main() {
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |