hdu 2461 容斥原理 Rectangle

题意:http://acm.hdu.edu.cn/showproblem.php?pid=2461

        给你n(n很小)个长方形,求这中间任意长方形的面积并。

题解:

        容斥原理。

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int n,Q;
struct Rec{
    int x1,x2,y1,y2;
}rec[30];
int s[(1 << 20) + 3], a[22],cnt,add,id;
Rec intersec(Rec a, Rec b){
    Rec ret;
    if(a.x2 <= b.x1 || b.x2 <= b.x1 || a.y2 <= b.y1 || b.y2 <= a.y1){
        ret.x1 = ret.y1 = ret.x2 = ret.y2 = 0;
        return ret;
    }
    ret.x1 = max(a.x1, b.x1);
    ret.y1 = max(a.y1, b.y1);
    ret.x2 = min(a.x2, b.x2);
    ret.y2 = min(a.y2, b.y2);
    return ret;
}
int Area(Rec r){
    if(r.x1 >= r.x2 || r.y1 >= r.y2)
        return 0;
    return (r.y2 - r.y1) * (r.x2 - r.x1);
}
int In_exclusion(int k, Rec r){
    if(Area(r) == 0) return 0;//
    int ret = 0;
    for(int i = k; i <= cnt; i ++){
        Rec tmp = intersec(rec[a[i]],r);
        ret += Area(tmp) - In_exclusion(i + 1,tmp);
    }
    return ret;
}
int main(){
    int k,ca = 1;
    while(~scanf("%d%d",&n,&Q) && (n || Q)){
        printf("Case %d:\n",ca ++);
        Rec total ;
        total.x1 = total.y1 = 0;
        total.x2 = total.y2 = 1000;
        memset(s,0,sizeof(s));
        for(int i = 1; i <= n; ++i){
            scanf("%d%d%d%d",&rec[i].x1,&rec[i].y1,&rec[i].x2,&rec[i].y2);
        }
        for(int qu = 1; qu <= Q; qu ++){
            scanf("%d",&k);
            cnt = 0, add = 0;
            memset(a,0,sizeof(a));
            while(k --){
                scanf("%d",&id);
                add = (add | (1 << (id - 1)));
                a[cnt ++] = id;
            }
            if(s[add] == 0)
                s[add] = In_exclusion(0,total);
            printf("Query %d: %d\n",qu, s[add]);
        }
        printf("\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值