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"
using namespace std;
int n;
int k;
vector<pair<long long int, long long int> > v;
vector<long long int> vv;
bool cmp(pair<int, int> a, pair<int, int> b) {
return a.first + a.second < b.first + b.second;
}
#define MAX 200002
long long int ans[MAX];
long long int bit[MAX];
int cnt[MAX];
void add(int i, long long int x) {
i++;
while (i < MAX) {
bit[i] += x;
cnt[i]++;
i += i&-i;
}
}
long long int sum(int i) {
long long int r = 0;
i++;
while (i) {
r += bit[i];
i -= i&-i;
}
return r;
}
long long int rng(int l, int r) {
long long int ret = sum(r);
if (l)ret -= sum(l - 1);
return ret;
}
int get_id(long long int val) {
return lower_bound(vv.begin(), vv.end(),val) - vv.begin();
}
int sum_cnt(int idx) {
int r = 0;
idx++;
while (idx) {
r += cnt[idx];
idx -= idx&-idx;
}
return r;
}
int rng_cnt(int l, int r) {
int ret = sum_cnt(r);
if (l)ret -= sum_cnt(l - 1);
return ret;
}
long long int tmp[MAX];
int main() {
cin >> k >> n;
long long int ans = 0;
for (int i = 0; i < n; i++) {
int a, b;
char A[2], B[2];
scanf("%s%d%s%d", A, &a, B, &b);
if (A[0] == B[0]) {
ans += abs(a-b);
}
else {
ans++;
tie(a, b) = minmax(a, b);
v.push_back(make_pair(a, b));
vv.push_back(a);
vv.push_back(b);
}
}
sort(vv.begin(), vv.end());
vv.erase(unique(vv.begin(), vv.end()), vv.end());
sort(v.begin(), v.end(), cmp);
long long int AA = LLONG_MAX;
{
int idx = 0;
int cn = 0;
for (int i = 0; i < vv.size(); i++) {
while (idx < v.size() && (v[idx].first + v[idx].second) <= vv[i] * 2) {
add(get_id(v[idx].first), v[idx].first);
add(get_id(v[idx].second), v[idx].second);
cn++;
idx++;
}
int mint = 0;
int maxt = vv.size() - 1;
while (mint + 1 < maxt) {
int mid = (mint + maxt) >> 1;
if (sum_cnt(mid) >= cn) {
maxt = mid;
}
else {
mint = mid;
}
}
if (sum_cnt(mint) >= cn) {
maxt = mint;
}
else{
mint = maxt;
}
long long int sm =rng_cnt(0, mint)*vv[mint] - rng(0, mint);
sm += rng(mint + 1, vv.size() - 1) - rng_cnt(mint+1,vv.size()-1)*vv[mint];
tmp[i]=sm;
}
AA = min(AA, tmp[vv.size() - 1]);
}
{
memset(cnt, 0, sizeof(cnt));
memset(bit, 0, sizeof(bit));
int idx = v.size()-1;
int cn = 0;
for (int i = vv.size()-1; i>=0; i--) {
while (idx >=0 && (v[idx].first + v[idx].second) >= vv[i] * 2) {
add(get_id(v[idx].first), v[idx].first);
add(get_id(v[idx].second), v[idx].second);
cn++;
idx--;
}
int mint = 0;
int maxt = vv.size() - 1;
while (mint + 1 < maxt) {
int mid = (mint + maxt) >> 1;
if (sum_cnt(mid) >= cn) {
maxt = mid;
}
else {
mint = mid;
}
}
if (sum_cnt(mint) >= cn) {
maxt = mint;
}
else {
mint = maxt;
}
long long int sm = rng_cnt(0, mint)*vv[mint] - rng(0, mint);
sm += rng(mint + 1, vv.size() - 1) - rng_cnt(mint + 1, vv.size() - 1)*vv[mint];
if (i)sm += tmp[i - 1];
AA = min(AA, sm);
}
ans += AA;
}
if (k == 1) {
printf("%lld\n", ans - AA + tmp[vv.size()-1]);
return 0;
}
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:92:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < vv.size(); i++) {
~~^~~~~~~~~~~
bridge.cpp:93:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (idx < v.size() && (v[idx].first + v[idx].second) <= vv[i] * 2) {
~~~~^~~~~~~~~~
bridge.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s%d%s%d", A, &a, B, &b);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |