結果

問題 No.649 ここでちょっとQK!
ユーザー akakimidoriakakimidori
提出日時 2018-12-09 03:04:28
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 132 ms / 3,000 ms
コード長 6,808 bytes
コンパイル時間 738 ms
コンパイル使用メモリ 31,432 KB
実行使用メモリ 11,288 KB
最終ジャッジ日時 2023-10-13 11:35:24
合計ジャッジ時間 5,169 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 21 ms
4,352 KB
testcase_04 AC 128 ms
11,256 KB
testcase_05 AC 125 ms
11,288 KB
testcase_06 AC 120 ms
11,264 KB
testcase_07 AC 0 ms
4,352 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 0 ms
4,352 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 60 ms
5,548 KB
testcase_13 AC 58 ms
5,684 KB
testcase_14 AC 61 ms
5,588 KB
testcase_15 AC 61 ms
5,528 KB
testcase_16 AC 58 ms
6,020 KB
testcase_17 AC 68 ms
6,300 KB
testcase_18 AC 71 ms
6,656 KB
testcase_19 AC 79 ms
7,076 KB
testcase_20 AC 89 ms
7,268 KB
testcase_21 AC 102 ms
7,820 KB
testcase_22 AC 110 ms
8,196 KB
testcase_23 AC 124 ms
8,576 KB
testcase_24 AC 115 ms
8,932 KB
testcase_25 AC 127 ms
9,308 KB
testcase_26 AC 132 ms
9,624 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 51 ms
5,560 KB
testcase_31 AC 50 ms
5,832 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 0 ms
4,348 KB
testcase_35 AC 0 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct WBTNode {
  size_t size;
  struct WBTNode *parent;
  struct WBTNode *left;
  struct WBTNode *right;
  unsigned long long int val[];
} WBTNode;

WBTNode* newNode (const void *val, const size_t size) {
  WBTNode *r = (WBTNode *) malloc (sizeof (WBTNode) + size);
  r -> size = 1;
  r -> parent = NULL;
  r -> left = NULL;
  r -> right = NULL;
  memcpy (r -> val, val, size);
  return r;
}

int getSize (const WBTNode *t) {
  return t == NULL ? 0 : t -> size;
}

int getBias (const WBTNode *t, const int b) {
  if (t == NULL) return 0;
  int l = getSize (t -> left);
  int r = getSize (t -> right);
  if (3 * l >= r && l <= 3 * r) {
    return 0;
  }
  return 3 * l < r ? -2 : 2;
}

void update (WBTNode *t) {
  t -> size = getSize (t -> left) + getSize (t -> right) + 1;
}

int isLeftChild (const WBTNode *t) {
  return t != NULL && t -> parent != NULL && t -> parent -> left == t;
}

void concatL (WBTNode *parent, WBTNode *child) {
  if(child != NULL) {
    child -> parent = parent;
  }
  if(parent != NULL) {
    parent -> left = child;
  }
}

void concatR (WBTNode *parent, WBTNode *child) {
  if(child != NULL) {
    child -> parent = parent;
  }
  if(parent != NULL) {
    parent -> right = child;
  }
}

WBTNode* rightRotate (WBTNode *v) {
  WBTNode *u = v -> left;
  concatL (v, u -> right);
  if (isLeftChild (v)) {
    concatL (v -> parent, u);
  } else {
    concatR (v -> parent, u);
  }
  concatR (u, v);
  update (v);
  update (u);
  return u;
}

WBTNode* leftRotate (WBTNode *u) {
  WBTNode *v = u -> right;
  concatR (u, v -> left);
  if (isLeftChild (u)) {
    concatL (u -> parent, v);
  } else {
    concatR (u -> parent, v);
  }
  concatL (v, u);
  update (u);
  update (v);
  return v;
}

WBTNode* balance (WBTNode *t) {
  const short b = getBias (t, 3);
  if (-1 <= b && b <= 1) {
    return t;
  }
  if (b < -1) {
    if (getBias (t -> right, 2) >= 1) {
      rightRotate (t -> right);
    }
    return leftRotate (t);
  } else {
    if(getBias (t -> left, 2) <= -1){
      leftRotate (t -> left);
    }
    return rightRotate (t);
  }
}

WBTNode* saveTree (WBTNode *t) {
  WBTNode *now = t;
  WBTNode *before = NULL;
  while (now != NULL) {
    update (now);
    before = balance (now);
    now = before -> parent;
  }
  return before;
}

typedef struct MultiSet_WBT {
  WBTNode *root;
  size_t valSize;
  int (*cmp) (const void *, const void *);
} multiset;

multiset* newMultiSet (const size_t size, int (*cmp) (const void *, const void *)) {
  multiset *mset = (multiset *)malloc(sizeof (multiset));
  mset -> root = NULL;
  mset -> valSize = size;
  mset -> cmp = cmp;
  return mset;
}

int getSetSize (const multiset *set) {
  return getSize (set -> root);
}

WBTNode* getMaxRef (const multiset *set) {
  if (set -> root == NULL) {
    return NULL;
  }
  WBTNode *now = set -> root;
  while (now -> right != NULL) {
    now = now -> right;
  }
  return now;
}

WBTNode* getMinRef (multiset *set) {
  if (set -> root == NULL) {
    return NULL;
  }
  WBTNode *now = set -> root;
  while (now -> left != NULL) {
    now = now -> left;
  }
  return now;
}

WBTNode* getAtRef (multiset *set, int k) {
  if (k <= 0 || getSetSize (set) < k) {
    return NULL;
  }
  WBTNode *now = set -> root;
  while (1) {
    int lSize = getSize (now -> left);
    if (k == lSize + 1){
      return now;
    }
    if (k <= lSize) {
      now = now -> left;
    } else {
      k -= lSize + 1;
      now = now -> right;
    }
  }
}

WBTNode* nextRef(WBTNode *t){
  if (t -> right != NULL) {
    WBTNode *s = t -> right;
    while (s -> left != NULL) {
      s = s-> left;
    }
    return s;
  }
  WBTNode *now = t -> parent;
  WBTNode *before = t;
  while (now != NULL && now -> left != before) {
    before = now;
    now = now -> parent;
  }
  return now;
}

WBTNode* prevRef(WBTNode *t){
  if (t -> left != NULL) {
    WBTNode *s = t -> left;
    while (s -> right != NULL) {
      s = s-> right;
    }
    return s;
  }
  WBTNode *now = t -> parent;
  WBTNode *before = t;
  while (now != NULL && now -> right != before) {
    before = now;
    now = now -> parent;
  }
  return now;
}

WBTNode* insert (multiset *set, const void *val) {
  WBTNode * const in = newNode (val, set -> valSize);
  if (set -> root == NULL) {
    set -> root = in;
    return in;
  }
  int (*cmp) (const void *, const void *) = set -> cmp;
  WBTNode *before = NULL;
  WBTNode *now = set -> root;
  int cmpRes = 0;
  while (now != NULL) {
    before = now;
    cmpRes = cmp (in -> val, now -> val);
    now = cmpRes <= 0 ? now -> left : now -> right;
  }
  if (cmpRes <= 0) {
    concatL (before, in);
  } else {
    concatR (before, in);
  }
  set -> root = saveTree (before);
  return in;
}

void eraseByRef (multiset * const set, WBTNode * const t) {
  if (t == NULL) {
    return;
  }
  if(t -> left == NULL && t -> right == NULL) {
    if(isLeftChild (t)) {
      concatL (t -> parent, NULL);
    } else {
      concatR (t -> parent, NULL);
    }
    set -> root = saveTree (t -> parent);
    free (t);
  } else if (t -> left == NULL) {
    if (isLeftChild (t)) {
      concatL (t -> parent, t -> right);
    } else {
      concatR (t -> parent, t -> right);
    }
    set -> root = saveTree (t -> right);
    free (t);
  } else if (t -> right == NULL) {
    if (isLeftChild (t)) {
      concatL (t -> parent, t -> left);
    } else {
      concatR (t -> parent, t -> left);
    }
    set -> root = saveTree (t -> left);
    free (t);
  } else {
    WBTNode *max = t -> left;
    while (max -> right != NULL) {
      max = max -> right;
    }
    WBTNode *p = NULL;
    if (max != t -> left) {
      concatR (max -> parent, max -> left);
      t -> left -> parent = NULL;
      p = saveTree (max -> parent);
    } else {
      p = t -> left -> left;
    }
    concatL (max, p);
    concatR (max, t -> right);
    if(isLeftChild (t)) {
      concatL (t -> parent, max);
    } else {
      concatR (t -> parent, max);
    }
    set -> root = saveTree (max);
    free (t);
  }
}

void eraseAt (multiset *set, const int k) {
  if (k < 1 || getSize (set -> root) < k) {
    return;
  }
  eraseByRef (set, getAtRef (set, k));
}

typedef WBTNode Ref;

typedef long long int int64;

int cmp(const void *a,const void *b){
  int64 p=*(int64 *)a;
  int64 q=*(int64 *)b;
  return p==q?0:p-q<0?-1:1;
}

void run(void){
  int q,k;
  scanf("%d%d",&q,&k);
  multiset *set=newMultiSet(sizeof(int64),cmp);
  while(q--){
    int c;
    scanf("%d",&c);
    if(c==1){
      int64 v;
      scanf("%lld",&v);
      insert(set,&v);
    } else if(getSetSize(set)<k){
      printf("-1\n");
    } else {
      Ref *p=getAtRef(set,k);
      int64 v=*(int64 *)p->val;
      eraseByRef(set, p);
      printf("%lld\n",v);
    }
  }
}

int main(void){
  run();
  return 0;
}
0