結果

問題 No.953 席
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-12-16 02:47:42
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 435 ms / 2,000 ms
コード長 4,948 bytes
コンパイル時間 2,082 ms
コンパイル使用メモリ 185,148 KB
実行使用メモリ 16,784 KB
最終ジャッジ日時 2023-09-04 04:04:32
合計ジャッジ時間 11,715 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 242 ms
15,556 KB
testcase_03 AC 231 ms
16,484 KB
testcase_04 AC 235 ms
14,812 KB
testcase_05 AC 215 ms
15,012 KB
testcase_06 AC 240 ms
15,080 KB
testcase_07 AC 378 ms
13,536 KB
testcase_08 AC 409 ms
16,396 KB
testcase_09 AC 332 ms
13,296 KB
testcase_10 AC 111 ms
9,208 KB
testcase_11 AC 308 ms
13,792 KB
testcase_12 AC 329 ms
15,100 KB
testcase_13 AC 394 ms
16,152 KB
testcase_14 AC 303 ms
15,712 KB
testcase_15 AC 301 ms
14,972 KB
testcase_16 AC 301 ms
15,292 KB
testcase_17 AC 344 ms
15,824 KB
testcase_18 AC 323 ms
15,964 KB
testcase_19 AC 435 ms
16,784 KB
testcase_20 AC 325 ms
15,100 KB
testcase_21 AC 252 ms
14,400 KB
testcase_22 AC 337 ms
16,240 KB
testcase_23 AC 262 ms
15,236 KB
testcase_24 AC 267 ms
15,088 KB
testcase_25 AC 410 ms
14,996 KB
testcase_26 AC 191 ms
15,844 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 main() {
  try {
    for (; ; ) {
      const N = readInt();
      const K1 = readInt();
      const K2 = readInt();
      const Q = readInt();
      auto A = new long[Q];
      auto B = new long[Q];
      foreach (q; 0 .. Q) {
        A[q] = readLong();
        B[q] = readLong();
      }
      
      auto ans = new int[Q];
      
      int toDist(int x) {
        const d1 = abs(x - K1);
        const d2 = abs(x - K2);
        // return (d1 <= d2) ? (3 * d1 + 1) : (3 * d2 + 2);
        if (d1 <= d2) {
          return 3 * d1 + 1;
        } else {
          return 3 * d2 + 2;
        }
      }
      int toPos(int d) {
        if (d % 3 == 1) {
          return K1 + (K1 - K2) * (d / 3);
        } else {
          return K2 + (K2 - K1) * (d / 3);
        }
      }
      debug {
        foreach (x; 0 .. N + 2) {
          writeln(x, " ", toDist(x), " ", toPos(toDist(x)));
          assert(x == toPos(toDist(x)));
        }
      }
      
      auto filled = new bool[N + 2];
      auto setGood = new RedBlackTree!int;
      auto setBad = new RedBlackTree!int;
      foreach (x; 1 .. N + 1) {
        setGood.insert(toDist(x));
      }
      
      alias Event = Tuple!(long, "time", int, "type", int, "id");
      BinaryHeap!(Array!Event, "a > b") events;
      DList!int que;
      
      void updateSets(int pos) {
        foreach (x; pos - 2 .. pos + 2 + 1) {
          if (1 <= x && x <= N) {
            setGood.removeKey(toDist(x));
            setBad.removeKey(toDist(x));
            if (!filled[x]) {
              if (!filled[x - 1] && !filled[x + 1]) {
                setGood.insert(toDist(x));
              } else {
                setBad.insert(toDist(x));
              }
            }
          }
        }
        debug {
          writeln("  ", filled);
          writeln("  ", setGood, " ", setBad);
        }
      }
      void seat(long time, int id, int pos) {
        debug {
          writefln("seat  time = %s, id = %s, pos = %s", time, id, pos);
        }
        events.insert(Event(time + B[id], -1, id));
        ans[id] = pos;
        filled[pos] = true;
        updateSets(pos);
      }
      void leave(int id) {
        debug {
          writefln("leave  id = %s", id);
        }
        const pos = ans[id];
        filled[pos] = false;
        updateSets(pos);
      }
      
      foreach (q; 0 .. Q) {
        events.insert(Event(A[q], +1, q));
      }
      for (; !events.empty; ) {
        const e = events.front;
        events.removeFront;
        debug {
          writeln("e = ", e);
        }
        if (e.type > 0) {
          if (!setGood.empty || !setBad.empty) {
            assert(que.empty);
            // const pos = toPos(!setGood.empty ? setGood.front : setBad.front);
            int pos;
            if (!setGood.empty) {
              pos = toPos(setGood.front);
            } else {
              pos = toPos(setBad.front);
            }
            assert(!filled[pos]);
            seat(e.time, e.id, pos);
          } else {
            que ~= e.id;
          }
        } else {
          leave(e.id);
          if (events.empty || tuple(e.time, e.type) < tuple(events.front.time, events.front.type)) {
            for (; !que.empty && (!setGood.empty || !setBad.empty); ) {
              // const pos = toPos(!setGood.empty ? setGood.front : setBad.front);
              int pos;
              if (!setGood.empty) {
                pos = toPos(setGood.front);
              } else {
                pos = toPos(setBad.front);
              }
              seat(e.time, que.front, pos);
              que.removeFront;
            }
          }
        }
      }
      assert(que.empty);
      
      foreach (q; 0 .. Q) {
        writeln(ans[q]);
      }
    }
  } catch (EOFException e) {
  }
}
0