結果

問題 No.649 ここでちょっとQK!
ユーザー akakimidoriakakimidori
提出日時 2018-10-08 17:58:49
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 358 ms / 3,000 ms
コード長 3,869 bytes
コンパイル時間 252 ms
コンパイル使用メモリ 34,304 KB
実行使用メモリ 17,944 KB
最終ジャッジ日時 2024-04-20 18:38:56
合計ジャッジ時間 6,503 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 23 ms
5,376 KB
testcase_04 AC 309 ms
17,944 KB
testcase_05 AC 297 ms
17,876 KB
testcase_06 AC 318 ms
17,840 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 139 ms
8,216 KB
testcase_13 AC 135 ms
8,320 KB
testcase_14 AC 130 ms
8,140 KB
testcase_15 AC 138 ms
8,244 KB
testcase_16 AC 131 ms
8,936 KB
testcase_17 AC 151 ms
9,220 KB
testcase_18 AC 172 ms
9,940 KB
testcase_19 AC 186 ms
10,424 KB
testcase_20 AC 202 ms
10,968 KB
testcase_21 AC 221 ms
11,648 KB
testcase_22 AC 244 ms
12,308 KB
testcase_23 AC 268 ms
12,912 KB
testcase_24 AC 279 ms
13,684 KB
testcase_25 AC 301 ms
14,240 KB
testcase_26 AC 358 ms
14,808 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 128 ms
8,312 KB
testcase_31 AC 125 ms
8,632 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<stdlib.h>
#include<stdio.h>
#include<string.h>

//Exercise8..10 :  http://opendatastructures.org/ods-python/8_2_Discussion_Exercises.html

typedef struct DynamiteTreeNode{
  void *val;
  int size;
  struct DynamiteTreeNode *left;
  struct DynamiteTreeNode *right;
} DNode;

typedef struct DynamiteTreeHead{
  DNode *root;
  size_t size;
  int (*cmp)(const void *,const void *);
} DTree;

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

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

void freeNode(DNode *t){
  free(t->val);
  free(t);
  return;
}

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

int explode(DNode *r){
  if(r==NULL) return 0;
  return 1.0/getSize(r)>((double)rand()+1.0)/((double)RAND_MAX+2.0);
}

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

int packToArray(DNode *r,DNode **array){
  if(r==NULL) return 0;
  int cnt=packToArray(r->left,array);
  array[cnt++]=r;
  return cnt+packToArray(r->right,array+cnt);
}

DNode* buildBalanced(DNode **array,int n){
  if(n<=0) return NULL;
  int m=n/2;
  DNode *res=array[m];
  res->left=buildBalanced(array,m);
  res->right=buildBalanced(array+m+1,n-1-m);
  update(res);
  return res;
}

DNode* rebuild(DNode *r){
  DNode **array=(DNode **)malloc(sizeof(DNode *)*getSize(r));
  int n=packToArray(r,array);
  DNode *res=buildBalanced(array,n);
  free(array);
  return res;
}

DNode* insert_func(DNode *r,const void *v,DTree *t,int burst){
  if(r==NULL) return newNode(v,t->size);
  int f=explode(r);
  if(t->cmp(r->val,v)>=0){
    r->left=insert_func(r->left,v,t,burst||f);
  } else {
    r->right=insert_func(r->right,v,t,burst||f);
  }
  update(r);
  if(!burst && f) r=rebuild(r);
  return r;
}

void insert(DTree *t,const void *v){
  t->root=insert_func(t->root,v,t,0);
}

DNode* erase_func_findMax(DNode *r,DNode **max,int burst){
  if(r->right==NULL){
    *max=r;
    return r->left;
  }
  int f=explode(r);
  r->right=erase_func_findMax(r->right,max,burst|f);
  update(r);
  if(!burst && f) r=rebuild(r);
  return r;
}

DNode* erase_func(DNode *r,const void *v,DTree *t,int burst){
  if(r==NULL) return NULL;
  int ex=explode(r);
  int c=t->cmp(r->val,v);
  if(c==0){
    DNode *f=r;
    if(r->left==NULL){
      r=r->right;
    } else if(r->right==NULL){
      r=r->left;
    } else {
      DNode *max;
      r->left=erase_func_findMax(r->left,&max,ex|burst);
      max->left=r->left;
      max->right=r->right;
      r=max;
    }
    freeNode(f);
  } else if(c>0){
    r->left=erase_func(r->left,v,t,ex|burst);
  } else {
    r->right=erase_func(r->right,v,t,ex|burst);
  }
  update(r);
  if(!burst && ex) r=rebuild(r);
  return r;
}

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

void search(DTree *t,int k,void *v){
  DNode *r=t->root;
  while(1){
    if(getSize(r->left)<k){
      k-=getSize(r->left);
      if(k==1){
	memcpy(v,r->val,t->size);
	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);
  DTree *t=newDTree(sizeof(int64),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