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<stdio.h>
#include<algorithm>
#define SZ 262144
using namespace std;
int K, n;
long long Res, SS, D1[101000], D2[101000];
struct A{
int b, e, num;
bool operator<(const A &p)const{
return b+e<p.b+p.e;
}
}w[101000];
int Num[101000];
struct AA{
int x, num;
bool operator<(const AA &p)const{
return x<p.x;
}
}ord[201000];
long long IT[SZ+SZ+1][4];
void Ins(int pv, int a, int b){
a+=SZ;
while(a){
IT[a][pv]+=b;
a>>=1;
}
}
int Findx(int node, int g){
if(node >= SZ)return node-SZ;
if(IT[node*2][0] + IT[node*2][1] < g)return Findx(node*2+1,g-IT[node*2][0] - IT[node*2][1]);
return Findx(node*2,g);
}
long long Sum(int pv, int b, int e){
b+=SZ, e+=SZ;
long long r = 0;
while(b<=e){
if(b&1)r+=IT[b][pv];
if(!(e&1))r += IT[e][pv];
b=(b+1)>>1, e=(e-1)>>1;
}
return r;
}
long long Gap(int a){
long long t1 = Sum(0, 1, a-1), t2 = Sum(1, a+1, n+n);
return t1*ord[a].x - Sum(2, 1, a-1) + Sum(3,a+1,n+n) - t2*ord[a].x;
}
int main(){
int i, a, b, c = 0, e, mid, r, pv = 1;
scanf("%d%d",&K,&n);
char p1[3], p2[3];
for(i=1;i<=n;i++){
scanf("%s",p1);
scanf("%d",&a);
scanf("%s",p2);
scanf("%d",&b);
SS += max(a,b)-min(a,b);
if(p1[0] != p2[0]){
SS++;
c++;
w[c].b = min(a,b);
w[c].e = max(a,b);
w[c].num =c;
}
}
n=c;
sort(w+1,w+n+1);
for(i=1;i<=n;i++){
ord[i].x=w[i].b, ord[i].num = i;
ord[i + n].x = w[i].e, ord[i+n].num = i+n;
}
sort(ord+1,ord+n+n+1);
for(i=1;i<=n+n;i++){
a = ord[i].num;
if(a <= n)w[a].b = i;
else w[a-n].e = i;
}
for(i=1;i<=n;i++){
Ins(0, w[i].e, 1);
Ins(1, w[i].b, 1);
Ins(2, w[i].e, ord[w[i].e].x);
Ins(3, w[i].b, ord[w[i].b].x);
a = Findx(1, i);
D1[i] = Gap(a);
}
for(i=1;i<=n;i++){
Ins(0, w[i].e, -1);
Ins(1, w[i].b, -1);
Ins(2, w[i].e, -ord[w[i].e].x);
Ins(3, w[i].b, -ord[w[i].b].x);
}
for(i=n;i>=1;i--){
Ins(0, w[i].e, 1);
Ins(1, w[i].b, 1);
Ins(2, w[i].e, ord[w[i].e].x);
Ins(3, w[i].b, ord[w[i].b].x);
a = Findx(1, n+1-i);
D2[i] = Gap(a);
}
Res = SS + D1[n]*2;
if(K==1){
printf("%lld\n",Res);
return 0;
}
for(i=1;i<n;i++){
Res = min(Res, SS + (D1[i] + D2[i+1])*2);
}
printf("%lld\n",Res);
return 0;
}
Compilation message (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:48:25: warning: unused variable 'e' [-Wunused-variable]
int i, a, b, c = 0, e, mid, r, pv = 1;
^
bridge.cpp:48:28: warning: unused variable 'mid' [-Wunused-variable]
int i, a, b, c = 0, e, mid, r, pv = 1;
^
bridge.cpp:48:33: warning: unused variable 'r' [-Wunused-variable]
int i, a, b, c = 0, e, mid, r, pv = 1;
^
bridge.cpp:48:36: warning: unused variable 'pv' [-Wunused-variable]
int i, a, b, c = 0, e, mid, r, pv = 1;
^
bridge.cpp:49:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&K,&n);
^
bridge.cpp:52:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s",p1);
^
bridge.cpp:53:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&a);
^
bridge.cpp:54:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s",p2);
^
bridge.cpp:55:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%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... |