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;
typedef long long ll;
typedef tuple<ll, ll, ll> tp;
const ll inf = 1e18, L = 262144;
ll x, n, m, sub;
vector<ll> cp;
vector<tp> a;
struct segtree {
ll val[2*L], cnt[2*L];
void init () {
for(ll i=2*L;--i;) {
val[i] = 0;
cnt[i] = 0;
}
}
void upd (ll P, ll V) {
P += L;
while(P) {
val[P] += V;
cnt[P]++;
P /= 2;
}
}
ll get (ll K) {
ll P = 1, R = 0;
while(P < L) {
if(cnt[2*P] >= K) P = 2*P;
else {
K -= cnt[2*P];
R += val[2*P];
P = 2*P+1;
}
}
return K * cp[P-L] + R;
}
} seg;
ll cpr (ll X) {
return lower_bound(cp.begin(), cp.end(), X) - cp.begin();
}
void getdp (vector<ll> &D, vector<tp> &V) {
seg.init();
D.push_back(0);
ll I = 0;
for(auto &T : V) {
ll _, A, B;
tie(_, A, B) = T;
A = cpr(A);
B = cpr(B);
++I;
seg.upd(A, cp[A]);
seg.upd(B, cp[B]);
D.push_back(seg.get(2*I)-2*seg.get(I));
}
}
int main()
{
scanf("%lld%lld",&x,&n);
for(ll i=1;i<=n;i++) {
ll A, B;
char C[2], D[2];
scanf("%s%lld%s%lld",C,&A,D,&B);
if(C[0] == D[0]) sub += abs(A-B);
else {
a.push_back(make_tuple(A+B, A, B));
cp.push_back(A);
cp.push_back(B);
sub++;
}
}
sort(a.begin(), a.end());
n = a.size();
sort(cp.begin(), cp.end());
cp.erase(unique(cp.begin(), cp.end()), cp.end());
m = cp.size();
vector<ll> pre, suf;
getdp(pre, a);
reverse(a.begin(), a.end());
getdp(suf, a);
ll ans = inf;
for(ll i=0;i<=n;i++) {
ans = min(ans, pre[i] + suf[n-i]);
}
printf("%lld\n", (x==1 ? pre[n] : ans) + sub);
}
Compilation message (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:63:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld",&x,&n);
~~~~~^~~~~~~~~~~~~~~~~~
bridge.cpp:67:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s%lld%s%lld",C,&A,D,&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... |