結果

問題 No.953 席
コンテスト
ユーザー hos.lyric
提出日時 2019-12-16 00:27:27
言語 D
(dmd 2.112.0)
コンパイル:
dmd -fPIE -m64 -w -wi -O -release -inline -I/opt/dmd/src/druntime/import/ -I/opt/dmd/src/phobos -L-L/opt/dmd/linux/lib64/ -fPIC _filename_
実行:
./Main
結果
WA  
実行時間 -
コード長 4,538 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,721 ms
コンパイル使用メモリ 160,684 KB
実行使用メモリ 14,844 KB
最終ジャッジ日時 2026-03-06 09:15:34
合計ジャッジ時間 14,794 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 WA * 1
other AC * 7 WA * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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);
      }
      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)));
        }
      }
      
      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 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;
        foreach (x; pos - 1 .. pos + 1 + 1) {
          setGood.removeKey(toDist(x));
          setBad.removeKey(toDist(x));
        }
        filled[pos] = true;
        foreach (x; pos - 1 .. pos + 1 + 1) {
          if (1 <= x && x <= N && !filled[x]) {
            if (!filled[x - 1] && !filled[x + 1]) {
              setGood.insert(toDist(x));
            } else {
              setBad.insert(toDist(x));
            }
          }
        }
      }
      void leave(int id) {
        debug {
          writefln("leave  id = %s", id);
        }
        const pos = ans[id];
        foreach (x; pos - 1 .. pos + 1 + 1) {
          setGood.removeKey(toDist(x));
          setBad.removeKey(toDist(x));
        }
        filled[pos] = false;
        foreach (x; pos - 1 .. pos + 1 + 1) {
          if (1 <= x && x <= N && !filled[x]) {
            if (!filled[x - 1] && !filled[x + 1]) {
              setGood.insert(toDist(x));
            } else {
              setBad.insert(toDist(x));
            }
          }
        }
      }
      
      foreach (q; 0 .. Q) {
        events.insert(Event(A[q], 0, q));
      }
      for (; !events.empty; ) {
        const e = events.front;
        events.removeFront;
        debug {
          writeln("e = ", e);
        }
        if (e.type == 0) {
          if (setGood.empty && setBad.empty) {
            que ~= e.id;
          } else {
            const pos = toPos(!setGood.empty ? setGood.front : setBad.front);
            seat(e.time, e.id, pos);
          }
        } else {
          leave(e.id);
          if (events.empty || [e.time, e.type] < [events.front.time, events.front.type]) {
            if (!que.empty) {
              const pos = toPos(!setGood.empty ? setGood.front : setBad.front);
              seat(e.time, que.front, pos);
              que.removeFront;
            }
          }
        }
      }
      assert(que.empty);
      
      foreach (q; 0 .. Q) {
        writeln(ans[q]);
      }
    }
  } catch (EOFException e) {
  }
}
0