HDU 4419 Colourful Rectangle 扫描线

题目链接:点击打开链接

题意:给定由红绿蓝组成的一些矩阵

输出每种颜色对应的面积。

思路:

如果颜色只有一种就是扫描线。

这里先求出包含各类颜色的面积,然后容斥一下得到答案。

画个图就能快速得到答案了

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std; template <class T>
inline bool rd(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
template <class T>
inline void pt(T x) {
	if (x <0) {
		putchar('-');
		x = -x;
	}
	if (x>9) pt(x / 10);
	putchar(x % 10 + '0');
}
#define N 200005
#define L(x) (x<<1)
#define R(x) (x<<1|1)
typedef long long ll;
ll Mid(ll a, ll b){ return (a + b) >> 1; }
//这份代码是对y轴建树,从左到右扫
struct node{ //这个区间被c条线段覆盖了
	ll l, r;
	ll c;  //用来记录重叠情况
	ll cnt, lf, rf;//离散点l对应的实际点lf cnt表示这个区间内存在的线段长度
}tree[N * 4];
struct Line{
	ll x, y1, y2;
	ll f, col;
	Line(ll a = 0.0, ll b = 0.0, ll c = 0.0, ll d = 0, ll e = 0) :x(a), y1(b), y2(c), f(d), col(e){}
}line[N * 2];
bool cmp(Line a, Line b){ return a.x<b.x; }
ll y[N];
void build(ll l, ll r, ll id){
	tree[id].l = l, tree[id].r = r;
	tree[id].cnt = tree[id].c = 0;
	tree[id].lf = y[l]; tree[id].rf = y[r];
	if (l + 1 == r)return;
	ll mid = Mid(l, r);
	build(l, mid, L(id));
	build(mid, r, R(id));
}
void push_up(ll id){
	if (tree[id].c){
		tree[id].cnt = tree[id].rf - tree[id].lf;
		return;
	}
	if (tree[id].l + 1 == tree[id].r) tree[id].cnt = 0;
	else tree[id].cnt = tree[L(id)].cnt + tree[R(id)].cnt;
}
void update(ll id, Line e){
	if (e.y1 == tree[id].lf && tree[id].rf == e.y2){
		tree[id].c += e.f;
		push_up(id);	return;
	}
	if (e.y2 <= tree[L(id)].rf)
		update(L(id), e);
	else if (tree[R(id)].lf <= e.y1)
		update(R(id), e);
	else {
		Line tmp = e;	tmp.y2 = tree[L(id)].rf;	update(L(id), tmp);
		tmp = e;		tmp.y1 = tree[R(id)].lf;	update(R(id), tmp);
	}
	push_up(id);
}
set<ll>myset;
set<ll>::iterator p;
ll siz, top;
ll ALL(int x1, int x2 = -1, int x3 = -1){
	build(1, siz - 1, 1);
	int i = 1, last;
	for (; i < top;i++)
	if (line[i].col == x1 || line[i].col == x2 || line[i].col == x3){
		update(1, line[i]);
		last = i;
		i++; break;
	}
	ll all = 0;
	for (; i < top; i++)
		if (line[i].col == x1 || line[i].col == x2 || line[i].col == x3){
		all += tree[1].cnt * (line[i].x - line[last].x);
		update(1, line[i]);
		last = i;
	}
	return all;
}
int main(){
	ll i, j, n;
	int T, Cas = 1; rd(T);
	ll x1, x2, y1, y2;
	while (T--){
		rd(n);		myset.clear();		top = 1;
		for (i = 1; i <= n; i++){
			char str[2]; scanf("%s", str); int col = 0; if (str[0] == 'G')col = 1; else if (str[0] == 'B')col = 2;
			rd(x1); rd(y1); rd(x2); rd(y2);
			if (x1 > x2)swap(x1, x2); if (y1 > y2)swap(y1, y2);
			myset.insert(y1); myset.insert(y2);
			Line E = Line(x1, y1, y2, 1, col);
			line[top++] = E;
			Line E2 = Line(x2, y1, y2, -1, col);
			line[top++] = E2;
		}
		sort(line + 1, line + top, cmp);
		siz = 1;	for (p = myset.begin(); p != myset.end(); p++)	y[siz++] = *p;
		ll all = ALL(0,1,2);
		ll RG = ALL(0, 1), RB = ALL(0, 2), GB = ALL(1, 2);
		ll R = ALL(0), G = ALL(1), B = ALL(2);

		printf("Case %d:\n", Cas++);
		ll r = all - GB, g = all - RB, b = all - RG;
		ll tmp = all - r - g - b;
		ll gb = tmp - (R - r);
		ll rb = tmp - (G - g);
		ll rg = tmp - (B - b);
		ll rgb = tmp - rg - rb - gb;
		pt(r); puts("");
		pt(g); puts("");
		pt(b); puts("");
		pt(rg); puts("");
		pt(rb); puts("");
		pt(gb); puts("");
		pt(rgb); puts("");

	}
	return 0;
}
时间: 2024-08-07 04:31:32

HDU 4419 Colourful Rectangle 扫描线的相关文章

[HDU 4419] Colourful Rectangle (扫描线 矩形面积并)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:比矩形面积并多了颜色,问染成的每种颜色的面积. 矩形面积并的扫描线维护的是长度,这道题就是维护每个颜色的长度,写起来很蛋疼. 1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 #include <cstring> 5 #include <vector> 6

hdu 4419 Colourful Rectangle

http://acm.hdu.edu.cn/showproblem.php?pid=4419 题意:给出3种颜色,重叠会生成新的颜色,然后有一些矩形,求出每种颜色的面积. 转化为二进制表示颜色:001 R ,010G,100B,011RG,101RB,....111RGB; 在结构体里面加上一个len[8]和cover[8]表示每种颜色所占的长度和在区间的覆盖次数. 1 #include <cstdio> 2 #include <cstring> 3 #include <al

HDU 4419 Colourful Rectangle --离散化+线段树扫描线

题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何维护每种颜色的长度着实让我伤透了脑筋.后来看了一位朋友的题解,才幡然醒悟. 开始想到了用二进制表示颜色,R用001表示,G用010表示,B用100表示.那么就可以用十进制1~7表示7种不同颜色了. 维护 cov[rt][1~3] 表示此区间内3种原色各有多少个, Len[rt][i]表示每种颜色的长

Hdu 4419 Colourful Rectangle(线段树扫描线)

题目大意: 给出多个不同颜色的矩形,求最后覆盖的颜色的面积. 思路分析: 我是自己手动暴力枚举. 比赛的时候漏了一种情况. RGB 可以从 RG+RB组合来(只是举例,就是说可以从两种颜色组合而来). 然后就只需要维护所有的颜色 用扫描线来判断. #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define MAXN 42222 using name

HDU 1505 Largest Rectangle in a Histogram &amp;&amp; HDU 1506 City Game(动态规划)

1506题意:给你连续的直方图(底边边长为1),求连续的矩阵面积. 对每个直方图,分别向左向右进行扩展. #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #include<cmath> #include<iostream> #include <queue> #include <sta

POJ 1177 &amp; HDU 1828 Picture(扫描线 + 求周长)

题目链接: POJ:http://poj.org/problem?id=1177 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1828 几个不错的讲解: http://www.cnblogs.com/scau20110726/archive/2013/04/13/3018702.html http://blog.csdn.net/xingyeyongheng/article/details/8931410 Description A number

HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1506 Appoint description: Description A histogram is a polygon composed of a sequence of rectangles aligned a

HDU 1506 Largest Rectangle in a Histogram

Largest Rectangle in a Histogram Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 150664-bit integer IO format: %I64d      Java class name: Main A histogram is a polygon composed of a sequence of rectangles al

hdu 1506 Largest Rectangle in a Histogram 构造

题目链接:HDU - 1506 A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rec