結果

問題 No.649 ここでちょっとQK!
ユーザー akakimidoriakakimidori
提出日時 2018-10-08 10:52:23
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 129 ms / 3,000 ms
コード長 3,721 bytes
コンパイル時間 386 ms
コンパイル使用メモリ 33,152 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-04-20 18:29:25
合計ジャッジ時間 3,849 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 22 ms
5,376 KB
testcase_04 AC 104 ms
11,264 KB
testcase_05 AC 109 ms
11,264 KB
testcase_06 AC 112 ms
11,264 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 0 ms
5,376 KB
testcase_12 AC 59 ms
5,632 KB
testcase_13 AC 58 ms
5,632 KB
testcase_14 AC 58 ms
5,632 KB
testcase_15 AC 61 ms
5,632 KB
testcase_16 AC 58 ms
5,888 KB
testcase_17 AC 66 ms
6,272 KB
testcase_18 AC 73 ms
6,784 KB
testcase_19 AC 79 ms
7,040 KB
testcase_20 AC 88 ms
7,424 KB
testcase_21 AC 93 ms
7,680 KB
testcase_22 AC 104 ms
8,192 KB
testcase_23 AC 112 ms
8,448 KB
testcase_24 AC 121 ms
8,832 KB
testcase_25 AC 125 ms
9,216 KB
testcase_26 AC 129 ms
9,600 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 51 ms
5,632 KB
testcase_31 AC 50 ms
5,888 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 0 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 0 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

//refet : http://www.cs.toronto.edu/~trebla/CSCB63-2018-Summer/WBT.pdf

typedef long long int int64;
typedef int64 nodeData;

typedef struct WeightBalancedTreeNode{
  nodeData val;
  int size;
  struct WeightBalancedTreeNode *left;
  struct WeightBalancedTreeNode *right;
} WBTNode;

typedef struct WeightBalancedTreeHead{
  WBTNode *root;
  int (*cmp)(const void *,const void *);
} WBTree;

WBTNode* newWBTNode(const nodeData *v){
  WBTNode *res=(WBTNode *)malloc(sizeof(WBTNode));
  res->val=*v;
  res->size=1;
  res->left=NULL;
  res->right=NULL;
  return res;
}

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

int getWeight(const WBTNode *t){
  return getSize(t)+1;
}

void freeWBTNode(WBTNode *t){
  free(t);
  return;
}

WBTree* newWBTree(int (*cmp)(const void *,const void *)){
  WBTree *res=(WBTree *)malloc(sizeof(WBTree));
  res->root=NULL;
  res->cmp=cmp;
  return res;
}

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

WBTNode* leftRotate(WBTNode *v){
  WBTNode *u=v->right;
  v->right=u->left;
  u->left=v;
  update(v);
  update(u);
  return u;
}

WBTNode* rightRotate(WBTNode *u){
  WBTNode *v=u->left;
  u->left=v->right;
  v->right=u;
  update(u);
  update(v);
  return v;
}

WBTNode* balance(WBTNode *t){
  if(t==NULL) return NULL;
  if(getWeight(t->left)*3<getWeight(t->right)){
    if(getWeight(t->right->left)>=2*getWeight(t->right->right)) t->right=rightRotate(t->right);
    t=leftRotate(t);
  } else if(getWeight(t->right)*3<getWeight(t->left)){
    if(getWeight(t->left->right)>=2*getWeight(t->left->left)) t->left=leftRotate(t->left);
    t=rightRotate(t);
  }
  return t;
}

WBTNode* insert_func(WBTNode *r,const nodeData *v,WBTree *t){
  if(r==NULL) return newWBTNode(v);
  if(t->cmp(&r->val,v)>=0){
    r->left=insert_func(r->left,v,t);
  } else {
    r->right=insert_func(r->right,v,t);
  }
  update(r);
  return balance(r);
}

void insert(WBTree *t,const nodeData *v){
  t->root=insert_func(t->root,v,t);
  return;
}

WBTNode* erase_func_max(WBTNode *r,WBTNode **max){
  if(r->right==NULL){
    *max=r;
    return r->left;
  }
  r->right=erase_func_max(r->right,max);
  update(r);
  return balance(r);
}

WBTNode* erase_func(WBTNode *r,const nodeData *v,WBTree *t){
  if(r==NULL) return NULL;
  int c=t->cmp(&r->val,v);
  if(c==0){
    WBTNode *f=r;
    if(r->left==NULL){
      r=r->right;
    } else if(r->right==NULL){
      r=r->left;
    } else {
      WBTNode *max;
      r->left=erase_func_max(r->left,&max);
      max->left=r->left;
      max->right=r->right;
      r=max;
    }
    freeWBTNode(f);
  } else if(c>0){
    r->left=erase_func(r->left,v,t);
  } else {
    r->right=erase_func(r->right,v,t);
  }
  update(r);
  return balance(r);
}

void erase(WBTree *t,const nodeData *v){
  t->root=erase_func(t->root,v,t);
}

void search(WBTree *t,int k,nodeData *res){
  if(k<1 || getSize(t->root)<k) return;
  WBTNode *r=t->root;
  while(1){
    if(getSize(r->left)<k){
      k-=getSize(r->left);
      if(k==1){
	*res=r->val;
	return;
      }
      k--;
      r=r->right;
    } else {
      r=r->left;
    }
  }
}

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?-1:1;
}

void run(void){
  int q,k;
  scanf("%d%d",&q,&k);
  WBTree *t=newWBTree(cmp);
  for(int i=0;i<q;i++){
    int c;
    scanf("%d",&c);
    if(c==1){
      int64 v;
      scanf("%lld",&v);
      insert(t,&v);
    } else if(getSize(t->root)<k){
      printf("-1\n");
    } else {
      int64 v;
      search(t,k,&v);
      printf("%lld\n",v);
      erase(t,&v);
    }
  }
}

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