-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdedup_sort_bitmap_tree.c
More file actions
executable file
·340 lines (274 loc) · 5.9 KB
/
dedup_sort_bitmap_tree.c
File metadata and controls
executable file
·340 lines (274 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* Problem Description:
* Sorting and deduplication are the basis in data compression and
* optimization. Now there are N big numbers between 0 and 9999999999 generated
* by computer randomly, where n <= 500000. Your task is to get rid of repeated
* numbers, and sort them. (The judge system is a 32-bit system with memory
* limit of <=512M.)
* (EMC Shanghai COE Coding Contest 2014, Round 1)
*
* Program Description:
* This Program is based on bitmap tree with algorithm optimization to
* achieve O(N) on both time and space complexity. Meanwhile, optimization and
* performance tuning were done on implementation to make it run faster, e.g.
* Cache optimization, I/O optimization.
* (This is the version submitted to online judge system of the contest)
*
* Author:
* Windyon(Wenyu) Zhou <windyon9@gmail.com>
*
* Copyright (C) 2014, All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef unsigned char u8;
typedef unsigned short u16;
typedef int s32;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef u32 BOOL;
typedef u32 E;
//#define ASM_OPT
#define TRUE 1
#define FALSE 0
#define TOTAL_BITS 34
#define BYTE_BITS 3
#define BYTE_MASK ((1 << BYTE_BITS) - 1)
#define BITS_PER_BYTE (1 << BYTE_BITS)
#define BTABLE_ORDERS (BITS_PER_BYTE << 1)
#define BTABLE_SIZE (1 << BTABLE_ORDERS)
#define BTREE_HEIGHT 4
#define BTREE_BYTE_DEGREE_BITS 3
#define BTREE_BYTE_DEGREE (sizeof(u64))
#define BTREE_BIT_DEGREE_BITS (BYTE_BITS + BTREE_BYTE_DEGREE_BITS)
#define BTREE_BIT_DEGREE (1 << BTREE_BIT_DEGREE_BITS)
#define U64_IN_LEN 16
typedef struct _EL {
E* base;
E* head;
E* tail;
u32 count;
} EL;
char* buf = NULL;
char* bufp = NULL;
u64* A = NULL;
E* B = NULL;
EL* elist = NULL;
u32 elist_length = 0;
u32 buf_size = 0;
u8 btable[BTABLE_SIZE] = {0};
u8* btree[BTREE_HEIGHT] = {0};
u32 btree_width[BTREE_HEIGHT] = {0};
u32 btree_path[BTREE_HEIGHT] = {0};
u32 blist_shift = 0;
u32 blist_mask = 0;
inline u8 order32(u32 n)
{
return (n >> 16) ? 16 + btable[n >> 16] : btable[n];
}
#ifdef ASM_OPT
inline u64 u32_suffix_0(u64 n)
{
u64 ret;
__asm__ __volatile__ (
"tzcnt %1, %0"
: "=b"(ret)
: "a"(n), "b"(ret)
: "1"
);
return ret;
}
inline u32 suffix_0(u64 n)
{
return ((u32)n) ? u32_suffix_0((u32)n) : 32 + u32_suffix_0(n >> 32);
}
#endif
inline u8 get_min_bit_and_clear(u64 * d)
{
u64 m = (*d) - ((*d) & (*d - 1));
(*d) -= m;
#ifdef ASM_OPT
return suffix_0(m);
#else
return (m >> 32) ? 32 + order32(m >> 32) : order32(m);
#endif
}
inline BOOL get_bit(u8* blist, u32 n)
{
return blist[n >> BYTE_BITS] & ( 1 << (n & BYTE_MASK));
}
inline void set_bit(u8* blist, u32 n)
{
blist[n >> BYTE_BITS] |= ( 1 << (n & BYTE_MASK));
}
inline BOOL get_bit_and_set(u8* blist, u32 n)
{
BOOL b = get_bit(blist, n);
if (!b) {
set_bit(blist, n);
}
return b;
}
inline void pute(EL* list, E e)
{
*list->tail ++ = e;
}
inline E gete(EL* list)
{
return *list->head ++;
}
inline E gete_rev(EL* list)
{
return * -- list->tail;
}
inline void list_rewind(EL* list)
{
list->tail = list->head = list->base;
}
u32 bsort(EL* elist)
{
register s32 i = 0, j = 0;
register u32 n = 0;
u64* bm = NULL;
u32 count = 0;
while (elist->tail > elist->head) {
n = gete(elist);
for (i = BTREE_HEIGHT - 1; i >= 0; i --) {
if (get_bit_and_set(btree[i], n)) {
break;
}
n >>= BTREE_BIT_DEGREE_BITS;
}
}
i = j = 0;
list_rewind(elist);
bm = (u64 *)&btree[0][0];
while (i >= 0) {
if (*bm) {
n = (j << BYTE_BITS) + get_min_bit_and_clear(bm);
if (i == BTREE_HEIGHT - 1) {
pute(elist, n);
count ++;
} else {
btree_path[i] = j;
i ++;
j = n << BTREE_BYTE_DEGREE_BITS;
bm = (u64 *)&btree[i][j];
}
} else {
i --;
j = btree_path[i];
bm = (u64 *)&btree[i][j];
}
}
return count;
}
void init(u32 N)
{
u32 i = 0, n = 0;
buf_size = N * U64_IN_LEN;
buf = (char *)malloc(sizeof(char) * buf_size);
bufp = buf;
A = (u64 *)malloc(sizeof(u64) * N);
B = (E *)malloc(sizeof(E) * N);
for (i = 0; i < BTABLE_ORDERS; i ++) {
btable[1 << i] = i;
}
n = 0;
for (i = 0; i < BTREE_HEIGHT; i ++) {
n += BTREE_BIT_DEGREE_BITS;
btree_width[i] = (1 << n) >> BYTE_BITS;
btree[i] = (u8 *)malloc(sizeof(u8) * btree_width[i]);
}
blist_shift = n;
blist_mask = (1 << blist_shift) - 1;
elist_length = 1 << (TOTAL_BITS - n);
elist = (EL *)malloc(sizeof(EL) * elist_length);
memset(elist, 0, sizeof(EL) * elist_length);
}
void fini()
{
u32 i = 0;
free(buf);
free(A);
free(B);
free(elist);
for (; i < BTREE_HEIGHT; i ++) {
free(btree[i]);
}
}
inline u64 getu64()
{
u64 n = 0;
int c = 0;
while (!isdigit(c = *bufp ++))
;
n = c - '0';
while (isdigit(c = *bufp ++)) {
n = n * 10 + c - '0';
}
return n;
}
inline void putu64(u64 n)
{
do {
* -- bufp = n % 10 + '0';
n /= 10;
} while (n);
* -- bufp = ' ';
}
u32 input()
{
register u32 i = 0;
u32 N = 0;
scanf("%d\n", &N);
init(N);
fread(buf, buf_size, sizeof(char), stdin);
for (; i < N; i ++) {
//scanf("%llu", &A[i]);
A[i] = getu64();
elist[A[i] >> blist_shift].count ++;
}
elist[0].base = B;
list_rewind(&elist[0]);
for (i = 1; i < elist_length; i ++) {
elist[i].base = elist[i - 1].base + elist[i - 1].count;
list_rewind(&elist[i]);
}
for (i = 0; i < N; i ++) {
pute(&elist[A[i] >> blist_shift], A[i] & blist_mask);
}
return N;
}
void output(u32 N)
{
register u64 d = 0;
register s32 i = 0;
bufp = buf + buf_size;
* -- bufp = '\n';
for (i = elist_length - 1; i >= 0; i --) {
while (elist[i].tail > elist[i].head) {
d = (u64)i << blist_shift | gete_rev(&elist[i]);
putu64(d);
}
}
*bufp = '\n';
putu64(N);
fwrite(bufp + 1, buf + buf_size - 1 - bufp, sizeof(char), stdout);
fini();
}
int main(int argc, char **argv)
{
u32 i = 0;
u32 N = 0;
input();
for (i = 0; i < elist_length; i ++) {
if (elist[i].tail > elist[i].head) {
N += bsort(&elist[i]);
}
}
output(N);
return 0;
}