Submission #43625

#TimeUsernameProblemLanguageResultExecution timeMemory
43625comtalystPalembang Bridges (APIO15_bridge)C++14
22 / 100
779 ms46996 KiB
#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 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) template<typename T>using oset=tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>; char c1[5],c2[5]; pair<long,long> a[100005]; vector<long> pos(1,0); long sml[600005],smll[600005],smls[600005],smr[600005],smrl[600005],smrs[600005],best[100005]; umap<long,long> wh; 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; smls[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] || smls[tidx]){ sm_upl(tidx*2,l,mid,l,r,smll[tidx],smls[tidx]); sm_upl(tidx*2+1,mid+1,r,l,r,smll[tidx],smls[tidx]); smll[tidx] = smls[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; smrs[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] || smrs[tidx]){ sm_upr(tidx*2,l,mid,l,r,smrl[tidx],smrs[tidx]); sm_upr(tidx*2+1,mid+1,r,l,r,smrl[tidx],smrs[tidx]); smrl[tidx] = smrs[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] || smls[tidx]){ sm_upl(tidx*2,l,mid,l,r,smll[tidx],smls[tidx]); sm_upl(tidx*2+1,mid+1,r,l,r,smll[tidx],smls[tidx]); smll[tidx] = smls[tidx] = 0; } if(smrl[tidx] || smrs[tidx]){ sm_upr(tidx*2,l,mid,l,r,smrl[tidx],smrs[tidx]); sm_upr(tidx*2+1,mid+1,r,l,r,smrl[tidx],smrs[tidx]); smrl[tidx] = smrs[tidx] = 0; } return sm_get(tidx*2,l,mid,x) + sm_get(tidx*2+1,mid+1,r,x); } bool rs(pair<int,int> x,pair<int,int> y){ if(x.y == y.y){ return x.x < y.x; } return x.y < y.y; } main(){ long t,i,j,k,n,m,x,y,res=0,mn=MAX,v; oset<int> apos; scanf("%lld %lld",&k,&n); 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); } if(!n){ printf("%d\n",res); return 0; } sort(all(pos)); pos.resize(unique(all(pos))-pos.begin()); m = pos.size()-1; sort(a+1,a+1+n); for(i = 0; i < pos.size(); i++){ wh[pos[i]] = i; } for(i = 1; i <= n; i++){ x = a[i].x; y = a[i].y; apos.insert(x); apos.insert(y); sm_upl(1,1,m,1,wh[x],1,0); sm_upr(1,1,m,wh[y],m,1,0); best[i] = sm_get(1,1,m,wh[*apos.find_by_order(apos.size()/2)]); if(apos.size()/2+1 < apos.size()) best[i] = min(best[i],sm_get(1,1,m,wh[*apos.find_by_order(apos.size()/2+1)])); if(apos.size()/2-1 > 0) best[i] = min(best[i],sm_get(1,1,m,wh[*apos.find_by_order(apos.size()/2-1)])); } if(k == 1){ printf("%lld\n",res+best[n]); return 0; } memset(sml,0,sizeof sml); memset(smll,0,sizeof smll); memset(smls,0,sizeof smls); memset(smr,0,sizeof smr); memset(smrl,0,sizeof smrl); memset(smrs,0,sizeof smrs); apos.clear(); mn = best[n]; for(i = n; i >= 1; i--){ x = a[i].x; y = a[i].y; apos.insert(x); apos.insert(y); sm_upl(1,1,m,1,wh[x],1,0); sm_upr(1,1,m,wh[y],m,1,0); mn = min(mn,sm_get(1,1,m,wh[*apos.find_by_order(apos.size()/2)]) + best[i-1]); if(apos.size()/2+1 < apos.size()) mn = min(mn,sm_get(1,1,m,wh[*apos.find_by_order(apos.size()/2+1)]) + best[i-1]); if(apos.size()/2-1 > 0) mn = min(mn,sm_get(1,1,m,wh[*apos.find_by_order(apos.size()/2-1)]) + best[i-1]); } memset(sml,0,sizeof sml); memset(smll,0,sizeof smll); memset(smls,0,sizeof smls); memset(smr,0,sizeof smr); memset(smrl,0,sizeof smrl); memset(smrs,0,sizeof smrs); apos.clear(); sort(a+1,a+1+n,rs); for(i = n; i >= 1; i--){ x = a[i].x; y = a[i].y; apos.insert(x); apos.insert(y); sm_upl(1,1,m,1,wh[x],1,0); sm_upr(1,1,m,wh[y],m,1,0); mn = min(mn,sm_get(1,1,m,wh[*apos.find_by_order(apos.size()/2)]) + best[i-1]); if(apos.size()/2+1 < apos.size()) mn = min(mn,sm_get(1,1,m,wh[*apos.find_by_order(apos.size()/2+1)]) + best[i-1]); if(apos.size()/2-1 > 0) mn = min(mn,sm_get(1,1,m,wh[*apos.find_by_order(apos.size()/2-1)]) + best[i-1]); } printf("%lld\n",mn+res); return 0; }

Compilation message (stderr)

bridge.cpp:100:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
bridge.cpp: In function 'int main()':
bridge.cpp:122:20: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
   printf("%d\n",res);
                    ^
bridge.cpp:129:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i = 0; i < pos.size(); i++){
               ^
bridge.cpp:101:7: warning: unused variable 't' [-Wunused-variable]
  long t,i,j,k,n,m,x,y,res=0,mn=MAX,v;
       ^
bridge.cpp:101:11: warning: unused variable 'j' [-Wunused-variable]
  long t,i,j,k,n,m,x,y,res=0,mn=MAX,v;
           ^
bridge.cpp:101:36: warning: unused variable 'v' [-Wunused-variable]
  long t,i,j,k,n,m,x,y,res=0,mn=MAX,v;
                                    ^
bridge.cpp:104: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:106: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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...