結果

問題 No.1874 Minimum of Sum of Rectangles
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-03-17 01:24:07
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 234 ms / 3,000 ms
コード長 5,335 bytes
コンパイル時間 1,618 ms
コンパイル使用メモリ 124,208 KB
実行使用メモリ 16,760 KB
最終ジャッジ日時 2023-09-04 16:22:55
合計ジャッジ時間 11,143 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 3 ms
4,384 KB
testcase_05 AC 82 ms
7,536 KB
testcase_06 AC 83 ms
7,512 KB
testcase_07 AC 83 ms
7,544 KB
testcase_08 AC 79 ms
7,516 KB
testcase_09 AC 117 ms
7,556 KB
testcase_10 AC 118 ms
7,504 KB
testcase_11 AC 117 ms
7,560 KB
testcase_12 AC 118 ms
7,520 KB
testcase_13 AC 117 ms
7,504 KB
testcase_14 AC 226 ms
14,220 KB
testcase_15 AC 229 ms
14,244 KB
testcase_16 AC 226 ms
15,192 KB
testcase_17 AC 230 ms
14,288 KB
testcase_18 AC 234 ms
15,232 KB
testcase_19 AC 234 ms
16,000 KB
testcase_20 AC 233 ms
15,752 KB
testcase_21 AC 230 ms
14,260 KB
testcase_22 AC 226 ms
14,256 KB
testcase_23 AC 228 ms
14,276 KB
testcase_24 AC 233 ms
14,244 KB
testcase_25 AC 227 ms
15,992 KB
testcase_26 AC 229 ms
14,244 KB
testcase_27 AC 229 ms
15,828 KB
testcase_28 AC 229 ms
15,280 KB
testcase_29 AC 229 ms
16,760 KB
testcase_30 AC 229 ms
14,264 KB
testcase_31 AC 231 ms
14,260 KB
testcase_32 AC 229 ms
14,252 KB
testcase_33 AC 234 ms
14,268 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 117 ms
7,572 KB
testcase_38 AC 117 ms
7,560 KB
testcase_39 AC 117 ms
7,556 KB
testcase_40 AC 117 ms
7,524 KB
testcase_41 AC 118 ms
7,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


void bAdd(T)(T[] bit, int pos, T val)
in {
  assert(0 <= pos && pos < bit.length, "bAdd: 0 <= pos < |bit| must hold");
}
do {
  for (int x = pos; x < bit.length; x |= x + 1) {
    bit[x] += val;
  }
}

// sum of [0, pos)
T bSum(T)(T[] bit, int pos)
in {
  assert(0 <= pos && pos <= bit.length, "bSum: 0 <= pos <= |bit| must hold");
}
do {
  T ret = 0;
  for (int x = pos - 1; x >= 0; x = (x & (x + 1)) - 1) {
    ret += bit[x];
  }
  return ret;
}


void main() {
  try {
    for (; ; ) {
      const N = readInt;
      auto X = new long[N];
      auto Y = new long[N];
      foreach (i; 0 .. N) {
        X[i] = readLong;
        Y[i] = readLong;
      }
      
      auto ys = Y.dup;
      ys = ys.sort.uniq.array;
      const ysLen = cast(int)(ys.length);
      debug {
        writeln("ys = ", ys);
      }
      
      alias Entry = Tuple!(long, "x", int, "i");
      auto es = new Entry[N];
      foreach (i; 0 .. N) {
        es[i] = Entry(X[i], i);
      }
      es.sort;
      
      // 0: 1, 1: X, 2: Y, 3: XY
      long[4] totL, totR;
      long[][4] bitL, bitR;
      static foreach (h; 0 .. 4) {
        bitL[h] = new long[ysLen];
        bitR[h] = new long[ysLen];
      }
      foreach (i; 0 .. N) {
        const pos = ys.lowerBound(Y[i]);
        totR[0] += 1;
        totR[1] += X[i];
        totR[2] += Y[i];
        totR[3] += X[i] * Y[i];
        bitR[0].bAdd(pos, 1);
        bitR[1].bAdd(pos, X[i]);
        bitR[2].bAdd(pos, Y[i]);
        bitR[3].bAdd(pos, X[i] * Y[i]);
      }
      
      long ans = long.max;
      foreach (e; es) {
        {
          const i = e.i;
          const pos = ys.lowerBound(Y[i]);
          totL[0] += 1;
          totL[1] += X[i];
          totL[2] += Y[i];
          totL[3] += X[i] * Y[i];
          bitL[0].bAdd(pos, 1);
          bitL[1].bAdd(pos, X[i]);
          bitL[2].bAdd(pos, Y[i]);
          bitL[3].bAdd(pos, X[i] * Y[i]);
          totR[0] -= 1;
          totR[1] -= X[i];
          totR[2] -= Y[i];
          totR[3] -= X[i] * Y[i];
          bitR[0].bAdd(pos, -1);
          bitR[1].bAdd(pos, -X[i]);
          bitR[2].bAdd(pos, -Y[i]);
          bitR[3].bAdd(pos, -X[i] * Y[i]);
        }
/*
// min pos s.t. pred(sum of [0, pos))
//   assume pred(sum of [0, pos)) is non-decreasing
int bBinarySearch(alias pred, T)(T[] bit) {
  import core.bitop : bsr;
  import std.functional : unaryFun;
  alias predFun = unaryFun!pred;
  if (predFun(0)) return 0;
  int pos = 0;
  T sum = 0;
  foreach_reverse (e; 0 .. bsr(bit.length) + 1) {
    const x = (pos | 1 << e) - 1;
    if (x < bit.length && !predFun(sum + bit[x])) {
      pos |= 1 << e;
      sum += bit[x];
    }
  }
  return pos + 1;
}
*/
        {
          long tot;
          tot += (totL[0] * e.x - totL[1]);
          tot -= (totR[0] * e.x - totR[1]);
          int pos;
          long sum;
          foreach_reverse (f; 0 .. bsr(ysLen) + 1) {
            const pp = (pos | 1 << f) - 1;
            if (pp < ysLen) {
              long ss = sum;
              ss += (bitL[0][pp] * e.x - bitL[1][pp]);
              ss -= (bitR[0][pp] * e.x - bitR[1][pp]);
              if (2 * ss <= tot) {
                pos |= 1 << f;
                sum = ss;
              }
            }
          }
          assert(0 <= pos && pos <= ysLen);
          
          long[4] resL, resR;
          static foreach (h; 0 .. 4) {
            resL[h] = bitL[h].bSum(pos);
            resR[h] = bitR[h].bSum(pos);
          }
          const x = X[e.i];
          const y = ys[min(pos, ysLen - 1)];
          long cost;
          cost += (resL[0] * x * y - resL[1] * y - resL[2] * x + resL[3]);
          cost -= ((totL[0] - resL[0]) * x * y - (totL[1] - resL[1]) * y - (totL[2] - resL[2]) * x + (totL[3] - resL[3]));
          cost -= (resR[0] * x * y - resR[1] * y - resR[2] * x + resR[3]);
          cost += ((totR[0] - resR[0]) * x * y - (totR[1] - resR[1]) * y - (totR[2] - resR[2]) * x + (totR[3] - resR[3]));
          debug {
            writeln(e, " ", pos, " ", totL, " ", resL, " ", totR, " ", resR, " ", cost);
          }
          chmin(ans, cost);
        }
      }
      
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0