ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【模板】动态凸包

2022-08-18 07:30:43  阅读:139  来源: 互联网

标签:dn return first 凸包 second 动态 it2 模板 it1


好诶,我luogu帮我成功的交了一回codeforces了

\(\textrm{CF70D - Professor's task}\)

#include <stdio.h>
#include <map>
using namespace std;
#define llt long long int
map<int,int> up, dn;
llt cross(int a,int b,int x,int y) {
	return (llt)a*y-(llt)b*x;
}
bool find_up(int x,int y) {
	map<int,int> :: iterator it1 = up.lower_bound(x);
	if(it1 == up.end()) 
		return false;
	if(it1->first == x) 
		return y <= it1->second;
	if(it1 == up.begin()) 
		return false;
	map<int,int> :: iterator it2 = it1;
	--it2;
	return cross(it1->first-it2->first,it1->second-it2->second,x-it2->first,y-it2->second) <= 0;
}
bool find_dn(int x,int y) {
	map<int,int> :: iterator it1 = dn.lower_bound(x);
	if(it1 == dn.end()) 
		return false;
	if(it1->first == x) 
		return y >= it1->second;
	if(it1 == dn.begin()) 
		return false;
	map<int,int> :: iterator it2 = it1;
	--it2;
	return cross(it1->first-it2->first,it1->second-it2->second,x-it2->first,y-it2->second) >= 0;
}
bool delete_up(map<int,int> :: iterator it) {
	if(it == up.begin()) 
		return false;
	map<int,int> :: iterator it1 = it, it2 = it;
	--it1;
	++it2;
	if(it2 == up.end()) 
		return false;
	if(cross(it->first-it1->first,it->second-it1->second,it2->first-it1->first,it2->second-it1->second) >= 0) {
		up.erase(it);
		return true;
	}
	return false;
}
bool delete_dn(map<int,int> :: iterator it) {
	if(it == dn.begin()) 
		return false;
	map<int,int> :: iterator it1 = it, it2 = it;
	--it1;
	++it2;
	if(it2 == dn.end()) 
		return false;
	if(cross(it->first-it1->first,it->second-it1->second,it2->first-it1->first,it2->second-it1->second) <= 0) {
		dn.erase(it);
		return true;
	}
	return false;
}
void insert_up(int x,int y) {
	if(find_up(x,y)) 
		return;
	up[x] = y;
	map<int,int> :: iterator it1 = up.find(x);
	map<int,int> :: iterator it2 = it1;
	if(it1 != up.begin()) {
		--it2;
		while(delete_up(it2++)) 
			--it2;
	}
	if(++it2 != up.end()) {
		while(delete_up(it2--)) 
			++it2;
	}
}
void insert_dn(int x,int y) {
	if(find_dn(x,y)) 
		return;
	dn[x] = y;
	map<int,int> :: iterator it1 = dn.find(x);
	map<int,int> :: iterator it2 = it1;
	if(it1 != dn.begin()) {
		--it2;
		while(delete_dn(it2++)) 
			--it2;
	}
	if(++it2 != dn.end()) {
		while(delete_dn(it2--)) 
			++it2;
	}
}
int n;
signed main() {
	scanf("%d",&n);
	for(int i = 1, opt, x, y;i <= n;++i) {
		scanf("%d %d %d",&opt,&x,&y);
		if(opt == 1) {
			insert_up(x,y);
			insert_dn(x,y);
		} else {
			printf("%s\n",(find_up(x,y)&&find_dn(x,y)) ? "YES" : "NO");
		}
	}
	return 0;
}

标签:dn,return,first,凸包,second,动态,it2,模板,it1
来源: https://www.cnblogs.com/bikuhiku/p/online_2D_convex_hull.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有