poj 3695 Rectangles(容斥原理)
来源:互联网 发布:威海市网络推广专员 编辑:程序博客网 时间:2024/05/18 01:33
在容斥原理题单里看到这个题,第一想法肯定是扫描线啊。但一看题单分析,还真是容斥。矩形相交的图形和文氏图差不多。然后dfs容斥就好了
题单里第四题:http://blog.csdn.net/shengtao96/article/details/52490020
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;struct Rec{ int x1,y1,x2,y2;};Rec rec[30];int nums[30];int calc(Rec& t){ return (t.x2-t.x1)*(t.y2-t.y1);}bool Union(Rec& a, Rec b){ Rec t; //左边界 t.x1 = max(a.x1,b.x1); //下边界 t.y1 = max(a.y1,b.y1); //右边界 t.x2 = min(a.x2,b.x2); //上边界 t.y2 = min(a.y2,b.y2); if(t.x1 < t.x2 && t.y1 < t.y2) { a = t; return false; } return true;}int dfs(int n, Rec t, int num){ if(n == -1) { if(num) { if(num&1) return calc(t); else return -1*calc(t); } return 0; } int ans = dfs(n-1,t,num); //俩矩阵没交集就返回,有交集就继续 if(Union(t,rec[nums[n]-1])) return ans; ans += dfs(n-1,t,num+1); return ans;}int main(){ int n,m; int time = 0; while(scanf("%d %d",&n,&m) && n+m) { for(int i = 0; i < n; ++i) scanf("%d %d %d %d",&rec[i].x1,&rec[i].y1,&rec[i].x2,&rec[i].y2); int r,res; printf("Case %d:\n",++time); for(int i = 1; i <= m; ++i) { scanf("%d",&r); for(int i = 0; i < r; ++i) scanf("%d",&nums[i]); Rec t; t.x1 = t.y1 = -1; t.x2 = t.y2 = 1001; res = dfs(r-1,t,0); printf("Query %d: %d\n",i,res); } printf("\n"); } return 0;}
阅读全文
0 0
- Rectangles - POJ 3695 容斥原理
- poj 3695 Rectangles(容斥原理)
- POJ 3695 Rectangles(容斥)
- poj 3695 Rectangles 线段树扫描线 or 容斥原理
- HDU 2461 Rectangles 容斥原理
- [容斥原理] hdu 2461 Rectangles
- HDU-2461 Rectangles(容斥原理)
- hdu2461 Rectangles-----容斥
- POJ 3695 Rectangles 1w询问求20个矩阵面积并 容斥
- POJ 3695 Rectangles (矩形并 状压+容斥定理 好题)
- hdu-2461-Rectangles(容斥)
- poj 1091 (容斥原理)
- poj 3904 容斥原理
- POJ 3904(容斥原理)
- POJ 3904 容斥原理
- Rectangles POJ 3695
- POJ 3695 Rectangles 笔记
- POJ 3904 Sky Code 容斥原理
- neural networks deep learning Python Basics with numpy (optional) Homework
- android应用开发-从设计到实现 4-7天气详情的布局
- Divide Two Integers LeetCode 解题报告
- 好用的工具
- Alpha选股:资本资产定价模型(CAPM)
- poj 3695 Rectangles(容斥原理)
- 题目1040:Prime Number
- 源代码安全管理
- 【jzoj5329】【NOIP2017提高A组模拟8.22】【时间机器】【数据结构】【扫描线】
- SAP/ABAP: Smartforms, SSF_OPEN, SSF_CLOSE
- POJ 2417 Discrete Logging(离散对数 BSGS)
- 记一次WebService失败重构
- java.lang.NoClassDefFoundError:com/fasterxml/jackson/databind/ObjectMapper
- Android 8.0 正式发布,Google 带来了哪些新功能?