【扫描线】 codeforces 391D2 Supercollider

注意到没有两条共线的线段具有公共点,没有重合的线段

说明每个十字形最多涉及一个水平线段和一个竖直线段

这说明我们可以二分了:每条线段两端砍掉delta长度后,有没有线段有公共点?

很水的扫描线吧~  按 X 轴扫描,先把横的线段扫进去,再用竖的线段去查询

但是写法是很重要的,我说我不用线段树你信喵?我说我连离散化都不用你信喵?
只存操作(包括插入、删除、询问)真是无比的高大上,跪跪跪

还有那鬼畜的查询写法

(set).lower_bound(l)!=(set).upper_bound(r)

可以查询 [l..r] 中是否有数存在,至于为什么这样是对的而不是别的样子了,大家在纸上画画就知道了吧

其实更好理解的是

当 (set).lower_bound(l)==(set).upper_bound(r) 时, [l..r] 中一定没有数

lower_bound是为了将 l 算入 [l..r] 中,upper_bound(r) 也是为了将 r 算入 [l..r] 中
#include <iostream>  
#include <queue>  
#include <stack>  
#include <map>  
#include <set>  
#include <bitset>  
#include <cstdio>  
#include <algorithm>  
#include <cstring>  
#include <climits>  
#include <cstdlib>
//#include <cmath>
#include <time.h>
#define maxn 300005
#define maxm 100005
#define eps 1e-10
#define mod 1000000007
#define INF 999999999
#define lowbit(x) (x&(-x))
#define mp mark_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid  
#define rson o<<1 | 1, mid+1, R  
typedef long long LL;
//typedef int LL;
using namespace std;
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}
LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);}
// head

struct point
{
	int a, b, h;
}line[maxn];
struct node
{
	int k, h, x;
}op[maxn];
set<int> s;
int x1[maxn], x2[maxn], y1[maxn], y2[maxn];
int n, m1, m2;

void read(void)
{
	/*
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		scanf("%d%d%d%d", &x1[i], &y1[i], &x2[i], &y2[i]);
		if(x1[i] == x2[i]) {
			if(y1[i] > y2[i]) swap(y1[i], y2[i]);
		}
		else {
			if(x1[i] > x2[i]) swap(x1[i], x2[i]);
		}
	}
	*/
	scanf("%d%d", &m1, &m2), n = m1+m2;
	for(int i = 1; i <= m1; i++) {
		scanf("%d%d%d", &x1[i], &y1[i], &y2[i]), x2[i] = x1[i], y2[i] += y1[i];
		if(y1[i] > y2[i]) swap(y1[i], y2[i]);
	}
	for(int i = m1+1; i <= n; i++) {
		scanf("%d%d%d", &x1[i], &y1[i], &x2[i]), y2[i] = y1[i], x2[i] += x1[i];
		if(x1[i] > x2[i]) swap(x1[i], x2[i]);
	}
}
int cmp1(node a, node b)
{
	if(a.h == b.h) return a.k < b.k;
	else return a.h < b.h;
}
int cmp2(point a, point b)
{
	return a.h < b.h;
}
bool check(int x)
{
	m1 = m2 = 0;
	for(int i = 1; i <= n; i++) {
		if(x1[i] == x2[i]) {
			if(y2[i] - y1[i] >= 2*x) {
				op[m1].k = 1, op[m1].x = x1[i], op[m1++].h = y1[i]+x;
				op[m1].k = -1, op[m1].x = x1[i], op[m1++].h = y2[i]-x+1;
			}
		}
		else {
			if(x2[i] - x1[i] >= 2*x) {
				line[m2].a = x1[i]+x;
				line[m2].b = x2[i]-x;
				line[m2++].h = y1[i];
			}
		}
	}
	sort(op, op+m1, cmp1);
	sort(line, line+m2, cmp2);
	s.clear();
	for(int i = 0, j = 0; i < m2; i++) {
		while(j < m1 && line[i].h >= op[j].h)
			if(op[j].k == 1) s.insert(op[j++].x);
			else s.erase(op[j++].x);
		if(s.lower_bound(line[i].a) != s.upper_bound(line[i].b)) return true;
	}
	return false;
}
void work(void)
{
	int bot = 1, top = 1000000000, mid, res = -1;
	while(top >= bot) {
		mid = (top + bot) >> 1;
		if(check(mid)) bot = mid+1, res = mid;
		else top = mid-1;
	}
	if(~res) printf("%d\n", res);
	else printf("0\n");
}
int main(void)
{
	read();
	work();
	return 0;
}

你可能感兴趣的:(codeforces)