Submission #18874398


Source Code Expand

Copy
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 H = readInt();
      const W = readInt();
      const M = readInt();
      auto X = new int[M];
      auto Y = new int[M];
      foreach (i; 0 .. M) {
        X[i] = readInt() - 1;
        Y[i] = readInt() - 1;
      }
      
      auto as = new int[H];
      as[] = W;
      auto bs = new int[W];
      bs[] = H;
      foreach (i; 0 .. M) {
        chmin(as[X[i]], Y[i]);
        chmin(bs[Y[i]], X[i]);
      }
      debug {
        if (H <= 10 && W <= 10) {
          writeln("as = ", as);
          writeln("bs = ", bs);
        }
      }
      
      alias Entry = Tuple!(int, "a", int, "x");
      auto es = new Entry[bs[0]];
      foreach (x; 0 .. bs[0]) {
        es[x] = Entry(as[x], x);
      }
      es.sort!"a.a > b.a";
      
      long ans;
      foreach (x; 0 .. bs[0]) {
        ans += as[x];
      }
      foreach (y; 0 .. as[0]) {
        ans += bs[y];
      }
      auto bit = new long[H];
      {
        int j;
        foreach_reverse (y; 0 .. as[0]) {
          for (; j < bs[0] && es[j].a > y; ++j) {
            bit.bAdd(es[j].x, 1);
          }
          ans -= bit.bSum(bs[y]);
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

Submission Info

Submission Time
Task F - Rook on Grid
User hos_lyric
Language D (LDC 1.20.1)
Score 600
Code Size 2955 Byte
Status AC
Exec Time 113 ms
Memory 10340 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 29
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
hand_01.txt AC 27 ms 7688 KB
hand_02.txt AC 24 ms 7628 KB
hand_03.txt AC 6 ms 3920 KB
hand_04.txt AC 6 ms 5160 KB
hand_05.txt AC 4 ms 3112 KB
hand_06.txt AC 3 ms 3228 KB
random_01.txt AC 108 ms 10340 KB
random_02.txt AC 89 ms 7016 KB
random_03.txt AC 108 ms 10208 KB
random_04.txt AC 113 ms 10296 KB
random_05.txt AC 95 ms 7200 KB
random_06.txt AC 106 ms 10164 KB
random_07.txt AC 105 ms 10288 KB
random_08.txt AC 21 ms 7760 KB
random_09.txt AC 22 ms 7796 KB
random_10.txt AC 24 ms 7964 KB
random_11.txt AC 79 ms 5428 KB
random_12.txt AC 81 ms 5492 KB
random_13.txt AC 79 ms 5440 KB
random_14.txt AC 81 ms 5448 KB
random_15.txt AC 82 ms 5488 KB
random_16.txt AC 77 ms 5516 KB
random_17.txt AC 76 ms 5568 KB
random_18.txt AC 4 ms 3140 KB
random_19.txt AC 4 ms 3404 KB
random_20.txt AC 4 ms 3300 KB
sample_01.txt AC 4 ms 3428 KB
sample_02.txt AC 4 ms 3404 KB
sample_03.txt AC 21 ms 7480 KB