-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.c
412 lines (412 loc) · 7.9 KB
/
state.c
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include <stdlib.h>
#include <stdio.h>
#include <bsd/stdlib.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>
#include "state.h"
#include "graph.h"
#include "splayTree.h"
#include "sigterm.h"
extern uint n0;
extern uint n1;
typedef struct crossings crossings;
struct crossings{
uint v;
};
static crossings *B;
static uint *BI;
static uint *S;
static uint *SI;
static uint R = 32;
static uint M = 0xFFFFFFFF;
static uint *Bscore;
static uint *Sscore;
static uint *ops;
static uint *BestScore;
void
gdbBreak(void)
{}
static void
setupTree(void)
{
R = 32 - __builtin_clz (n1-1);
M = (1<<R)-1;
}
static uint
lca(uint a, uint b)
{
int s = 32 - __builtin_clz (a ^ b);
return (a|(1<<R)) >> s;
}
static uint
itop(uint i)
{
uint s = i << (R + __builtin_clz (i) - 31);
return s & M;
}
static int
intcmp(const void *p1, const void *p2)
{
int A = *(const uint *) p1;
int B = *(const uint *) p2;
return A - B;
}
static uint
levelSize(uint l)
{
uint m = itop(l);
uint n = itop(l+1);
if((0 == n) || (n > n1)) n = n1;
return n - m;
}
static void
setBest(uint v)
{
BestScore[v] = -1;
if(2*v < (uint)(1<<R))
BestScore[v] = BestScore[2*v] + BestScore[2*v+1] + Sscore[v] - Sscore[2*v] - Sscore[2*v+1];
if(BestScore[v] > Bscore[v])
BestScore[v] = Bscore[v];
}
void
assertConsistency(void)
#ifdef NDEBUG
{}
#else
{
for(uint i = 0; i < n1; i++){
assert(SI[S[i]] == i && "Failed saved consistency");
}
for(uint i = 0; i < n1; i++){
assert(BI[B[i].v] == i && "Failed current consistency");
}
uint lB[n1];
for(uint l = 1; l < (uint)(1<<R); l++){
if(itop(l) < n1){
for(uint i = 0; i < levelSize(l); i++)
lB[i] = BI[S[itop(l)+i]];
qsort(lB, levelSize(l), sizeof(uint), intcmp);
for(uint i = 0; i < levelSize(l); i++)
lB[i] = B[lB[i]].v;
assert(Bscore[l] == totalCross(levelSize(l), lB) && "Score failure");
assert(Sscore[l] == totalCross(levelSize(l), &S[itop(l)]) && "Score failure");
}
}
}
#endif
static void
storeInterval(uint v)
{
uint l = levelSize(v);
uint t = itop(v);
for(uint i = 0; i < l; i++)
S[t+i] = BI[S[t+i]];
qsort(&S[t], l, sizeof(uint), intcmp);
for(uint i = 0; i < l; i++){
S[t+i] = B[S[t+i]].v;
SI[S[t+i]] = t+i;
}
uint d = Sscore[v];
uint j = 1;
while(lca(t,t+j) >= v){
for(uint i = 0; i < l; i+= 2*j){
uint len = 2*j;
if(t+i+len > n1) len = n1 - i - t;
BestScore[lca(t+i,t+i+j)] = \
Bscore[lca(t+i,t+i+j)] = \
Sscore[lca(t+i,t+i+j)] = \
totalCross(len, &S[t+i]);
ops[lca(t+i,t+i+j)] = 0;
}
j *= 2;
}
d -= Sscore[v];
v /= 2;
while(0 < v){
Sscore[v] -= d;
setBest(v);
v /= 2;
}
}
static void
recursiveStore(uint v)
{
if(v >= (uint)(1<<R))
return;
if(Sscore[v] <= BestScore[v]) return;
if(Bscore[v] <= BestScore[v]){
storeInterval(v);
return;
}
recursiveStore(2*v);
recursiveStore(2*v+1);
}
static uint *F = NULL;
static uint
queryF(uint i)
{
uint s = 0;
while(0 < i){
s += F[i];
i -= i&-i;
}
return s;
}
static void
updateF(uint i, uint v)
{
while(i <= n0){
F[i] += v;
i += i&-i;
}
}
static uint
totalCrossingsR(void)
{
uint res = 0;
F = (uint *)calloc(n0+2, sizeof(uint));
for(uint j = n1; j > 0;){
j--;
uint *A = Adj(B[j].v);
int d = (int)deg(B[j].v);
A += d-1;
for(int i = 0; i < d; i++){
res += queryF(A[-i]-1);
updateF(A[-i], 1);
}
}
free(F);
return res;
}
static uint
totalCrossingsL(void)
{
uint res = 0;
F = (uint *)calloc(n0+2, sizeof(uint));
for(uint j = 0; j < n1; j++){
uint *A = Adj(B[j].v);
uint d = deg(B[j].v);
for(uint i = 0; i < d; i++){
res += queryF(n0-A[i]);
updateF(1+n0-A[i], 1);
}
}
free(F);
return res;
}
#define miniM(M,V) if((V) < (M)) (M) = (V);
#define swapM(T,A,B) {T _mt = A; A = B; B = _mt;}
static void
vertexSwap(uint li)
{
assert("Out of Bounds Swap" && li < n1-1);
uint l = lca(SI[B[li].v], SI[B[li+1].v]);
if(0 < vertexEnergy(li) &&
BestScore[l] < Sscore[l] &&
ops[l] >= levelSize(l)
){
while(0 < l/2){
if(ops[l/2] < levelSize(l/2))
break;
if(BestScore[l/2] >= Sscore[l/2])
break;
if(BestScore[2*(l/2)] >= Sscore[2*(l/2)])
break;
if(BestScore[2*(l/2)+1] >= Sscore[2*(l/2)+1])
break;
l /= 2;
}
recursiveStore(l);
}
l = lca(SI[B[li].v], SI[B[li+1].v]);
while(0 < l){
ops[l]++;
Bscore[l] += vertexEnergy(li);
setBest(l);
l /= 2;
}
swapM(crossings, B[li], B[li+1]);
BI[B[li].v] = li;
BI[B[li+1].v] = li+1;
removeSp(li);
if(0 < li) removeSp(li-1);
if(li+1 < n1-1) removeSp(li+1);
insertSp(li);
if(0 < li) insertSp(li-1);
if(li+1 < n1-1) insertSp(li+1);
}
int
vertexEnergy(uint i)
{
return (int)cjj(B[i+1].v, B[i].v) - (int)cjj(B[i].v, B[i+1].v);
}
static int
cmpAvg(const void *p1, const void *p2)
{
uint j = *(const uint *) p1;
uint jj = *(const uint *) p2;
if(0 == deg(j)) return -1;
if(0 == deg(jj)) return 1;
double a1 = getAvg(*(const uint *) p1);
double a2 = getAvg(*(const uint *) p2);
int r = 0;
if(a1 != a2){
if(a1 > a2)
r = 1;
else
r = -1;
}
return r;
}
uint
setInitialState(void)
{
S = (uint *)malloc(n1*sizeof(uint));
for(uint i = 0; i < n1; i++) S[i] = n0+1+i;
qsort(S, n1, sizeof(uint), cmpAvg);
freeAvg();
SI = (uint *)malloc(n1*sizeof(uint));
SI = &SI[(int)-n0-1];
for(uint i = 0; i < n1; i++) SI[S[i]] = i;
B = (crossings *)calloc(n1, sizeof(struct crossings));
for(uint i = 0; i < n1; i++) B[i].v = S[i];
BI = (uint *)calloc(n1, sizeof(uint));
BI = &BI[(int)-n0-1];
for(uint i = 0; i < n1; i++) BI[B[i].v] = i;
allocSplay(n1);
uint max[n1];
uint min[n1];
if(Adj(S[0]+1) != Adj(S[0]))
max[0] = Adj(S[0]+1)[-1];
else
max[0] = 0;
for(uint i = 1; i < n1; i++){
max[i] = max[i-1];
if(Adj(S[i]+1) != Adj(S[i]) && Adj(S[i]+1)[-1] > max[i])
max[i] = Adj(S[i]+1)[-1];
}
if(Adj(S[n1-1]+1) != Adj(S[n1-1]))
min[n1-1] = Adj(S[n1-1])[0];
else
min[n1-1] = n0 + 1;
for(int i = n1-2; i >= 0; i--){
min[i] = min[i+1];
if(Adj(S[i]+1) != Adj(S[i]) && Adj(S[i])[0] < min[i])
min[i] = Adj(S[i])[0];
}
for(uint i = 0; i+1 < n1; i++){
if(max[i] <= min[i+1]){
} else
insertSp(i);
}
setupTree();
uint al = 1<<R;
Bscore = (uint *) malloc(al*sizeof(uint));
BestScore = (uint *) calloc(al, sizeof(uint));
Sscore = (uint *) calloc(al, sizeof(uint));
ops = (uint *) calloc(al, sizeof(uint));
storeInterval(1);
return Bscore[1];
}
void
freeState(void
)
{
freeSplay();
free(S);
free(&SI[n0+1]);
free(B);
free(&BI[n0+1]);
free(Bscore);
free(BestScore);
free(Sscore);
free(ops);
}
void
printState(void)
{
uint i = 0;
for(; i+1 < n1; i++){
printf("%d-(%d|%d\t%d-%d = %d) ",
B[i].v,
cjj(B[i+1].v, B[i].v),
cjj(B[i].v, B[i+1].v),
cjj(B[i+1].v, B[i].v),
cjj(B[i].v, B[i+1].v),
cjj(B[i+1].v, B[i].v) - cjj(B[i].v, B[i+1].v)
);
putchar('\n');
}
}
static void
saveState(uint j)
{
uint min;
uint max;
}
void
transitionBound(int ell)
{
uint li;
uint p;
{
uint pool = getSizeByD(ell);
if(0 == pool)
p = 0;
else
p = arc4random_uniform(pool);
li = getNodeOnSplay(p);
}
int d = vertexEnergy(li);
if(0 <= d){
vertexSwap(li);
} else {
int k = 0;
while(canContinue() &&
li + k < n1 - 1 &&
containsSp(li+k) &&
0 >= vertexEnergy(li+k)
){
vertexSwap(li+k);
k++;
}
k = 1;
while(canContinue() &&
k <= (int)li &&
containsSp(li-k) &&
0 >= vertexEnergy(li-k)
){
vertexSwap(li-k);
k++;
}
}
}
void
saveMin(void)
{
recursiveStore(1);
}
void
showMin(void)
{
for(uint i = 0; i < n1; i++)
printf("%d\n", S[i]);
}
void
resetState(void)
{
saveMin();
for(uint i = 0; i < n1; i++){
B[i].v = S[i];
BI[B[i].v] = i;
}
for(uint i = 0; i < (uint)(1<<R); i++)
BestScore[i] = Bscore[i] = Sscore[i];
}
uint
getScore(void)
{
return Sscore[1];
}