結果

問題 No.649 ここでちょっとQK!
ユーザー akakimidoriakakimidori
提出日時 2018-10-07 01:19:55
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 234 ms / 3,000 ms
コード長 4,038 bytes
コンパイル時間 419 ms
コンパイル使用メモリ 32,384 KB
実行使用メモリ 17,408 KB
最終ジャッジ日時 2024-04-20 18:08:27
合計ジャッジ時間 4,978 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 24 ms
5,376 KB
testcase_04 AC 120 ms
17,408 KB
testcase_05 AC 118 ms
17,408 KB
testcase_06 AC 122 ms
17,408 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 1 ms
5,376 KB
testcase_12 AC 81 ms
8,064 KB
testcase_13 AC 80 ms
8,192 KB
testcase_14 AC 80 ms
8,064 KB
testcase_15 AC 82 ms
8,064 KB
testcase_16 AC 84 ms
8,832 KB
testcase_17 AC 102 ms
9,216 KB
testcase_18 AC 108 ms
9,728 KB
testcase_19 AC 126 ms
10,368 KB
testcase_20 AC 137 ms
11,136 KB
testcase_21 AC 150 ms
11,648 KB
testcase_22 AC 160 ms
12,416 KB
testcase_23 AC 178 ms
12,928 KB
testcase_24 AC 195 ms
13,568 KB
testcase_25 AC 217 ms
14,208 KB
testcase_26 AC 234 ms
14,848 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 80 ms
8,064 KB
testcase_31 AC 89 ms
8,448 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long int int64;

//refer : http://dopal.cs.uec.ac.jp/okamotoy/lect/2005/DSA/avl.pdf

typedef struct AVLTreeNode{
  void *val;
  int size;
  int rank;
  struct AVLTreeNode *left;
  struct AVLTreeNode *right;
} AVLNode;

typedef struct AVLTreeHead{
  AVLNode *root;
  size_t valSize;
  int (*cmp)(const void *,const void *);
} AVLTree;

int getRank(AVLNode *t){
  return t==NULL?0:t->rank;
}

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

AVLNode* newAVLNode(void *v,size_t size){
  AVLNode *res=(AVLNode *)malloc(sizeof(AVLNode));
  res->val=malloc(size);
  memcpy(res->val,v,size);
  res->size=1;
  res->rank=1;
  res->left=NULL;
  res->right=NULL;
  return res;
}

void freeAVLNode(AVLNode *t){
  free(t->val);
  free(t);
  return;
}

AVLTree* newAVLTree(size_t size,int (*cmp)(const void *,const void *)){
  AVLTree *res=(AVLTree *)malloc(sizeof(AVLTree));
  res->root=NULL;
  res->valSize=size;
  res->cmp=cmp;
  return res;
}

int bias(AVLNode *t){
  if(t==NULL) return 0;
  return getRank(t->left)-getRank(t->right);
}

AVLNode* update(AVLNode *t){
  if(t==NULL) return NULL;
  int lrank=getRank(t->left);
  int rrank=getRank(t->right);
  t->rank=1+(lrank>rrank?lrank:rrank);
  t->size=1+getSize(t->left)+getSize(t->right);
  return t;
}

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

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

AVLNode* balance(AVLNode *t){
  if(t==NULL) return NULL;
  int b=bias(t);
  if(b<=-2){
    if(bias(t->right)<=0){
      t=leftRotate(t);
    } else {
      t->right=rightRotate(t->right);
      t=leftRotate(t);
    }
  } else if(b>=2){
    if(bias(t->left)>=0){
      t=rightRotate(t);
    } else {
      t->left=leftRotate(t->left);
      t=rightRotate(t);
    }
  }
  return t;
}

AVLNode* insert_func(AVLNode *r,void *v,AVLTree *t){
  if(r==NULL) return newAVLNode(v,t->valSize);
  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(AVLTree *t,void *v){
  t->root=insert_func(t->root,v,t);
  return;
}

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

AVLNode* erase_func(AVLNode *r,void *v,AVLTree *t){
  if(r==NULL) return NULL;
  int c=t->cmp(r->val,v);
  if(c==0){
    AVLNode *f=r;
    if(r->left==NULL){
      r=r->right;
    } else if(r->right==NULL){
      r=r->left;
    } else {
      AVLNode *max;
      r->left=erase_func_max(r->left,&max);
      max->left=r->left;
      max->right=r->right;
      r=max;
    }
    freeAVLNode(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(AVLTree *t,void *v){
  t->root=erase_func(t->root,v,t);
}

void search(AVLTree *t,int k,void *res){
  assert(1<=k && k<=getSize(t->root));
  AVLNode *r=t->root;
  while(1){
    if(getSize(r->left)<k){
      k-=getSize(r->left);
      if(k==1){
	memcpy(res,r->val,t->valSize);
	return;
      }
      k--;
      r=r->right;
    } else {
      r=r->left;
    }
  }
}

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);
  AVLTree *t=newAVLTree(sizeof(int64),cmp);
  while(q--){
    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);
      erase(t,&v);
      printf("%lld\n",v);
    }
  }
}

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