1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| #include <iostream> #include <cstdio> #include <cstring> #include<algorithm> #include <cmath> using namespace std; #define ll long long const int N = 5e3+10; const int inf = 0x3f3f; struct line { int l,r; int in; int h; bool operator < (const line a)const { return h<a.h; } }edge[N*10]; struct node { int l,r; int s; bool lc,rc; int val; int num; int len() { return r-l+1; } }tree[N*40]; void PushUp(int rt) { if(tree[rt].s) { tree[rt]. val=tree[rt].len(); tree[rt].lc=tree[rt].rc=1; tree[rt].num = 1; } else if(tree[rt].l==tree[rt].r) { tree[rt].val = 0; tree[rt].lc=tree[rt].rc=0; tree[rt].num=0; } else { tree[rt].val=tree[rt<<1].val+tree[rt<<1|1].val; tree[rt].lc=tree[rt<<1].lc; tree[rt].rc=tree[rt<<1|1].rc; tree[rt].num=tree[rt<<1].num+tree[rt<<1|1].num-(tree[rt<<1].rc&tree[rt<<1|1].lc); } } void build(int rt,int l,int r) { tree[rt].l=l; tree[rt].r=r; tree[rt].s=tree[rt].val=0; tree[rt].lc=tree[rt].rc=tree[rt].num=0; if(l==r)return ; int mid = (l+r)>>1; build(rt<<1,l,mid); build(rt<<1|1,mid+1,r); } void update(int rt,int l,int r,int c) { if(tree[rt].l>r||tree[rt].r<l)return ; if(tree[rt].l>=l&&tree[rt].r<=r) { tree[rt].s+=c; PushUp(rt); return ; } update(rt<<1,l,r,c); update(rt<<1|1,l,r,c); PushUp(rt); } int main() { int n; while(~scanf("%d",&n)) { int x1,x2,y1,y2; int mx = -inf; int mi = inf; int tot = 0; for(int i=0;i<n;i++) { scanf("%d%d%d%d",&x1,&y1,&x2,&y2); mx=max(mx,max(x1,x2)); mi=min(mi,min(x1,x2)); edge[tot].l=edge[tot+1].l=x1; edge[tot].r=edge[tot+1].r=x2; edge[tot].in=1; edge[tot+1].in=-1; edge[tot].h=y1; edge[tot+1].h=y2; tot+=2; } sort(edge,edge+tot); int ans = 0; int last = 0; build(1,mi,mx); for(int i=0;i<tot;i++) { update(1,edge[i].l,edge[i].r-1,edge[i].in); ans+=abs(tree[1].val-last); ans+=(edge[i+1].h-edge[i].h)*2*tree[1].num; last = tree[1].val; } printf("%d\n",ans); } }
|