level 2
第一个文件:
#include
"main.h"
void add_seg(struct line_t *l, struct rect_t *r){
update_l(l, 1, 0, l->n/2 - 1,
r->ybottom - l->ymin, r->ytop - l->ymin - 1, l->l[0], 1);
/*
int i;
printf("add_segment\n");
for (i=0; i< l->n; i++){
printf("%li
",l->l[i]);
}
printf("\n\n");
*/
}
void remove_seg(struct line_t *l, struct rect_t *r){
update_l(l, 1, 0, l->n/2 - 1,
r->ybottom - l->ymin, r->ytop - l->ymin - 1, l->l[0], -1);
/*
int i;
printf("remove_segment\n");
for (i=0; i< l->n; i++){
printf("%li
",l->l[i]);
}
printf("\n\n");
*/
}
void update_l(struct line_t *l, unsigned long int pos, unsigned long int lmin,
unsigned long int lmax, uint32_t b, uint32_t t, unsigned long int ov, int
op){
// //printf(" update_l pos: %i, lmin:
%i; lmax: %i, b: %i, t:%i, op: %i\n", pos, lmin, lmax, b, t, op);
/* is the segment included in our
segment */
if ((b <= lmin) && (t >=
lmax)){
switch (op){
case 1: /* adding element */
(l->l[pos-1])++;
//printf("setting bit pos:
%i\n", pos);
if (!ov){
if (l->l[pos-1] == 1){
l->cur += (lmax - lmin + 1);
//printf("lcur increase adding %i, pos: %i\n", lmax - lmin +1,
pos);
if ((lmax - lmin +1)/2 > 0 ){
update_l(l, pos*2, lmin , (lmin+lmax)/2 ,
b, t, ov, 2);
update_l(l, pos*2 +1 , (lmin+lmax)/2 + 1
, lmax , b, t, ov, 2);
}
}
}
break;
case 2: /* inspecting subtree after
addition */
if (!ov){ /* if there the interval
is not overflowed */
if (l->l[pos-1] > 0){ /* and the interval is marked */
l->cur -= (lmax - lmin + 1);
//printf("lcur inspection decrease removing %i, pos: %i\n",
lmax - lmin +1, pos);
}
else{
if ((lmax - lmin +1)/2 > 0 ){
update_l(l, pos*2, lmin , (lmin+lmax)/2 ,
b, t, ov, op);
update_l(l, pos*2 +1 , (lmin+lmax)/2 + 1
, lmax , b, t, ov, op);
}
}
}
break;
case -1: /* removing segment
*/
(l->l[pos-1])--;
//printf("removing bit pos: %i\n", pos);
if (!ov){
if ( l->l[pos-1] == 0){
l->cur -= (lmax - lmin + 1);
//printf("lcur decrease remove %i, pos: %i\n", lmax - lmin +1,
pos);
if ((lmax - lmin +1)/2 > 0 ){
update_l(l, pos*2, lmin , (lmin+lmax)/2 ,
b, t, ov, 0);
update_l(l, pos*2 +1 , (lmin+lmax)/2 + 1
, lmax , b, t, ov, 0);
}
}
}
break;
case 0: /* inspecting subtree after
deletion */
if (!ov){ /* if there the interval
is not overflowed */
if (l->l[pos-1] > 0){ /* and the interval is marked */
l->cur += (lmax - lmin + 1);
//printf("lcur inspection
increase add %i, pos: %i\n", lmax - lmin +1, pos);
}
else{
if ((lmax - lmin +1)/2 > 0 ){
update_l(l, pos*2, lmin , (lmin+lmax)/2 ,
b, t, ov, op);
update_l(l, pos*2 +1 , (lmin+lmax)/2 + 1
, lmax , b, t, ov, op);
}
}
}
break;
}
}
else
{
if ((l->l[pos-1] > 0) ||
ov){
//printf("overflow at position
pos: %i\n", pos);
ov = 1;
}
if (b <= (lmin+lmax)/2){
update_l(l, pos*2, lmin ,
(lmin+lmax)/2 , b, t, ov, op);
}
if (t > (lmin+lmax)/2){
update_l(l, pos*2 +1, (lmin+lmax)/2
+ 1 , lmax , b, t, ov, op);
}
}
}
2012年12月14日 19点12分
