Submission #232768

#TimeUsernameProblemLanguageResultExecution timeMemory
232768balbitPalembang Bridges (APIO15_bridge)C++14
22 / 100
438 ms114644 KiB
#include <bits/stdc++.h> #define ll long long using namespace std; #ifdef BALBIT #define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<" - ", _do(__VA_ARGS__) template<typename T> void _do(T && x){cerr<<x<<endl;} template<typename T, typename ...S> void _do(T && x, S&&...y){cerr<<x<<", "; _do(y...);} #define IOS() #else #define bug(...) #define IOS() ios::sync_with_stdio(0), cin.tie(0) #define endl '\n' #endif // BALBIT //#define int ll #define pii pair<ll, ll> #define f first #define s second #define ALL(x) x.begin(), x.end() #define SZ(x) (int)(x.size()) #define pb push_back vector<pii> v; int n; const int maxn = 1e5+3; vector<int> rbs, lbs;// right bounds and left bounds, separated and sorted ll rps[maxn], lps[maxn];// right bounds and left bounds, separated and sorted and ps struct node{ int l=0,r=0; ll val=0; int sz=0; } tree [maxn<<6]; int T_cnt = 0; //inline ll getval(node *&o){return o?o->val:0;} //inline int getsz(node *&o){return o?o->sz:0;} // void build(int &o, int l, int r) { o = T_cnt++; if (l == r) { return; } int mid = (l+r)/2; build(tree[o].l,l,mid); build(tree[o].r,mid+1,r); } inline void MO(int &i,int l,int r, ll num, ll v) //&的神奇功能,会把外面传入变量一起改变 { tree[T_cnt++]=tree[i]; //先继承上一棵树的信息 i=T_cnt-1; //修改子树关系 ++tree[i].sz; tree[i].val += v; if (l==r) return; int mid=(l+r)>>1; if (num<=mid) MO(tree[i].l,l,mid,num,v); //小于等于mid则在左边更新,右边直接与上一棵树共用 else MO(tree[i].r,mid+1,r,num,v); //大于mid,左边更新,右边共用 // tree[i].sz = tree[i].l.sz + tree[i].r.sz; // tree[i].val = tree[i].l.val + tree[i].r.val; } ll A=0, B=0; void QU(int o, int l, int r, int L, int R) { if (l > R || r<L) return; if (l>=L && r <=R) { A += tree[o].val; B += tree[o].sz; return; // tree[o].val, tree[o].sz; } ll mid = (l+r)/2; QU(tree[o].l, l,mid, L, R); QU(tree[o].r,mid+1,r, L, R); // return {A.f+B.f, A.s+B.s}; } vector<int> lhistory, rhistory; vector<int> lrev; map<ll, int> segmp; ll C(ll l, ll r) { if (l>r) swap(l,r); int L = lower_bound(ALL(rbs), l) - rbs.begin() -1;// right bounds less than l int R = upper_bound(ALL(lbs), r) - lbs.begin();// right bounds less than l ll re = 0; re += (lps[n] - lps[R]) - (n-R) * r; bug(re); bug(L+1); re += (L+1) * (l) - (rps[L+1]); bug(re); int lat = lower_bound(ALL(lrev), l, greater<int>()) - lrev.begin() - 1; int rat = lower_bound(ALL(rbs), r) - rbs.begin()-1; bug(lat, rat); auto sit = segmp.lower_bound(r+l); A=B=0; QU(lhistory[lat+1], 0, 1e5+3, 0, (*prev(sit)).s); re += A - B * l; A=B=0; QU(rhistory[rat+1], 0, 1e5+3, (*(sit)).s, 1e5+3); re += B*r-A; return re; } ll RE; vector<int> p; int m; node * lroot = 0, *rroot = 0; void go(int il, int ir, int jl, int jr) { if (il > ir || jl > jr) return; int mid = (il + ir)/2; int best = -1; ll bestval = 2e18+3; for (int j = jl; j<=min(mid, jr); ++j) { ll tt = C(p[mid], p[j]); RE = min (RE, tt); if (tt < bestval) { best = j; bestval = tt; } } go(il, mid-1, jl, best); go(mid+1, ir, best, jr); } signed main(){ IOS(); int k; cin>>k>>n; ll re = 0; map<int, int> hv; for (int i = 0; i<n; ++i) { char x, x2; int l, r; cin>>x>>l>>x2>>r; re += abs(r-l); if (x == x2) { }else{ if (r < l) swap(l,r); v.pb({l,r}); rbs.pb(r); lbs.pb(l); hv[l]--; hv[r]--; } } n = SZ(v); lhistory.pb(0); rhistory.pb(0); sort(ALL(v), [&](pii a, pii b){return a.f>b.f;}); vector<ll> fff; for (int i = 0; i<n; ++i) { fff.pb(v[i].f + v[i].s); } sort(ALL(fff)); for (int i = 0; i<n; ++i) { segmp[fff[i]] = i; } segmp[-1]=-1; build(lhistory.back(), 0, 1e5+3); for (int i = 0; i<n; ++i) { int topb = T_cnt; MO(lhistory.back(), 0, 1e5+3, segmp[v[i].f+v[i].s], v[i].f); lhistory.pb(topb); } sort(ALL(v), [&](pii a, pii b){return a.s<b.s;}); build(rhistory.back(), 0, 1e5+3); for (int i = 0; i<n; ++i) { // if (v[i].s == 6) bug(v[i].s, v[i].f+v[i].s); int topb = T_cnt; MO(rhistory.back(), 0, 1e5+3, segmp[v[i].f+v[i].s], v[i].s); rhistory.pb(topb); } sort(ALL(rbs)); sort(ALL(lbs)); lrev = lbs; sort(ALL(lrev), greater<int>()); for (int i = 0; i<n; ++i) { rps[i+1] = rps[i] + rbs[i]; lps[i+1] = lps[i] + lbs[i]; } // int L, R; // while (cin>>L>>R) { // cout<<C(L, R)<<endl; // } // // return 0; if (k == 1) { ll now = lps[n]; bug(now); ll ans = now; ll x = 0; ll frt = n; for (pii ele : hv) { ll X = ele.f; now -= (X-x) * (frt); bug(X, now); ans = min(ans, now); frt += ele.s; x=X; } // if (ans == 1000000000000000ll) ans = 0; cout<<re + ans*2 + n<<endl; }else{ m = SZ(hv); p.resize(m); RE = C(0,0); int _it = 0; for (pii ele : hv) { p[_it++] = ele.f; } go(0,m-1,0,m-1); // cout<<re + ans*2 + n<<endl; bug(RE); cout<<re + RE*2 + n<<endl; } } /* 2 5 B 0 A 4 B 1 B 3 A 5 B 7 B 2 A 6 B 1 A 7 */
#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...