結果

問題 No.649 ここでちょっとQK!
ユーザー akakimidoriakakimidori
提出日時 2018-10-08 01:10:19
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 218 ms / 3,000 ms
コード長 4,328 bytes
コンパイル時間 370 ms
コンパイル使用メモリ 36,760 KB
実行使用メモリ 18,288 KB
最終ジャッジ日時 2024-04-20 18:25:57
合計ジャッジ時間 4,953 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 21 ms
5,376 KB
testcase_04 AC 215 ms
18,288 KB
testcase_05 AC 204 ms
17,720 KB
testcase_06 AC 213 ms
18,280 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 88 ms
8,320 KB
testcase_13 AC 86 ms
8,320 KB
testcase_14 AC 88 ms
8,192 KB
testcase_15 AC 93 ms
8,320 KB
testcase_16 AC 90 ms
8,960 KB
testcase_17 AC 104 ms
9,344 KB
testcase_18 AC 114 ms
10,112 KB
testcase_19 AC 126 ms
10,752 KB
testcase_20 AC 135 ms
11,264 KB
testcase_21 AC 145 ms
11,776 KB
testcase_22 AC 179 ms
12,552 KB
testcase_23 AC 172 ms
13,056 KB
testcase_24 AC 189 ms
14,208 KB
testcase_25 AC 206 ms
14,464 KB
testcase_26 AC 218 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,192 KB
testcase_31 AC 84 ms
8,704 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>
#include<math.h>

typedef struct scapegoatTreeNode{
  void *val;
  int size;
  struct scapegoatTreeNode *left;
  struct scapegoatTreeNode *right;
} sNode;

typedef struct scapegoatTreeHead{
  sNode *root;
  int del;
  size_t size;
  int (*cmp)(const void *,const void *);
} sTree;

sTree* newSTree(size_t size,int (*cmp)(const void *,const void *)){
  sTree *res=(sTree *)malloc(sizeof(sTree));
  res->root=NULL;
  res->del=0;
  res->size=size;
  res->cmp=cmp;
  return res;
}

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

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

void freeAllNode(sNode *r){
  if(r==NULL) return;
  freeAllNode(r->left);
  freeAllNode(r->right);
  freeNode(r);
  return;
}

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

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

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

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

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

sNode* insert_func(sNode *r,const void *v,sTree *t,int *depth,sNode **u){
  if(r==NULL) return *u=newNode(v,t->size);
  if(t->cmp(r->val,v)>=0){
    r->left=insert_func(r->left,v,t,depth,u);
  } else {
    r->right=insert_func(r->right,v,t,depth,u);
  }
  (*depth)++;
  update(r);
  return r;
}

sNode* searchScapegoat(sNode *r,sNode *u,sTree *t,int *cond){
  if(r==u) return r;
  int c=t->cmp(r->val,u->val);
  int cnt=0;
  if(c>=0){
    r->left=searchScapegoat(r->left,u,t,cond);
    cnt=getSize(r->left);
  } else {
    r->right=searchScapegoat(r->right,u,t,cond);
    cnt=getSize(r->right);
  }
  if(*cond && 2*getSize(r)<3*cnt){
    *cond=0;
    r=rebuild(r);
  }
  return r;
}

void insert(sTree *t,const void *v){
  const double f=1/1.5849625007211563;
  int depth=0;
  sNode *u=NULL;
  t->root=insert_func(t->root,v,t,&depth,&u);
  if(depth>log(getSize(t->root))*f){
    int cond=1;
    t->root=searchScapegoat(t->root,u,t,&cond);
  }
}

sNode* erase_func_findMax(sNode *r,sNode **max){
  if(r->right==NULL){
    *max=r;
    return r->left;
  }
  r->right=erase_func_findMax(r->right,max);
  update(r);
  return r;
}

sNode* erase_func(sNode *r,const void *v,sTree *t){
  if(r==NULL) return NULL;
  int c=t->cmp(r->val,v);
  if(c==0){
    sNode *f=r;
    if(r->left==NULL){
      r=r->right;
    } else if(r->right==NULL){
      r=r->left;
    } else {
      sNode *max;
      r->left=erase_func_findMax(r->left,&max);
      max->left=r->left;
      max->right=r->right;
      r=max;
    }
    freeNode(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 r;
}

void erase(sTree *t,const void *v){
  t->root=erase_func(t->root,v,t);
  t->del++;
  if(t->del*2>getSize(t->root)){
    t->root=rebuild(t->root);
    t->del=0;
  }
}

void search(sTree *t,int k,void *v){
  sNode *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);
  sTree *t=newSTree(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