Incredible Crazily Progressing Company (ICPC) suffered a lot with the low speed of procedure. After investigation, they found that the bottleneck was at Absolutely Crowded Manufactory (ACM). In oder to accelerate the procedure, they bought a new machine for ACM. But a new problem comes, how to place the new machine into ACM?
ACM is a rectangular factor and can be divided into W * H cells. There are N retangular old machines in ACM and the new machine can not occupy any cell where there is old machines. The new machine needs M consecutive cells. Consecutive cells means some adjacent cells in a line. You are asked to calculate the number of ways to choose the place for the new machine.
Input
There are multiple test cases (no more than 50). The first line of each test case contains 4 integers W, H, N, M (1 ≤ W, H ≤ 107, 0 ≤ N ≤ 50000, 1 ≤ M ≤ 1000), indicating the width and the length of the room, the number of old machines and the size of the new machine. Then N lines follow, each of which contains 4 integers Xi1, Yi1, Xi2 and Yi2 (1 ≤ Xi1 ≤ Xi2 ≤ W, 1 ≤ Yi1 ≤ Yi2 ≤ H), indicating the coordinates of the i-th old machine. It is guarantees that no cell is occupied by two old machines.
Output
Output the number of ways to choose the cells to place the new machine in one line.
Sample Input
3 3 1 2 2 2 2 2 3 3 1 3 2 2 2 2 2 3 2 2 1 1 1 1 2 3 2 3
Sample Output
8 4 3My Code
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 #define lson l,m,rt<<1 5 #define rson m+1,r,rt<<1|1 6 #define maxn 100100 7 struct node 8 { 9 int len,c; 10 }setree[maxn<<2]; 11 struct op 12 { 13 int l,r,cnt,h; 14 }mes[maxn],mes1[maxn]; 15 int sorted[maxn],sorted1[maxn]; 16 long long ans; 17 int w,h,n,m; 18 bool cmp(struct op a,struct op b) 19 { 20 return a.h<b.h; 21 } 22 void build(int l,int r,int rt) 23 { 24 setree[rt].c=0; 25 setree[rt].len=0; 26 if(l==r) 27 return; 28 int m=(l+r)>>1; 29 build(lson); 30 build(rson); 31 } 32 int binsearch(int a[],int l,int r,int num) 33 { 34 int m=(l+r)>>1; 35 if(a[m]==num) 36 return m; 37 if(num<a[m]) 38 return binsearch(a,l,m-1,num); 39 return binsearch(a,m+1,r,num); 40 } 41 void pushup(int a[],int rt,int l,int r) 42 { 43 if(setree[rt].c>0) 44 setree[rt].len=a[r]-a[l-1]; 45 else 46 setree[rt].len=setree[rt<<1].len+setree[rt<<1|1].len; 47 48 } 49 void update(int a[],int l,int r,int rt,int L,int R,int c) 50 { 51 if(L<=l&&r<=R){ 52 setree[rt].c+=c; 53 if(setree[rt].c>0) 54 setree[rt].len=a[r]-a[l-1]; 55 else{ 56 if(l==r) 57 setree[rt].len=0; 58 else 59 setree[rt].len=setree[rt<<1].len+setree[rt<<1|1].len; 60 } 61 return; 62 } 63 int m=(l+r)>>1; 64 if(L<=m) 65 update(a,lson,L,R,c); 66 if(R>m) 67 update(a,rson,L,R,c); 68 pushup(a,rt,l,r); 69 } 70 void solve() 71 { 72 sort(mes,mes+2*n,cmp); 73 sort(sorted,sorted+2*n); 74 int k=1; 75 long long s=0; 76 for(int i=1;i<2*n;i++) 77 if(sorted[i]!=sorted[i-1]) 78 sorted[k++]=sorted[i]; 79 build(1,k,1); 80 for(int i=0;i<2*n;i++){ 81 int l=binsearch(sorted,0,k-1,mes[i].l); 82 int r=binsearch(sorted,0,k-1,mes[i].r); 83 update(sorted,1,k,1,l+1,r,mes[i].cnt); 84 s+=(long long)(mes[i+1].h-mes[i].h)*setree[1].len; 85 } 86 ans+=(long long)w*h-s; 87 } 88 void solve1() 89 { 90 sort(mes1,mes1+2*n,cmp); 91 sort(sorted1,sorted1+2*n); 92 int k=1; 93 long long s=0; 94 for(int i=1;i<2*n;i++) 95 if(sorted1[i]!=sorted1[i-1]) 96 sorted1[k++]=sorted1[i]; 97 build(1,k,1); 98 for(int i=0;i<2*n;i++){ 99 int l=binsearch(sorted1,0,k-1,mes1[i].l); 100 int r=binsearch(sorted1,0,k-1,mes1[i].r); 101 update(sorted1,1,k,1,l+1,r,mes1[i].cnt); 102 s+=(long long)(mes1[i+1].h-mes1[i].h)*setree[1].len; 103 } 104 ans+=(long long)w*h-s; 105 } 106 int main() 107 { 108 while(~scanf("%d%d%d%d",&w,&h,&n,&m)){ 109 ans=0; 110 for(int i=0;i<n;i++){ 111 int a,b,c,d; 112 scanf("%d%d%d%d",&a,&b,&c,&d); 113 mes[2*i].l=a-m>0?a-m:0;mes[2*i].r=c;mes[2*i].cnt=1;mes[2*i].h=b-1; 114 mes[2*i+1].l=a-m>0?a-m:0;mes[2*i+1].r=c;mes[2*i+1].cnt=-1;mes[2*i+1].h=d; 115 sorted[2*i]=a-m>0?a-m:0;sorted[2*i+1]=c; 116 117 mes1[2*i].l=a-1;mes1[2*i].r=c;mes1[2*i].cnt=1;mes1[2*i].h=b-m>0?b-m:0; 118 mes1[2*i+1].l=a-1;mes1[2*i+1].r=c;mes1[2*i+1].cnt=-1;mes1[2*i+1].h=d; 119 sorted1[2*i]=a-1;sorted1[2*i+1]=c; 120 } 121 if(m>1){ 122 mes[2*n].l=w-m+1>0?w-m+1:0;mes[2*n].r=w;mes[2*n].h=0;mes[2*n].cnt=1; 123 mes[2*n+1].l=w-m+1>0?w-m+1:0;mes[2*n+1].r=w;mes[2*n+1].h=h;mes[2*n+1].cnt=-1; 124 sorted[2*n]=w-m+1>0?w-m+1:0;sorted[2*n+1]=w; 125 126 mes1[2*n].l=0;mes1[2*n].r=w;mes1[2*n].h=h-m+1>0?h-m+1:0;mes1[2*n].cnt=1; 127 mes1[2*n+1].l=0;mes1[2*n+1].r=w;mes1[2*n+1].h=h;mes1[2*n+1].cnt=-1; 128 sorted1[2*n]=0;sorted1[2*n+1]=w; 129 n++; 130 } 131 solve(); 132 solve1(); 133 if(m==1) 134 ans/=2; 135 printf("%lld\n",ans); 136 } 137 }
来源:https://www.cnblogs.com/kim888168/archive/2013/02/15/2912839.html