hdu 3265--线段树扫描线
http://acm.hdu.edu.cn/showproblem.php?pid=3265
题意:矩形面积并
思路:把切割完的面积分成四份,然后用扫描线扫描。注意要用__int64存。
代码:

1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 #define maxn 55555 8 #define lson l,m,rt<<1 9 #define rson m+1,r,rt<<1|1 10 struct node { 11 int l,r,h,s; 12 node(){} 13 node(int a,int b,int c,int d):l(a),r(b),h(c),s(d){} 14 bool operator < (const node &cmp) const{ 15 return h<cmp.h; 16 } 17 }ss[maxn<<3]; 18 int max(int a,int b) 19 { 20 return a>b?a:b; 21 } 22 int min(int a,int b) 23 { 24 return a<b?a:b; 25 } 26 __int64 sum[maxn<<2]; 27 int flag[maxn<<2]; 28 void Pushup(int l,int r,int rt) 29 { 30 if(flag[rt]) 31 sum[rt]=r-l+1; 32 else if(r==l) 33 sum[rt]=0; 34 else 35 sum[rt]=sum[rt<<1]+sum[rt<<1|1]; 36 } 37 void update(int L,int R,int w,int l,int r,int rt) 38 { 39 if(L<=l&&r<=R) 40 { 41 flag[rt]+=w; 42 Pushup(l,r,rt); 43 return ; 44 } 45 int m=(l+r)>>1; 46 if(L<=m) update(L,R,w,lson); 47 if(R>m) update(L,R,w,rson); 48 Pushup(l,r,rt); 49 } 50 int main() 51 { 52 int t,x1,x2,x3,x4,y1,y2,y3,y4; 53 while(~scanf("%d",&t),t) 54 { 55 int m=0,lrd=50000,rrd=-50000; 56 while(t--) 57 { 58 scanf("%d %d %d %d",&x1,&y1,&x2,&y2); 59 scanf("%d %d %d %d",&x3,&y3,&x4,&y4); 60 lrd=min(lrd,x1); 61 rrd=max(rrd,x2); 62 ss[m++]=node(x1,x3,y1,1); 63 ss[m++]=node(x1,x3,y2,-1); 64 ss[m++]=node(x3,x4,y1,1); 65 ss[m++]=node(x3,x4,y3,-1); 66 ss[m++]=node(x3,x4,y4,1); 67 ss[m++]=node(x3,x4,y2,-1); 68 ss[m++]=node(x4,x2,y1,1); 69 ss[m++]=node(x4,x2,y2,-1); 70 } 71 sort(ss,ss+m); 72 memset(sum,0,sizeof(sum)); 73 memset(flag,0,sizeof(flag)); 74 __int64 ret=0; 75 for(int i=0;i<m-1;i++) 76 { 77 if(ss[i].l<ss[i].r) update(ss[i].l,ss[i].r-1,ss[i].s,lrd,rrd-1,1); 78 ret+=sum[1]*(ss[i+1].h-ss[i].h); 79 } 80 printf("%I64d\n",ret); 81 } 82 return 0; 83 }
posted on 2013-02-19 13:56 acoderworld 阅读(91) 评论(0) 收藏 举报
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步