HDU 2461 Rectangles

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2461


题意:有n个矩形,每次查询m个矩形的面积交。


思路:用dfs容斥定理求面积交,可以写一个剪枝如果当前面积已经为0,就退出。


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)

#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod %100000007

struct node
{
    int x1,x2,y1,y2;
    node(int a = 0,int b = 0 ,int c = 0 ,int d = 0)
    {
        x1 = a;
        y1 = b;
        x2 = c;
        y2 = d;
    }
};

node rect[25];
int pos[25];
int n,m,p;
int ans;

int area(node x)
{
    if ( x.x1 >= x.x2 || x.y1 >= x.y2 ) return 0;
    return (x.x2-x.x1)*(x.y2 - x.y1);
}

node f( node x , int k ) //面积交
{
    if ( x.x1 + x.x2 + x.y1 + x.y2 == 0 ) return rect[k];

    x.x1 = max( x.x1 , rect[k].x1 );
    x.x2 = min( x.x2 , rect[k].x2 );
    x.y1 = max( x.y1 , rect[k].y1 );
    x.y2 = min( x.y2 , rect[k].y2 );
    return x;
}

void dfs(int k , int cnt , node x)
{
    x = f( x , pos[k] );

    if ( area(x) < 1 ) return;
    if ( cnt & 1 )
        ans+=area(x);
    else
        ans-=area(x);
    if ( k == p )
    {
        return;
    }
    rep(i,k+1,p) dfs( i , cnt+1 , x );
}

int main()
{
    int kase = 0;
    while( scanf("%d%d",&n,&m) == 2 )
    {
        if ( m+n == 0 ) break;
        printf("Case %d:\n",++kase);

        rep(i,1,n) scanf("%d%d%d%d" , &rect[i].x1 , &rect[i].y1 , &rect[i].x2 , &rect[i].y2);
        rep(i,1,m)
        {
            scanf("%d",&p);
            rep(j,1,p) scanf("%d",&pos[j]);
            node temp;
            ans = 0;
            rep(j,1,p)
            	dfs( j , 1 , temp );
            printf("Query %d: %d\n",i,ans);
        }
        puts("");
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值