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;
#define x first
#define y second
#define umap unordered_map
#define pqueue priority_queue
#define mset multiset
#define mp make_pair
#define mt make_tuple
#define all(x) x.begin(),x.end()
#define long long long
#define MOD 1000000007
#define MAX (long)(1e16+5)
#define MIN (long)(-1e16-5)
#define FILEIN_ freopen("__in.txt","r",stdin)
#define FILEOUT_ freopen("__out.txt","w",stdout)
#define FILEIO_ freopen("__in.txt","r",stdin),freopen("__out.txt","w",stdout)
#define FILEIN(text) freopen(text,"r",stdin)
#define FILEOUT(text) freopen(text,"w",stdout)
#define FILEIO(text) freopen(text".in","r",stdin),freopen(text".out","w",stdout)
char c1[5],c2[5];
pair<long,long> a[100005];
vector<long> pos(1,0);
long sml[600005],smll[600005],smlls[600005],smr[600005],smrl[600005],smrls[600005],db[600005];
void sm_upl(long tidx,long l,long r,long x,long y,long w,long s){
long mid = (l+r)/2;
if(x > r || y < l){
return;
}
if(x <= l && y >= r){
smll[tidx] += w;
smlls[tidx] += (pos[y]-pos[r])*2*w + s;
sml[tidx] += (pos[y]-pos[r])*2*w + s + ((pos[r]-pos[l])*(pos[r]-pos[l]+1))*w;
return;
}
if(smll[tidx] || smlls[tidx]){
sm_upl(tidx*2,l,mid,l,r,smll[tidx],smlls[tidx]);
sm_upl(tidx*2+1,mid+1,r,l,r,smll[tidx],smlls[tidx]);
smll[tidx] = smlls[tidx] = 0;
}
sm_upl(tidx*2,l,mid,x,y,w,s);
sm_upl(tidx*2+1,mid+1,r,x,y,w,s);
sml[tidx] = sml[tidx*2] + sml[tidx*2+1];
}
void sm_upr(long tidx,long l,long r,long x,long y,long w,long s){
long mid = (l+r)/2;
if(x > r || y < l){
return;
}
if(x <= l && y >= r){
smrl[tidx] += w;
smrls[tidx] += (pos[l]-pos[x])*2*w + s;
smr[tidx] += (pos[l]-pos[x])*2*w + s + ((pos[r]-pos[l])*(pos[r]-pos[l]+1))*w;
return;
}
if(smrl[tidx] || smrls[tidx]){
sm_upr(tidx*2,l,mid,l,r,smrl[tidx],smrls[tidx]);
sm_upr(tidx*2+1,mid+1,r,l,r,smrl[tidx],smrls[tidx]);
smrl[tidx] = smrls[tidx] = 0;
}
sm_upr(tidx*2,l,mid,x,y,w,s);
sm_upr(tidx*2+1,mid+1,r,x,y,w,s);
smr[tidx] = smr[tidx*2] + smr[tidx*2+1];
}
long sm_get(long tidx,long l,long r,long x){
long mid = (l+r)/2;
if(x > r || x < l){
return 0;
}
if(x <= l && x >= r){
return sml[tidx] + smr[tidx];
}
if(smll[tidx] || smlls[tidx]){
sm_upl(tidx*2,l,mid,l,r,smll[tidx],smlls[tidx]);
sm_upl(tidx*2+1,mid+1,r,l,r,smll[tidx],smlls[tidx]);
smll[tidx] = smlls[tidx] = 0;
}
if(smrl[tidx] || smrls[tidx]){
sm_upr(tidx*2,l,mid,l,r,smrl[tidx],smrls[tidx]);
sm_upr(tidx*2+1,mid+1,r,l,r,smrl[tidx],smrls[tidx]);
smrl[tidx] = smrls[tidx] = 0;
}
return sm_get(tidx*2,l,mid,x) + sm_get(tidx*2+1,mid+1,r,x);
}
main(){
long t,i,j,k,n,m,x,y,res=0,mn=MAX,v;
scanf("%lld %lld",&k,&n);
if(k != 1) return 136;
for(i = 1; i <= n; i++){
scanf("%s %lld %s %lld",c1,&x,c2,&y);
x++;
y++;
if(x > y) swap(x,y);
res += abs(y-x);
if(c1[0] == c2[0]){
n--;
i--;
continue;
}
a[i] = {x,y};
res++;
pos.emplace_back(x);
pos.emplace_back(y);
}
sort(all(pos));
pos.resize(unique(all(pos))-pos.begin());
m = pos.size()-1;
sort(a+1,a+1+n);
for(i = 1; i <= n; i++){
x = a[i].x;
y = a[i].y;
// printf("> %d %d\n",x,y);
int st = (lower_bound(all(pos),x)-pos.begin());
for(j = st; j >= 1; j--){
db[j] += 2*abs(pos[st]-pos[j]);
}
st = (lower_bound(all(pos),y)-pos.begin());
for(j = st; j <= n; j++){
db[j] += 2*abs(pos[st]-pos[j]);
}
sm_upl(1,1,m,1,(lower_bound(all(pos),x)-pos.begin()),1,0);
sm_upr(1,1,m,(lower_bound(all(pos),y)-pos.begin()),m,1,0);
/* for(j = 1; j <= m; j++){
// sm_get(1,1,m,j);
printf("%d : %d\n",pos[j],sm_get(1,1,m,j));
}*/
}
for(i = 1; i <= n; i++){
mn = min(mn,db[i]);
}
if(mn == MAX) mn = 0;
printf("%lld\n",mn+res);
return 0;
}
Compilation message (stderr)
bridge.cpp:89:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
bridge.cpp: In function 'int main()':
bridge.cpp:90:7: warning: unused variable 't' [-Wunused-variable]
long t,i,j,k,n,m,x,y,res=0,mn=MAX,v;
^
bridge.cpp:90:36: warning: unused variable 'v' [-Wunused-variable]
long t,i,j,k,n,m,x,y,res=0,mn=MAX,v;
^
bridge.cpp:92:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld",&k,&n);
^
bridge.cpp:95:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s %lld %s %lld",c1,&x,c2,&y);
^
# | 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... |