結果

問題 No.1874 Minimum of Sum of Rectangles
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-03-17 01:24:07
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 208 ms / 3,000 ms
コード長 5,335 bytes
コンパイル時間 1,057 ms
コンパイル使用メモリ 140,012 KB
実行使用メモリ 15,964 KB
最終ジャッジ日時 2024-06-22 14:36:41
合計ジャッジ時間 8,333 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 69 ms
7,428 KB
testcase_06 AC 69 ms
7,248 KB
testcase_07 AC 74 ms
7,276 KB
testcase_08 AC 65 ms
7,272 KB
testcase_09 AC 103 ms
7,276 KB
testcase_10 AC 101 ms
7,380 KB
testcase_11 AC 101 ms
7,300 KB
testcase_12 AC 105 ms
7,252 KB
testcase_13 AC 101 ms
7,436 KB
testcase_14 AC 182 ms
15,964 KB
testcase_15 AC 188 ms
14,020 KB
testcase_16 AC 185 ms
13,956 KB
testcase_17 AC 187 ms
14,044 KB
testcase_18 AC 189 ms
14,020 KB
testcase_19 AC 191 ms
14,180 KB
testcase_20 AC 190 ms
14,088 KB
testcase_21 AC 187 ms
14,064 KB
testcase_22 AC 191 ms
15,176 KB
testcase_23 AC 186 ms
14,028 KB
testcase_24 AC 185 ms
14,116 KB
testcase_25 AC 183 ms
15,212 KB
testcase_26 AC 185 ms
13,980 KB
testcase_27 AC 192 ms
14,068 KB
testcase_28 AC 190 ms
14,152 KB
testcase_29 AC 190 ms
14,036 KB
testcase_30 AC 193 ms
14,060 KB
testcase_31 AC 194 ms
14,176 KB
testcase_32 AC 208 ms
14,136 KB
testcase_33 AC 190 ms
14,108 KB
testcase_34 AC 1 ms
6,940 KB
testcase_35 AC 1 ms
6,940 KB
testcase_36 AC 1 ms
6,944 KB
testcase_37 AC 104 ms
7,376 KB
testcase_38 AC 101 ms
7,280 KB
testcase_39 AC 102 ms
7,232 KB
testcase_40 AC 102 ms
7,264 KB
testcase_41 AC 102 ms
7,264 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