Submission #80726

#TimeUsernameProblemLanguageResultExecution timeMemory
80726xiaowuc1Palembang Bridges (APIO15_bridge)C++14
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>

/*
unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
mt19937 g1.seed(seed1);

ios_base::sync_with_stdio(false);
cin.tie(NULL);
*/
using namespace std;

const double PI = 2 * acos(0);

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, ll> pill;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef vector<vector<ll>> matrix;

char buf1[5];
char buf2[5];

ll onesolve(vector<pii>& v, int x) {
  ll ret = 0;
  for(pii out: v) {
    ret += abs(out.first-x) + abs(out.second-x);
  }
  return ret;
}

ll onesolve(vector<pii>& v) {
  if(v.empty()) return 0;
  vector<int> all;
  for(auto out: v) {
    all.push_back(out.first);
    all.push_back(out.second);
  }
  sort(all.begin(), all.end());
  return onesolve(v, all[all.size()/2]);
}

vector<int> allV;
class medianBIT {
  vector<ll> sumbit;
  vector<int> cntbit;
  priority_queue<int> greaterQ;
  priority_queue<int, vector<int>, greater<int>> lowerQ;
  ll totalSum;
  public:
  ll query() {
    if(totalSum == 0) return 0;
    int med = greaterQ.top();
    int idx = lower_bound(allV.begin(), allV.end(), med) - allV.begin();
    pill x = _bit_query(idx);
    ll rhssum = totalSum - x.second;
    int rhsamt = greaterQ.size() + lowerQ.size() - x.first;
    ll ans = (med * (ll)x.first - x.second) + (rhssum - med * (ll)rhsamt);
    return ans;
  }
  void _rebalance() {
    while(greaterQ.size() - lowerQ.size() > 1) {
      lowerQ.push(greaterQ.top());
      greaterQ.pop();
    }
    while(lowerQ.size() > greaterQ.size()) {
      greaterQ.push(lowerQ.top());
      lowerQ.pop();
    }
    if(lowerQ.empty()) return;
    while(lowerQ.top() > greaterQ.top()) {
      int lowlow = lowerQ.top();
      int highhigh = greaterQ.top();
      lowerQ.pop();
      greaterQ.pop();
      greaterQ.push(lowlow);
      lowerQ.push(highhigh);
    }
  }
  medianBIT() {
    sumbit.resize(allV.size() + 2);
    cntbit.resize(allV.size() + 2);
  }
  void _bit_update(int idx, int val) {
    totalSum += val;
    idx++;
    while(idx < sumbit.size()) {
      sumbit[idx] += val;
      if(val > 0) cntbit[idx]++;
      else cntbit[idx]--;
      idx += idx & -idx;
    }
  }
  pill _bit_query(int idx) {
    idx++;
    ll sumret = 0;
    int cntret = 0;
    while(idx) {
      sumret += sumbit[idx];
      cntret += cntbit[idx];
      idx -= idx & -idx;
    }
    return {cntret, sumret};
  }
  void insert(int v) {
    greaterQ.push(v);
    int idx = lower_bound(allV.begin(), allV.end(), v) - allV.begin();
    _bit_update(idx, v);
    _rebalance();
  }
  void erase(int v) {
    if(lower.count(v)) {
      if(--lower[v] == 0) lower.erase(v);
      lowersize--;
    }
    else {
      assert(greater.count(v));
      if(--greater[v] == 0) greater.erase(v);
      greatersize--;
    }
    int idx = lower_bound(allV.begin(), allV.end(), v) - allV.begin();
    _bit_update(idx, -v);
    _rebalance();
  }

};

bool piisort(pii a, pii b) {
  return a.first + a.second < b.first + b.second;
}

ll twosolve(vector<pii>& v) {
  if(v.empty()) return 0;
  for(auto out: v) {
    allV.push_back(out.first);
    allV.push_back(out.second);
  }
  sort(allV.begin(), allV.end());
  ll ret = onesolve(v, allV[allV.size()/2]);
  medianBIT lhs;
  medianBIT rhs;
  sort(v.begin(), v.end(), piisort);
  for(auto out: v) {
    rhs.insert(out.first);
    rhs.insert(out.second);
  }
  for(auto out: v) {
    rhs.erase(out.first);
    rhs.erase(out.second);
    lhs.insert(out.first);
    lhs.insert(out.second);
    ret = min(ret, lhs.query() + rhs.query());
  }
  return ret;
}

int main() {
  int k, n;
  scanf("%d%d\n", &k, &n);
  vector<pii> x;
  ll inc = 0;
  while(n--) {
    int a, b;
    scanf("%s %d %s %d\n", buf1, &a, buf2, &b);
    a++;
    b++;
    if(buf1[0] == buf2[0]) {
      inc += abs(b-a);
    }
    else {
      x.push_back({min(a,b), max(a,b)});
    }
  }
  if(k == 1) printf("%lld\n", onesolve(x) + inc + x.size());
  else printf("%lld\n", twosolve(x) + inc + x.size());
}

Compilation message (stderr)

bridge.cpp: In member function 'void medianBIT::_bit_update(int, int)':
bridge.cpp:88:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(idx < sumbit.size()) {
           ~~~~^~~~~~~~~~~~~~~
bridge.cpp: In member function 'void medianBIT::erase(int)':
bridge.cpp:113:8: error: 'lower' was not declared in this scope
     if(lower.count(v)) {
        ^~~~~
bridge.cpp:113:8: note: suggested alternative: 'lowerQ'
     if(lower.count(v)) {
        ^~~~~
        lowerQ
bridge.cpp:115:7: error: 'lowersize' was not declared in this scope
       lowersize--;
       ^~~~~~~~~
bridge.cpp:115:7: note: suggested alternative: 'lowerQ'
       lowersize--;
       ^~~~~~~~~
       lowerQ
In file included from /usr/include/c++/7/cassert:44:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:33,
                 from bridge.cpp:1:
bridge.cpp:118:21: error: missing template arguments before '.' token
       assert(greater.count(v));
                     ^
bridge.cpp:119:19: error: missing template arguments before '[' token
       if(--greater[v] == 0) greater.erase(v);
                   ^
bridge.cpp:119:36: error: missing template arguments before '.' token
       if(--greater[v] == 0) greater.erase(v);
                                    ^
bridge.cpp:120:7: error: 'greatersize' was not declared in this scope
       greatersize--;
       ^~~~~~~~~~~
bridge.cpp:120:7: note: suggested alternative: 'greaterQ'
       greatersize--;
       ^~~~~~~~~~~
       greaterQ
bridge.cpp: In function 'int main()':
bridge.cpp:160:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d\n", &k, &n);
   ~~~~~^~~~~~~~~~~~~~~~~~
bridge.cpp:165:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s %d %s %d\n", buf1, &a, buf2, &b);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~