Pagini recente » Cod sursa (job #93148) | Cod sursa (job #1255608) | Cod sursa (job #638561) | Cod sursa (job #3212745) | Cod sursa (job #2728348)
#include <stdio.h>
#include <string.h>
int i, j, N;
unsigned int lowbias32(unsigned int x) {
x ^= x >> 16;
x *= 0x7feb352d;
x ^= x >> 15;
x *= 0x846ca68b;
x ^= x >> 16;
return x;
}
/*
// inverse
uint32_t
lowbias32_r(uint32_t x)
{
x ^= x >> 16;
x *= 0x43021123;
x ^= x >> 15 ^ x >> 30;
x *= 0x1d69e2a5;
x ^= x >> 16;
return x;
}*/
struct bucket {
unsigned int capacity;
unsigned int count;
unsigned int *members;
};
typedef struct bucket bucket;
bucket *hashtable[512];
unsigned int binary_search(unsigned int val, unsigned int *a, unsigned int start, unsigned int end) {
unsigned int N = end - start + 1, i, step;
for (step = 1; step < N; step <<= 1);
for (i = 0; step; step >>= 1){
if (i + step < N && a[start+ i + step] <= val) {
i += step;
}
}
return start+i;
}
void op_add(unsigned int index, unsigned int n) {
bucket *b = hashtable[index];
unsigned int pos = binary_search(n, b->members, 0, b->count), i;
if (b->members[pos] == n) {
return;
}
if (b->count == b->capacity) {
b->members = realloc(b->members, b->capacity<<3);
b->capacity <<= 1;
}
if (pos < b->count - 1) {
memmove(b->members+pos+1, b->members+pos, b->count-pos);
}
b->members[b->count] = n;
b->count++;
}
void op_remove(unsigned int index, unsigned int n) {
bucket *b = hashtable[index];
unsigned int pos = binary_search(n, b->members, 0, b->count);
if (b->members[pos] != n) {
return;
}
if (pos < b->count-1) {
memmove(b->members+pos, b->members+pos+1, b->count-pos-1);
}
b->members[b->count-1] = 0;
b->count--;
}
void op_question(unsigned int index, unsigned int n) {
bucket *b = hashtable[index];
unsigned int pos = binary_search(n, b->members, 0, b->count);
printf("%s", b->members[pos] == n ? "1\n" : "0\n");
}
void main() {
unsigned int n, o, flag;
freopen("hashuri.in", "r", stdin);
freopen("hashuri.out", "w", stdout);
unsigned int index;
void *op_fct[] = {op_add, op_remove, op_question};
for (i = 0; i < 512; i++) {
hashtable[i] = malloc(sizeof(bucket));
hashtable[i]->members = calloc(1300, sizeof(unsigned int));
hashtable[i]->capacity = 1300;
hashtable[i]->count = 0;
}
scanf("%d", &N);
i = N;
while (1) {
i--;
scanf("%u %u", &o, &n);
index = (lowbias32(n) & 0x1ff);
//if (o == 1)
((void (*)(unsigned int, unsigned int))op_fct[o-1])(index, n);
if (i == 0)
break;
}
return;
}