ii find_min(node *t,int left,int beg,int end){

    if(t==NULL) return inf;

    ii r_left=left;
    ii r_right=left+t->cnt-1;

    ii sz=tree.get_count(t->left)+1;
	ii curr_ind=left+tree.get_count(t->left);

    if(beg<=r_left && end>=r_right){
        return t->diff;
    }

    ii ret=inf;

    if(beg>curr_ind)             ret=mini(ret,find_min(t->right,curr_ind+1,beg,end));
    else if(end<curr_ind)        ret=mini(ret,find_min(t->left,left,beg,end));

    else                         ret=mini(mini(find_min(t->left,left,beg,end)
                                               ,find_min(t->right,curr_ind+1,beg,end)),ret);

    if(curr_ind>beg && curr_ind<=end && t->left){
        ret=mini(ret,t->value - t->left->max_val);
    }
    if(curr_ind<end && curr_ind>=beg && t->right){
        ret=mini(ret,t->right->min_val - t->value);
    }

    return ret;
}
int find_count(node *t,int value){

	if(t==NULL) return 0;

	if(value==t->value)   return tree.get_count(t->left);
	else if(value > t->value)  return tree.get_count(t->left)
								+find_count(t->right,value)+1;
	else return find_count(t->left,value);
}
ii find_kth(node *t,int kth){

    if(tree.get_count(t)<kth){
		return -1;
    }

    int sz=tree.get_count(t->left)+1;

    if(sz==kth){
        return t->value;
    }

    if(kth<sz){
        return find_kth(t->left,kth);
    }
    else{
        return find_kth(t->right,kth-sz);
    }
}