Submission #288017

# Submission time Handle Problem Language Result Execution time Memory
288017 2020-09-01T07:53:39 Z erd1 Ideal city (IOI12_city) C++14
0 / 100
316 ms 262148 KB
#include<bits/stdc++.h>
using namespace std;
/*
#include<bits/extc++.h>
using namespace __gnu_pbds;
template<typename T>
using order_statistics_tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
*/
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define pb push_back
typedef int64_t lld;
typedef pair<int, int> pii;
typedef long double ld;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> p){
  return out << "(" << p.ff << ", " << p.ss << ")";
}

template<typename Iter>
ostream& outIt(ostream& out, Iter b, Iter e){
  for(Iter i = b; i != e; i++)
    out << (i==b?"":" ") << *i;
  return out;
}

template<typename T>
ostream& operator<<(ostream& out, vector<T> v){
  return outIt(out << '[', all(v)) << ']';
}


template<typename T1, typename T2>
ostream& operator<<(ostream& out, map<T1, T2> m){
  return outIt(out << '{', all(m)) << '}';
}

template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;

template<typename T1, typename T2>
pair<T1, T2>& operator+=(pair<T1, T2>& a, pair<T1, T2> p){
  return a.ff += p.ff, a.ss += p.ss, a;
}

typedef vector<pii>::iterator it;


vector<vector<int>> G;
min_heap<pair<int, pii>> q;
vector<bool> gone;
map<int, pii> toX(it begs, it ends, int targ, map<int, pii>& ans){ // points should be sorted;
  //outIt(cout << "toX", begs, ends) << " " << targ << endl;
  int n = ends-begs;
  G.resize(0); G.resize(n);
  for(int i = 0; i < n; i++){
    auto pi = begs[i];
    auto it1 = lower_bound(begs, ends, (pii){pi.ff+1, pi.ss}), it2 = lower_bound(begs, ends, (pii){pi.ff, pi.ss+1});
    if(it1 != ends && *it1 == (pii){pi.ff+1, pi.ss})
      G[i].pb(it1-begs), G[it1-begs].pb(i);
    if(it2 != ends && *it2 == (pii){pi.ff, pi.ss+1})
      G[i].pb(it2-begs), G[it2-begs].pb(i);
    if(pi.ff == targ)q.push({0, {i, pi.ss}});
  }
  //cout << G << endl;
  ans.clear();
  gone.resize(0);
  gone.resize(n);
  while(q.size()){
    auto a = q.top(); q.pop();
    if(gone[a.ss.ff])continue;
    gone[a.ss.ff] = true;
    ans[a.ss.ss] += {a.ff, 1};
    //cout << a << endl;
    for(auto i: G[a.ss.ff])
      if(!gone[i])q.push({a.ff+1, {i, a.ss.ss}});
  }
  //cout << ans << endl;
  return ans;
}

map<int, pii> mpa, mpb;
int calcSum(it begs, it ends){
  if(begs == ends)return 0;
  if(next(begs) == ends) return 0;
  int n = ends - begs;
  sort(begs, ends);
  int midx = begs[n/2].ff;
  it mid = lower_bound(begs, ends, (pii){midx, INT_MIN});
  int ans = 0;
  if(mid != begs && mid != ends){
    toX(begs, mid, midx-1, mpa); toX(mid, ends, midx, mpb);
    int acnt = 0, bcnt = 0, asum = 0, bsum = 0, ldis = -1;
    for(auto ita = mpa.begin(), itb = mpb.begin(); ita != mpa.end() || itb != mpb.end();){
      if(ita == mpa.end() || itb != mpb.end() && *itb < *ita){
        //cout << "inb" << *itb << acnt << endl;
        asum += acnt*(itb->ff-ldis), bsum += bcnt*(itb->ff-ldis);
        ans += asum*itb->ss.ss + itb->ss.ff*acnt + itb->ss.ss*acnt;
        bsum += itb->ss.ff, bcnt += itb->ss.ss;
        ldis = itb->ff;
        ++itb;
      }else {
        asum += acnt*(ita->ff-ldis), bsum += bcnt*(ita->ff-ldis);
        ans += bsum*ita->ss.ss + ita->ss.ff*bcnt + ita->ss.ss*bcnt;
        asum += ita->ss.ff, acnt += ita->ss.ss;
        ldis = ita->ff;
        ++ita;
      }
      //cout << acnt << " " << bcnt << " " << ans << endl;
    }
  }
  /*
  {
    int ansx = 0;
    for(auto ax: mpa)
      for(auto bx: mpb)
        ansx += (abs(ax.ff-bx.ff)+1)*ax.ss.ss*bx.ss.ss + ax.ss.ff*bx.ss.ss + ax.ss.ss*bx.ss.ff;
    assert(ansx == ans);
  }
  */
  //outIt(cout << "calc sum", begs, ends) << " " << ans << endl;
  for(auto i = begs; i != ends; i++)swap(i->ff, i->ss);
  return ans + calcSum(begs, mid) + calcSum(mid, ends);
}

int DistanceSum(int N, int *X, int *Y) {
  vector<pii> v;
  for(int i = 0; i < N; i++)v.pb({X[i], Y[i]});
  return calcSum(all(v));
  return N;

}

Compilation message

city.cpp: In function 'int calcSum(it, it)':
city.cpp:97:47: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   97 |       if(ita == mpa.end() || itb != mpb.end() && *itb < *ita){
      |                              ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 288 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 195 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 316 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 208 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -