Submission #1088532

#TimeUsernameProblemLanguageResultExecution timeMemory
1088532SunbaePalembang Bridges (APIO15_bridge)C++17
100 / 100
124 ms8184 KiB
#include <bits/stdc++.h> typedef long long ll; #define F first #define S second #define mp make_pair using namespace std; using pii = pair<int,int>; const int N = 1e5 + 5, M = 2e5 + 5; int sz, m, med[2][N], bit[M], cp[M]; ll BIT[M], Ans[2][N]; pii A[N]; int pos(int x){ return upper_bound(cp, cp+m, x) - cp;} void upd(int i, int v){ for(; i<M; i+=i&-i) bit[i] += v;} void UPD(int i, ll v){ for(; i<M; i+=i&-i) BIT[i] += v;} int qry(int i){ int res = 0; for(; i; i-=i&-i) res += bit[i]; return res;} ll QRY(int i){ ll res = 0; for(; i; i-=i&-i) res += BIT[i]; return res;} int _find(int k){ int i = 0, sum = 0; for(int j = 18; j>=0; --j){ if(i+(1<<j) < M && sum + bit[i+(1<<j)] < k) sum += bit[i+=(1<<j)]; } return i + 1; } signed main(){ int k, n; scanf("%d %d", &k, &n); ll tot = 0; for(int i = 0, p[2]; i<n; ++i){ char c[2]; scanf(" %c %d %c %d", c, p, c+1, p+1); if(p[0] > p[1]) swap(p[0], p[1]); if(c[0] == c[1]) tot += p[1] - p[0]; else{ cp[m++] = p[0]; cp[m++] = p[1]; A[sz++] = mp(p[0], p[1]); } } auto cmp = [&](pii x, pii y){ return x.F + x.S < y.F + y.S;}; sort(A, A+sz, cmp); sort(cp, cp+m); m = unique(cp, cp+m) - cp; int st = 0, ed = sz; for(int j = 0; j<2; ++j){ memset(bit, 0, sizeof(bit)); memset(BIT, 0, sizeof(BIT)); for(int i = st, cnt = 0; i != ed; !j ? ++i : --i){ pii idx = mp(pos(A[i].F), pos(A[i].S)); upd(idx.F, +1); upd(idx.S, +1); UPD(idx.F, +A[i].F); UPD(idx.S, +A[i].S); cnt += 2; int I = _find((cnt+1)/2); Ans[j][i] = (ll)qry(I) * (ll)cp[I-1] - QRY(I) + QRY(M-1) - QRY(I) - (ll)(qry(M-1) - qry(I)) * cp[I-1]; } st = sz-1; ed = -1; } tot += sz; if(k == 1 || !sz){ printf("%lld", tot + Ans[0][sz-1]); return 0;} ll mn = 2e18; for(int i = 0; i+1<sz; ++i) mn = min(mn, Ans[0][i] + Ans[1][i+1]); printf("%lld", tot + mn); }

Compilation message (stderr)

bridge.cpp: In function 'int main()':
bridge.cpp:25:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |  int k, n; scanf("%d %d", &k, &n);
      |            ~~~~~^~~~~~~~~~~~~~~~~
bridge.cpp:28:19: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |   char c[2]; scanf(" %c %d %c %d", c, p, c+1, p+1); if(p[0] > p[1]) swap(p[0], p[1]);
      |              ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...