【题解】LuoGu4623:BUREK

本文介绍了一种判断直线是否穿过三角形的算法,并通过树状数组实现动态更新和查询的功能。利用差分思想,确定直线穿过三角形的条件,再通过树状数组支持动态修改。

原题传送门
直线能穿过三角形,因为直线是平行于坐标轴的,所以很简单
x = p x=p x=p为例

若直线能穿过某一个三角形,那么这条直线必定能穿过三角形的某一条边
换句话说,令三角形三个点中横坐标最小是 x m i n xmin xmin,最大是 x m a x xmax xmax
如果 x m i n < p < x m a x xmin<p<xmax xmin<p<xmax,直线就能穿过这个三角形

所以可以直接差分, d x m i n + 1 + + , d x m a x − − d_{xmin+1}++,d_{xmax}-- dxmin+1++,dxmax

然后我傻傻的写了树状数组,不过也好,还能支持动态修改

Code:

#include <bits/stdc++.h>
#define maxn 1000010
using namespace std;
int n, tree1[maxn << 1], tree2[maxn << 1];

inline int read(){
	int s = 0, w = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
	for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
	return s * w;
}

int lowbit(int x){ return x & -x; }
void update1(int x, int y){ for (; x < maxn; x += lowbit(x)) tree1[x] += y; }
void update2(int x, int y){ for (; x < maxn; x += lowbit(x)) tree2[x] += y; }
int query1(int x){ int s = 0; for (; x; x -= lowbit(x)) s += tree1[x]; return s; }
int query2(int x){ int s = 0; for (; x; x -= lowbit(x)) s += tree2[x]; return s; }

int main(){
	freopen("buerk.in", "r", stdin);
	freopen("buerk.out", "w", stdout);
	n = read();
	for (int i = 1; i <= n; ++i){
		int xmax = 0, xmin = maxn - 1, ymax = 0, ymin = maxn - 1;
		int x = read(), y = read();
		xmax = max(xmax, x), xmin = min(xmin, x), ymax = max(ymax, y), ymin = min(ymin, y);
		x = read(), y = read();
		xmax = max(xmax, x), xmin = min(xmin, x), ymax = max(ymax, y), ymin = min(ymin, y);
		x = read(), y = read();
		xmax = max(xmax, x), xmin = min(xmin, x), ymax = max(ymax, y), ymin = min(ymin, y);
		update1(xmin + 1, 1), update1(xmax, -1), update2(ymin + 1, 1), update2(ymax, -1);
	}
	int m = read();
	while (m--){
		char c = getchar();
		for (; c != 'x' && c != 'y'; c = getchar());
		int x = read();
		if (c == 'x') printf("%d\n", query1(x));
		else printf("%d\n", query2(x));
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值