void update(int i, int v) {
	arr[trace[i]] = v;
	
	int block = block_number(i);
	int j, block_t = block * t, block_1_t = (block + 1) * t - 1;
	
	for (j = trace[i]; j > block_t; j--) {
		if (arr[j] > arr[j - 1]) {
			swap(arr[j], arr[j - 1]);
			trace[idx[j]] = j - 1;
			trace[idx[j - 1]] = j;
			swap(idx[j], idx[j - 1]);
		}
		else
			break;
	}

	for (j = trace[i]; (j < block_1_t) && (j < n_1); j++) {
		if (arr[j] < arr[j + 1]) {
			swap(arr[j], arr[j + 1]);
			trace[idx[j]] = j + 1;
			trace[idx[j + 1]] = j;
			swap(idx[j], idx[j + 1]);
		}
		else
			break;
	}
}
int get_answer(int i, int k) {
	if (i == -1) 
		return 0;

	int block = block_number(i);
	int block_1 = block - 1;
	int begin = 0, end = t - 1;
	int j, sum = 0;

	for (j = 0; j <= block_1; j++) {
		sum = sum + end - binary_search(begin, end, k) + 1;
		begin = end + 1;
		end = begin + (t - 1);
	}

	if (end > n_1)
		end = n_1;

	for (j = begin; j <= end /*&& j <= n_1*/; j++) {
		if (arr[j] <= k && idx[j] <= i)
			sum++;
	}

	return sum;
}
Ejemplo n.º 3
0
void Table::print ()
{
	Block *p;
	for (int i = 0; i < total_blocks; ++i)
	{
		printf("%d:", i);
		p = position[i].up;
		while (p != p->position) {
			printf(" %d", block_number(p));
			p = p->up;
		}
		putchar('\n');
	}
}
Ejemplo n.º 4
0
/* A block is a sequence that does not transverse boundary letters. 
 * For more details on boundary lettters see sep in preprocess.c */
long data_to_blocks(char *data, size_t sz, char ***blocks) {
    long block_n;

    // compute the number of blocks in data. 
    block_n = block_number(data, sz);

    if((*blocks = (char **) malloc(sizeof(char *)*block_n)) == NULL) {
        setlocation(LOCATION, __func__);
        return RERROR;
    }

    // populate blocks from tokenised data
    if((block_n = blocks_compute(data, sz, *blocks, block_n))==RERROR) {
        setlocation(LOCATION, __func__);
        return RERROR;
    }

    return block_n;
}