站长软件下载 东莞佳利热转印机 空白卫衣批发 个性服饰定制

POJ 1389 Area of Simple Polygons 线段树求矩形面积

题意:给一些矩形,让求这些矩形的面积。矩形是给出的左下角和右下角的坐标,都是正整数。

思路:简单的线段树题目,就是求矩形面积。因为都是整数,而且数据范围不大,因此不需要离散化。

代码:

 

  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <algorithm>  
  4. #include <string.h>  
  5. using namespace std;  
  6.   
  7. const int N = 50010,M = 1010;  
  8. struct tree{  
  9.     int lp,rp,len,num;  
  10.     int getmid(){  
  11.        return (lp + rp) / 2;  
  12.     }  
  13. }tt[N * 4];  
  14. struct line{  
  15.     int lp,rp,cnt,value;  
  16. }LL[M * 2];  
  17. void built_tree(int lpos,int rpos,int pos){  
  18.     tt[pos].lp = lpos;  
  19.     tt[pos].rp = rpos;  
  20.     tt[pos].len = tt[pos].num = 0;  
  21.     if(tt[pos].lp + 1 == tt[pos].rp)  
  22.         return;  
  23.     int mid = tt[pos].getmid();  
  24.     built_tree(lpos,mid,pos*2);  
  25.     built_tree(mid,rpos,pos*2+1);  
  26. }  
  27. bool cmp(line a,line b){  
  28.     return a.value > b.value;  
  29. }  
  30. void getlen(int pos){  
  31.     if(tt[pos].num > 0)  
  32.         tt[pos].len = tt[pos].rp - tt[pos].lp;  
  33.     else if(tt[pos].lp + 1 == tt[pos].rp)  
  34.         tt[pos].len = 0;  
  35.     else  
  36.         tt[pos].len = tt[pos * 2].len + tt[pos * 2 + 1].len;  
  37. }  
  38. void update(int pos,int id){  
  39.     if(tt[pos].lp >= LL[id].lp && tt[pos].rp <= LL[id].rp){  
  40.         tt[pos].num += LL[id].cnt;  
  41.         getlen(pos);  
  42.         return;  
  43.     }  
  44.     if(tt[pos].lp + 1 == tt[pos].rp)  
  45.         return;  
  46.     int mid = tt[pos].getmid();  
  47.     if(LL[id].lp >= mid){  
  48.        update(pos * 2 + 1,id);  
  49.     }  
  50.     else if(LL[id].rp <= mid){  
  51.        update(pos * 2,id);  
  52.     }  
  53.     else{  
  54.        update(pos * 2,id);  
  55.        update(pos * 2 + 1,id);  
  56.     }  
  57.     getlen(pos);  
  58. }  
  59. int main(){  
  60.     //freopen("1.txt","r",stdin);  
  61.     int a,b,c,d,tot = 0;  
  62.     while(scanf("%d%d%d%d",&a,&b,&c,&d) && (a + b + c + d) != -4){  
  63.         tot = 0;  
  64.         LL[tot].lp = a; LL[tot].rp = c; LL[tot].value = d; LL[tot++].cnt = 1;  
  65.         LL[tot].lp = a; LL[tot].rp = c; LL[tot].value = b; LL[tot++].cnt = -1;  
  66.         int x1,y1,x2,y2;  
  67.         while(1){  
  68.            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);  
  69.            if(x1 + y1 + x2 + y2 == -4)  
  70.                break;  
  71.            LL[tot].lp = x1; LL[tot].rp = x2; LL[tot].value = y2; LL[tot++].cnt = 1;  
  72.            LL[tot].lp = x1; LL[tot].rp = x2; LL[tot].value = y1; LL[tot++].cnt = -1;  
  73.         }  
  74.         built_tree(0,N,1);  
  75.         sort(LL,LL+tot,cmp);  
  76.         update(1,0);  
  77.         long long ans = 0;  
  78.         for(int i = 1; i < tot; ++i){  
  79.            ans += tt[1].len * (LL[i-1].value - LL[i].value);  
  80.            update(1,i);  
  81.         }  
  82.         printf("%lld\n",ans);  
  83.     }  
  84.     return 0;  
  85. }  
posted on 2012-10-17 09:56  pc蛋蛋  阅读(212)  评论(0编辑  收藏  举报