void settree(int p,int l,int r) { dmin[p]=prev[p]=0; if(l==r) len[p]=hash[l]-hash[l-1]; else { int mid=(l+r)>>1; settree(ls(p),l,mid); settree(rs(p),mid+1,r); len[p]=len[ls(p)]+len[rs(p)]; } }
int main() { int n; int T=0; while(~scanf("%d",&n) && n!=0) { ++T; hcnt=segcnt=0; double x1,y1,x2,y2; for(int i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); hash[++hcnt]=x1;hash[++hcnt]=x2; seg[++segcnt]=(Segment){x1,x2,y1,1}; seg[++segcnt]=(Segment){x1,x2,y2,-1}; } std::sort(seg+1,seg+segcnt+1); std::sort(hash+1,hash+hcnt+1); settree(1,1,hcnt); double covered=0,ans=0; for(int i=1;i<=segcnt-1;i++) { covered+=add(1,1,hcnt,gethash(seg[i].l)+1,gethash(seg[i].r),seg[i].tp); ans+=covered*(seg[i+1].h-seg[i].h); } printf("Test case #%d\nTotal explored area: %.2f\n\n",T,ans); } }
int main() { int n; int T; scanf("%d",&T); while(T--) { scanf("%d",&n); hcnt=segcnt=0; double x1,y1,x2,y2; for(int i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); hash[++hcnt]=x1;hash[++hcnt]=x2; seg[++segcnt]=(Segment){x1,x2,y1,1}; seg[++segcnt]=(Segment){x1,x2,y2,-1}; } std::sort(seg+1,seg+segcnt+1); std::sort(hash+1,hash+hcnt+1); settree(1,1,hcnt); double covered=0,ans=0; for(int i=1;i<=segcnt-1;i++) { covered+=add(1,1,hcnt,gethash(seg[i].l)+1,gethash(seg[i].r),seg[i].tp); ans+=covered*(seg[i+1].h-seg[i].h); } printf("%.2f\n",ans); } }
int main() { gen(M1); gen(M2); POW1[0] = POW2[0] = 1; OPEN[0] = {0, 0}; CLOSE[0] = {0, 0}; for (int i = 1; i < N; i++) { POW1[i] = POW1[i - 1] * X % M1; POW2[i] = POW2[i - 1] * X % M2; if (i == 1) { OPEN[i] = {(long long) '(', 1}; CLOSE[i] = {(long long) ')', 1}; } else { OPEN[i] = OPEN[i - 1] + OPEN[1]; CLOSE[i] = CLOSE[i - 1] + CLOSE[1]; } } int c = getchar(); while (c <= 32) c = getchar(); n = 1; while (c > 32) { s[n++] = c; c = getchar(); } --n; for (int i = 1; i <= n; i++) s[i + n] = s[i]; bal[0] = 0; for (int i = 1; i <= 2 * n; i++) { bal[i] = bal[i - 1] + (s[i] == '(' ? 1 : -1); settree(i, bal[i]); } hs[0] = {0, 0}; for (int i = 1; i <= 2 * n; i++) { hash cur = {(long long) s[i], 1}; hs[i] = hs[i - 1] + cur; } answer ans = {INF / 2, -1, INF / 2}; for (int start = 1; start <= n; start++) { int minB = getmin(start - 1, start + n); int addOpen = bal[start - 1] - minB; int addClose = bal[start + n - 1] - minB; answer cur = {addOpen, start, addClose}; if (cur < ans) ans = cur; } for (int i = 0; i < ans.addOpen; i++) putchar('('); for (int i = 0; i < n; i++) putchar(s[ans.pos + i]); for (int i = 0; i < ans.addClose; i++) putchar(')'); }
double shake(int nb, char** seq, char** seqname, char* ctree, options opt, char** eval_input, int nb_eval_input){ int nb2, i, j, ii, jj, k, l, *ttree[MAXNSP], **curtree, **newtree, **evaltree, nbbi, nbdclade, nbgclade, print1, print2; int nochange, pres_grossiere=-1, l1, l2, restart_d, restart_g, oldmovedist; int **dclade, **gclade, **newdclade, **newgclade, **ddist, **gdist, movedist, maxmovedist; int **list_tree, lliste, *solid; long nblist, nblistmax; double *lgbp, *sortedlgbp, *lgbi, *bootvals, fracroot1, lkh, maxlkh, maxlcrossedbranch; char *nom[MAXNSP], *nom2[MAXNSP], **dcladename, **gcladename, **list1, **list2, racine, *ctreenew, *ctreenew_nobl, *treedeb, *ctree1, *ctree2; FILE* outfile1, *outfile2; print_option trueprint, noprint; print1=opt->print->PRINT1; print2=opt->print->PRINT2; noprint=check_alloc(1, sizeof(struct print_option)); noprint->PRINT3=0; if(opt->print->PRINT2) noprint->PRINT1=noprint->PRINT2=1; else noprint->PRINT1=noprint->PRINT2=0; if(print1) noprint->PRINT0=1; else noprint->PRINT0=0; trueprint=opt->print; opt->print=noprint; maxlcrossedbranch=opt->SH_MAXLCROSSED; /* READ TREE STRING */ lgbp=(double*)check_alloc(nb+1, sizeof(double)); sortedlgbp=(double*)check_alloc(nb+1, sizeof(double)); lgbi=(double*)check_alloc(nb+1, sizeof(double)); for(i=0;i<nb+1;i++) lgbp[i]=lgbi[i]=-1.; solid=(int*)check_alloc(nb+1, sizeof(int)); bootvals=(double*)check_alloc(nb+1, sizeof(double)); if(nb>=MAXNSP) {printf("Too many sequences\n"); exit(EXIT_FAILURE);} for(i=0;i<=nb;i++){ nom[i]=(char*)check_alloc(MAXLNAME+1, sizeof(char)); nom2[i]=(char*)check_alloc(MAXLNAME+1, sizeof(char)); ttree[i]=(int*)check_alloc(nb, sizeof(int)); } list1=check_alloc(nb, sizeof(char*)); list2=check_alloc(nb, sizeof(char*)); curtree=(int**)check_alloc(nb+1, sizeof(int*)); newtree=(int**)check_alloc(nb+1, sizeof(int*)); evaltree=(int**)check_alloc(nb+1, sizeof(int*)); for(i=0;i<nb+1;i++) newtree[i]=(int*)check_alloc(nb-2, sizeof(int)); ctreenew=(char*)check_alloc(2*MAXLNAME*nb, sizeof(char)); ctree1=(char*)check_alloc(2*MAXLNAME*nb, sizeof(char)); ctree2=(char*)check_alloc(2*MAXLNAME*nb, sizeof(char)); ctreenew_nobl=(char*)check_alloc(2*MAXLNAME*nb, sizeof(char)); nb2=ctot(ctree, ttree, lgbi, lgbp, bootvals, nom, &racine, &nbbi); if(nb2<nb){ printf("More species in sequence file than in tree file\n"); exit(EXIT_FAILURE); } if(nb2>nb){ printf("More species in tree file than in sequence file\n"); exit(EXIT_FAILURE); } /* PREPARE TREE : UNROOT, SET LEFT and RIGHT LISTS, SORT TAXA */ if(racine=='r'){ unroot(ttree, nb, lgbi, lgbp, bootvals, nom, list1, list2, &l1, &l2, &fracroot1); } else{ printf("Tree must be rooted\n"); exit(EXIT_FAILURE); } nbbi--; if(nbbi!=nb-3){ printf("Tree must be bifurcating\n"); exit(EXIT_FAILURE); } for(i=0;i<nb;i++){ for(j=0;j<nb;j++){ if(samename(seqname[i], nom[j])){ curtree[i]=ttree[j]; sortedlgbp[i]=lgbp[j]; break; } } } for(i=0;i<nb-3;i++) if(bootvals[i]>opt->SH_MAXBOOTCROSSED) solid[i]=1; ctree_noblbs(ctree, ctreenew_nobl, strlen(ctree)); /* EVALUATE INITIAL TREE */ if(print1) printf("\nEvaluating initial tree : \n%s\n", ctreenew_nobl); if(print2) printf("\n"); maxlkh=maxlike(nb, seq, seqname, curtree, lgbi, sortedlgbp, nbbi, l1, list1, l2, list2, opt, NULL, NULL, NULL); if(opt->SH_RESTART) save_current_best("current_best_tree", ctreenew_nobl, maxlkh, 1); if(opt->SH_RESTART) save_evaluated("evaluated_trees", ctreenew_nobl, maxlkh, 1); /* ALLOCATE SHAKE VARIABLES */ nbdclade=2*l1-1; nbgclade=2*l2-1; dclade=(int**)check_alloc(nbdclade, sizeof(int*)); for(i=0;i<nbdclade;i++) dclade[i]=(int*)check_alloc(nb, sizeof(int)); gclade=(int**)check_alloc(2*nb, sizeof(int*)); for(i=0;i<nbgclade;i++) gclade[i]=(int*)check_alloc(nb, sizeof(int)); newdclade=(int**)check_alloc(nbdclade, sizeof(int*)); for(i=0;i<nbdclade;i++) newdclade[i]=(int*)check_alloc(nb, sizeof(int)); newgclade=(int**)check_alloc(nbgclade, sizeof(int*)); for(i=0;i<nbgclade;i++) newgclade[i]=(int*)check_alloc(nb, sizeof(int)); ddist=(int**)check_alloc(nbdclade, sizeof(int*)); gdist=(int**)check_alloc(nbgclade, sizeof(int*)); for(i=0;i<nbdclade;i++) ddist[i]=(int*)check_alloc(nbdclade, sizeof(int)); for(i=0;i<nbgclade;i++) gdist[i]=(int*)check_alloc(nbgclade, sizeof(int)); dcladename=(char**)check_alloc(nbdclade, sizeof(char*)); gcladename=(char**)check_alloc(nbgclade, sizeof(char*)); for(i=0;i<nbdclade;i++) dcladename[i]=(char*)check_alloc(nb*(MAXLNAME+3)+1, sizeof(char)); for(i=0;i<nbgclade;i++) gcladename[i]=(char*)check_alloc(nb*(MAXLNAME+3)+1, sizeof(char)); nblistmax=nb*nb; if(nblistmax<MIN_NBLISTMAX) nblistmax=MIN_NBLISTMAX; if(nblistmax<nb_eval_input) nblistmax=nb_eval_input; if(nblistmax>MAX_NBLISTMAX) nblistmax=MAX_NBLISTMAX; while(1){ lliste=(nblistmax*(nb-3)+lmot-1)/lmot; list_tree=(int**)check_alloc(nb, sizeof(int*)); for(i=0;i<nb;i++) list_tree[i]=(int*)calloc(lliste, sizeof(int)); if(list_tree[nb-1]) break; nblistmax/=2; if(nblistmax==0){ printf("Not enough memory\n"); exit(EXIT_FAILURE); } } /* SET LIST OF EVALUATED TREES */ if(eval_input){ nblist=0; for(k=0;k<nb_eval_input;k++){ for(i=0;i<=nb;i++) ttree[i]=check_alloc(nb, sizeof(int)); nb2=ctot(eval_input[k], ttree, NULL, NULL, NULL, nom2, &racine, NULL); if(racine=='r') unroot(ttree, nb, NULL, NULL, NULL, nom2, NULL, NULL, NULL, NULL, NULL); else{ printf("Evaluated trees must be rooted\n"); exit(EXIT_FAILURE);} for(i=0;i<nb;i++){ for(j=0;j<nb;j++){ if(samename(seqname[i], nom2[j])){ evaltree[i]=ttree[j]; break; } } } if(deja_evalue(evaltree, nb, list_tree, nblist, nblistmax)) continue; addtolist(evaltree, nb, list_tree, nblist, nblistmax); nblist++; } printf("%d already evaluated topologies loaded\n", nblist); } else{ addtolist(curtree, nb, list_tree, 0, nblistmax); nblist=1; } /* SHAKE */ treedeb=ctreenew; nochange=1; movedist=0; if(opt->SH_G>0 && opt->SH_G<nb-3) maxmovedist=opt->SH_G; else maxmovedist=nb-3; if(print1) printf("\nStarting rearrangements\n"); do{ oldmovedist=movedist; if(nochange==1) movedist++; else movedist=1; if(print1 && !(nochange==0 && oldmovedist==1)){ printf("\nCrossing %d internal branch", movedist); if(movedist>1) printf("es"); printf("\n"); } nochange=1; while(1){ setclades(curtree, nb, seqname, l1, list1, l2, list2, dclade, gclade, ddist, gdist, nbdclade, nbgclade, dcladename, gcladename); restart_d=0; for(i=0;i<nbdclade;i++){ for(j=0;j<nbdclade;j++){ if(abs(ddist[i][j])!=movedist) continue; if(ddist[i][j]<0) continue; moveclade(dclade, newdclade, nbdclade, nb, ddist, i, j); settree(newdclade, nbdclade, gclade, nbgclade, nb, newtree); if(deja_evalue(newtree, nb, list_tree, nblist, nblistmax)) continue; if(solid_branch_crossed(newtree, curtree, nb, solid)) continue; addtolist(newtree, nb, list_tree, nblist, nblistmax); nblist++; if(print1){ printf("\nMoving %s toward %s\n", dcladename[i], dcladename[j]); } if(print2) printf("\n"); lkh=maxlike(nb, seq, seqname, newtree, NULL, NULL, nbbi, l1, list1, l2, list2, opt, ctreenew, NULL, NULL); if(lkh>maxlkh){ ctree_noblbs(ctreenew, ctreenew_nobl, strlen(ctreenew)); if(print1) printf("New tree is optimal : \n%s\n\nRestarting rearrangements\n", ctreenew_nobl); maxlkh=lkh; copytree(newtree, curtree, nb); if(opt->SH_RESTART) save_current_best("current_best_tree", ctreenew_nobl, lkh, 0); if(opt->SH_RESTART) save_evaluated("evaluated_trees", ctreenew_nobl, lkh, 0); restart_d=1; nochange=0; ctreenew=treedeb; while(*ctreenew) {*ctreenew=0; ctreenew++; } ctreenew=treedeb; break; } else{ if(print1) printf("No improvement:\n"); ctree_noblbs(ctreenew, ctreenew_nobl, strlen(ctreenew)); if(print1) printf("%s\n", ctreenew_nobl); if(opt->SH_RESTART) save_evaluated("evaluated_trees", ctreenew_nobl, lkh, 0); } ctreenew=treedeb; while(*ctreenew) {*ctreenew=0; ctreenew++; } ctreenew=treedeb; } if(restart_d) break; } if(!restart_d || movedist>1) break; } if(restart_d && movedist>1) continue; while(1){ setclades(curtree, nb, seqname, l1, list1, l2, list2, dclade, gclade, ddist, gdist, nbdclade, nbgclade, dcladename, gcladename); restart_g=0; for(i=0;i<nbgclade;i++){ for(j=0;j<nbgclade;j++){ if(abs(gdist[i][j])!=movedist) continue; if(gdist[i][j]<0) continue; moveclade(gclade, newgclade, nbgclade, nb, gdist, i, j); settree(dclade, nbdclade, newgclade, nbgclade, nb, newtree); if(deja_evalue(newtree, nb, list_tree, nblist, nblistmax)) continue; if(solid_branch_crossed(newtree, curtree, nb, solid)) continue; addtolist(newtree, nb, list_tree, nblist, nblistmax); nblist++; if(print1){ printf("\nMoving %s toward %s\n", gcladename[i], gcladename[j]); } if(print2){ printf("\n"); } lkh=maxlike(nb, seq, seqname, newtree, NULL, NULL, nbbi, l1, list1, l2, list2, opt, ctreenew, NULL, NULL); if(lkh>maxlkh){ ctree_noblbs(ctreenew, ctreenew_nobl, strlen(ctreenew)); if(print1) printf("New tree is optimal : \n%s\n\nRestarting rearrangements\n", ctreenew_nobl); maxlkh=lkh; copytree(newtree, curtree, nb); if(opt->SH_RESTART) save_current_best("current_best_tree", ctreenew_nobl, lkh, 0); if(opt->SH_RESTART) save_evaluated("evaluated_trees", ctreenew_nobl, lkh, 0); restart_g=1; nochange=0; ctreenew=treedeb; while(*ctreenew) {*ctreenew=0; ctreenew++; } ctreenew=treedeb; break; } else{ if(print1) printf("No improvement:\n"); ctree_noblbs(ctreenew, ctreenew_nobl, strlen(ctreenew)); if(print1) printf("%s\n", ctreenew_nobl); if(opt->SH_RESTART) save_evaluated("evaluated_trees", ctreenew_nobl, lkh, 0); } ctreenew=treedeb; while(*ctreenew) {*ctreenew=0; ctreenew++; } ctreenew=treedeb; } if(restart_g) break; } if(!restart_g || movedist>1) break; } } while(nochange==0 || movedist!=maxmovedist); /* FINAL EVALUATION */ if(print1) printf("\nFinal evaluation\n"); if(print2) printf("\n"); opt->print=trueprint; maxlkh=maxlike(nb, seq, seqname, curtree, NULL, NULL, nbbi, l1, list1, l2, list2, opt, NULL, ctree1, ctree2); if(ctree1){ outfile1=fopen("treefile.eqgc", "w"); outfile2=fopen("treefile.ndgc", "w"); if(outfile1==NULL || outfile2==NULL){ printf("Cannot write tree file\n"); exit(EXIT_FAILURE); } fprintf(outfile1, "%s\n", ctree1); fprintf(outfile2, "%s\n", ctree2); if(print1){ printf("Tree is written into files : treefile.eqgc (equilibrium G+C content)\n"); printf(" treefile.ndgc (G+C content at each node)\n\n"); } } free(lgbp); free(sortedlgbp); free(lgbi); free(bootvals); for(i=0;i<nb;i++){ free(nom[i]); free(ttree[i]); } free(list1); free(list2); free(curtree); return maxlkh; }