日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ_1151 Atlantis(线段树)

發(fā)布時間:2024/1/17 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ_1151 Atlantis(线段树) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
/*線段樹掃描線問題,感覺可以作為這類問題的模板。問過師兄以后才過的。。。

按y軸建線段樹。
*/

struct node{
int l, r, c; //c表示被覆蓋的情況,不為0表示被覆蓋
double sum, len; //len表示當(dāng)前段的長度,sum表示有效長度。就是可以計入求面積的長度
}node[N<<2];

struct line{
double x;  //記錄x坐標(biāo)
double y1;
double y2; //y1,y2用來記錄平行y軸的線段的兩個端點(diǎn),
int flag; //標(biāo)記是左邊的邊還是右邊的邊
}l[N];

//My Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define L(t) t << 1
#define R(t) t << 1 | 1

using namespace std;

const int N = 210;

struct node{
int l, r, c;
double sum, len;
}node[N<<2];

struct line{
double x;
double y1;
double y2;
int flag;
}l[N];

double y[N];

bool cmp(line a, line b){
return a.x < b.x;
}

void creat(int t, int l, int r){
node[t].l = l;
node[t].r = r;
node[t].c = 0;
node[t].sum = 0;
node[t].len = y[r] - y[l];
if(l + 1 == r) return ;
int mid = (l + r) >> 1;
creat(L(t), l, mid);
creat(R(t), mid, r);
}

void updata_sum(int t){
if(node[t].c > 0) node[t].sum = node[t].len;
else if(node[t].r != node[t].l + 1) node[t].sum = node[L(t)].sum + node[R(t)].sum;
else node[t].sum = 0;
}

void updata(int t, int l, int r, int c){
if(node[t].l == l && node[t].r == r){
if(c) node[t].c ++;
else node[t].c --;
updata_sum(t);
return ;
}
int mid = (node[t].l + node[t].r) >> 1;
if(l >= mid) updata(R(t), l, r, c);
else if(r <= mid) updata(L(t), l, r, c);
else{
updata(L(t), l, mid, c);
updata(R(t), mid, r, c);
}
updata_sum(t);
}

int find(double key, int n){
int l = 1, r = n, mid;
while(l <= r){
mid = (l + r) >> 1;
if(y[mid] == key) return mid;
else if(y[mid] > key) r = mid-1;
else l = mid + 1;
}
return 0;
}

int main(){
//freopen("data.in", "r", stdin);

int t, n, m, i, cas = 0;
double x1, x2, y1, y2, ans;
while(scanf("%d", &t), t){
m = 1;
while(t--){
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
y[m] = y1; l[m].x = x1; l[m].y1 = y1; l[m].y2 = y2; l[m].flag = 1; m++;
y[m] = y2; l[m].x = x2; l[m].y1 = y1; l[m].y2 = y2; l[m].flag = 0; m++;
}
sort(y+1, y+m);
sort(l+1, l+m, cmp);
m--; n = 2;
for(i = 1; i < m; i++)
if(y[i] != y[i+1]) y[n++] = y[i+1];
n--; creat(1, 1, n);
for(ans = 0, i = 1; i < m; i++){
if(l[i].flag) updata(1, find(l[i].y1, n), find(l[i].y2, n), 1);
else updata(1, find(l[i].y1, n), find(l[i].y2, n), 0);
ans += node[1].sum * (l[i+1].x - l[i].x);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n", ++cas, ans);
}
return 0;
}

轉(zhuǎn)載于:https://www.cnblogs.com/vongang/archive/2011/10/26/2225239.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的POJ_1151 Atlantis(线段树)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。