Pagini recente » Cod sursa (job #1550608) | Cod sursa (job #891237) | Cod sursa (job #1227567) | Cod sursa (job #3193242) | Cod sursa (job #2728491)
#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];
int binary_search(unsigned int val, unsigned int *a, unsigned int n) {
int start = 0;
int end = n - 1;
int mid;
while (start <= end) {
mid = (start + end)>>1;
if (a[mid] == val)
return mid;
else if (a[mid] < val)
start = mid + 1;
else
end = mid - 1;
}
return end + 1;
}
void op_add(unsigned int index, unsigned int n) {
bucket *b = hashtable[index];
unsigned int pos = binary_search(n, b->members, 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)<<2);
b->members[pos] = n;
}
else {
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, 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)<<2);
}
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, 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;
}