涂颜色,算面积

xy平面上有一个矩形,它的左下角在(0,0),右上角在(W,H)。它的每条边都平行于x轴或y轴。最初,矩形内的整个区域被涂成白色。

斯达克把N个点画在矩形上。第i个(1≤i≤N)点的坐标为(xi,yi)。

然后,他创建了一个长度为N的整数序列a,对于每个1≤i≤N,他将矩形内的某个区域涂成黑色,如下所示:

如果ai=1,则在矩形内画出满足x<xi的区域。

如果ai=2,他在矩形内画出满足x>xi的区域。

如果ai=3,则在矩形内画出满足y<yi的区域。

如果ai=4,则在矩形内画出满足y>yi的区域。

在他完成绘画后,找出矩形内白色区域的面积。

输入

The input is given from Standard Input in the following format:
W H N
x1 y1 a1
x2 y2 a2
:
xN yN aN

输出

Print the area of the white region within the rectangle after Snuke finished painting.

样例输入 Copy

5 4 2
2 1 1
3 3 4

样例输出 Copy

9

提示

The figure below shows the rectangle before Snuke starts painting.
 
First, as (x1,y1)=(2,1) and a1=1, he paints the region satisfying x<2 within the rectangle:

Then, as (x2,y2)=(3,3) and a2=4, he paints the region satisfying y>3 within the rectangle:

Now, the area of the white region within the rectangle is 9.
 
简单思维题:
只要每一次更新边界就行。
AC代码:
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
typedef long long ll;
const int maxn=1e3+10;
const int M=1e7+10;
const int INF=0x3f3f3f3f;
int vis[maxn][maxn];
int a[maxn],b[maxn],c[maxn];
int w,h,n;
void inint(){
    cin>>w>>h>>n;
    for(int i=0;i<n;i++){
        cin>>a[i]>>b[i]>>c[i];
    }
}
int main()
{
    inint();
    int w1=0,h1=0;
    int w2=w,h2=h; 
    for(int i=0;i<n;i++){
        if(c[i]==1){
            w1=max(w1,a[i]);
        }
        else if(c[i]==2){
            w2=min(w2,a[i]);
        }
        else if(c[i]==3){
            h1=max(h1,b[i]);
        }
        else if(c[i]==4){
            h2=min(h2,b[i]);
        }
    }
    if(h2-h1>0&&w2-w1>0){
        printf("%d",(h2-h1)*(w2-w1));
    }
    else{
        printf("0");
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/lipu123/p/12217150.html