poj1177


/*********************************
poj  1177    求矩形并的周长
水平和竖直各扫描一次!

采用计算几何上讲的方法,WA了老半天,于是决定用下面这种x轴和y轴各扫描一次的方法。虽然慢了点,但终于过了!

*********************************/


#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <memory>
using namespace std;
const int N = 5005, M = 10000;
struct li{
 int key, l, h;
 void set(int key, int l, int h, bool in){
  this->key = key, this->l = l, this->h = h, this->in = in;
 }
 bool in; 
}lis[N*2];

struct seg{
 int l, r;
 int c, m;
 void set(int l, int r, int c, int m){
  this->l = l, this->r = r, this->c = c, this->m = m;
 }
}segs[N*8+1000];

struct rec{
 int x1, y1;
 int x2, y2;
}recs[N];
int n, cnt;
int keys[N*2];
int hash[20005];

bool cmp(li l1, li l2){
 return l1.key < l2.key;
}

void build_tree(int id, int l, int r){
 segs[id].set(l, r, 0, 0);
 if(l < r - 1){
  int mid = (l + r) >> 1;
  build_tree(2*id+1, l, mid);
  build_tree(2*id+2, mid, r);
 }
}

void buildy(){
 for(int i = 0; i < n; i++){
  lis[2*i].set(recs[i].x1, recs[i].y1, recs[i].y2, true);
  lis[2*i+1].set(recs[i].x2, recs[i].y1, recs[i].y2, false);
  keys[2*i] = recs[i].y1;
  keys[2*i+1] = recs[i].y2;
 }
 sort(keys, keys+2*n);
 cnt = unique(keys, keys+2*n) - keys;
 build_tree(0, 0, cnt-1);
 sort(lis, lis+2*n, cmp);
 for(int i = 0; i < cnt; i++) hash[keys[i]] = i;
}

void buildx(){
 for(int i = 0; i < n; i++){
  lis[2*i].set(recs[i].y1, recs[i].x1, recs[i].x2, true);
  lis[2*i+1].set(recs[i].y2, recs[i].x1, recs[i].x2, false);
  keys[2*i] = recs[i].x1;
  keys[2*i+1] = recs[i].x2;
 }
 sort(keys, keys+2*n);
 cnt = unique(keys, keys+2*n) - keys;
 build_tree(0, 0, cnt-1);
 sort(lis, lis+2*n, cmp);
 for(int i = 0; i < cnt; i++) hash[keys[i]] = i;
}

void update(int id){
 if(segs[id].c > 0) segs[id].m = keys[segs[id].r] - keys[segs[id].l];
 else if(segs[id].l == segs[id].r - 1) segs[id].m = 0;
 else{
  segs[id].m = segs[2*id+1].m + segs[2*id+2].m;
 }
}

void renew(int id, int l, int r, int c){
 if(l <= segs[id].l && segs[id].r <= r){
  segs[id].c += c;
 }else if(segs[id].l < segs[id].r - 1){
  int mid = (segs[id].l + segs[id].r) >> 1;
  if(l < mid) renew(2*id+1, l, r, c);
  if(r > mid) renew(2*id+2, l, r, c);
 }
 update(id);
}

int getlen(){
 int m0;
 m0 = 0;
 int ans = 0, k = 0;
 for(int i = 0; i < 2*n; i++){
  while(i+1 < 2*n && lis[i].key == lis[i+1].key){
   //判断是否有重叠,如果没有就退出这个循环了!!
   if(lis[i+1].l >= lis[i].h || lis[i+1].h <= lis[i].l) break;
   if(lis[i].in) renew(0, hash[lis[i].l], hash[lis[i].h], 1);
   else renew(0, hash[lis[i].l], hash[lis[i].h], -1);
   i++;
  }
  if(lis[i].in) renew(0, hash[lis[i].l], hash[lis[i].h], 1);
  else renew(0, hash[lis[i].l], hash[lis[i].h], -1);
  ans += abs(segs[0].m - m0);
  m0 = segs[0].m;
 }
 return ans;
}

void solve(){
 int ans = 0;
 buildy();
 ans += getlen();
 buildx();
 ans += getlen();
 printf("%d/n", ans);

}

int main(){
 int i;
 while(scanf("%d", &n) != EOF){
  for(i = 0; i < n; i++){
   scanf("%d%d%d%d", &recs[i].x1, &recs[i].y1, &recs[i].x2, &recs[i].y2);
   recs[i].x1 += M, recs[i].x2 += M, recs[i].y1 += M, recs[i].y2 += M;
  }
  solve();
 }
 return 0;
}

 

 

 

/*  //下面是几组数据


2
0 0 5 5
5 0 10 5

2
0 0 5 5
5 5 10 10

2
0 0 5 5
6 0 11 5


47
-1105 -1155 -930 -285
-765 -1115 -615 -375
-705 -480 -165 -285
-705 -1200 -175 -1025
-275 -1105 -105 -385
-10 -1165 185 -285
315 -1160 400 -710
340 -1195 655 -1070
580 -1140 655 -265
325 -480 395 -335
365 -390 620 -265
365 -770 610 -665
815 -1195 1110 -1070
825 -760 1100 -660
810 -405 1115 -275
780 -700 860 -360
1065 -695 1130 -360
775 -1110 860 -735
1070 -1110 1145 -730
-1065 -95 140 260
-725 80 750 460
135 -135 490 840
135 -135 490 750
-520 40 -210 945
-595 620 215 695
670 -5 855 610
550 -75 830 -25
815 240 1085 370
980 -90 1125 145
280 150 490 315
-1035 -155 -845 -90
855 815 950 1030
785 980 860 1165
945 985 1015 1160
730 835 1075 895
875 695 935 790
-1165 420 -520 650
-1090 815 -210 945
-130 800 65 1160
120 980 690 1150
-1140 995 -125 1180
-825 1050 -195 1135
-90 865 10 1090
280 1045 625 1090
-655 1065 -245 1115
-1155 70 -790 315
-1005 110 -825 225

ans:  37000


*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值