【ZOJ】4012 Your Bridge is under Attack
【ZOJ】4012 Your Bridge is under Attack
平面上随机n个点,然后给出m条直线,问直线上有几个点
\(n,m \leq 10^{5}\)
由于共线的点不会太多,于是我们可以建KD树出来直接查询,这条直线和某个矩形不相交则不搜索这个子树
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,M,d;
pii p[MAXN];
#define lc(u) tr[u].lc
#define rc(u) tr[u].rc
#define siz(u) tr[u].siz
struct node {
int lc,rc,siz,x,y;
int x1,y1,x2,y2;
}tr[MAXN];
int rt,Ncnt;
void upmin(int &x,int y) {x = min(x,y);}
void upmax(int &x,int y) {x = max(x,y);}
void update(int u) {
siz(u) = siz(lc(u)) + siz(rc(u)) + 1;
tr[u].x1 = tr[u].x2 = tr[u].x;
tr[u].y1 = tr[u].y2 = tr[u].y;
upmin(tr[u].x1,min(tr[lc(u)].x1,tr[rc(u)].x1));
upmax(tr[u].x2,max(tr[lc(u)].x2,tr[rc(u)].x2));
upmin(tr[u].y1,min(tr[lc(u)].y1,tr[rc(u)].y1));
upmax(tr[u].y2,max(tr[lc(u)].y2,tr[rc(u)].y2));
}
bool cmp(pii a,pii b) {
if(d == 0) return a.fi < b.fi || (a.fi == b.fi && a.se < b.se);
else return a.se < b.se || (a.se == b.se && a.fi < b.fi);
}
bool check(int u,int a,int b) {
if(1LL * (tr[u].x2 - a) * b + 1LL * tr[u].y2 * a < 0) return false;
if(1LL * (tr[u].x1 - a) * b + 1LL * tr[u].y1 * a > 0) return false;
return true;
}
void build(int &u,int l,int r,int D) {
if(l > r) return;
u = ++Ncnt;
if(l == r) {
lc(u) = rc(u) = 0;
tr[u].x = p[l].fi;tr[u].y = p[l].se;
update(u);return;
}
d = D;
int mid = (l + r) >> 1;
nth_element(p + l,p + mid,p + r + 1,cmp);
tr[u].x = p[mid].fi;tr[u].y = p[mid].se;
build(lc(u),l,mid - 1,D ^ 1);
build(rc(u),mid + 1,r,D ^ 1);
update(u);
}
int Query(int u,int a,int b) {
if(!check(u,a,b)) return 0;
int res = 0;
if(1LL * (tr[u].x - a) * b + 1LL * a * tr[u].y == 0) ++res;
res += Query(lc(u),a,b);
res += Query(rc(u),a,b);
return res;
}
void Solve() {
Ncnt = 0;rt = 0;
tr[0].x1 = tr[0].y1 = 1e9 + 10;
tr[0].y2 = tr[0].y2 = -1;
read(N);read(M);
for(int i = 1 ; i <= N ; ++i) {
read(p[i].fi);read(p[i].se);
}
build(rt,1,N,0);
int a,b;
for(int i = 1 ; i <= M ; ++i) {
read(a);read(b);
out(Query(rt,a,b));enter;
}
}
int main(){
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
int T;
read(T);
for(int i = 1 ; i <= T ; ++i) Solve();
return 0;
}
【ZOJ】4012 Your Bridge is under Attack的更多相关文章
- 【ZOJ】【3329】One Person Game
概率DP/数学期望 kuangbin总结题目中的第三道 看来还是没有进入状态啊……都说是DP了……当然是要找[状态之间的转移关系]了…… 本题中dp[i]跟 dp[i-(k1+k2+k3)] 到dp[ ...
- 【有上下界网络流】【ZOJ】2314 Reactor Cooling
[算法]有上下界网络流-无源汇(循环流) [题解]http://www.cnblogs.com/onioncyc/p/6496532.html //未提交 #include<cstdio> ...
- 【设计模式】—— 桥接模式Bridge
前言:[模式总览]——————————by xingoo 模式意图 这个模式使用的并不多,但是思想确实很普遍.就是要分离抽象部分与实现部分. 实现弱关联,即在运行时才产生依赖关系. 降低代码之间的耦合 ...
- 【ZOJ】3785 What day is that day? ——浅谈KMP在ACM竞赛中的暴力打表找规律中的应用
转载请声明出处:http://www.cnblogs.com/kevince/p/3887827.html ——By Kevince 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这 ...
- 【Centos7】Delete virtual bridge
Previously,Stop service which controls virtual bridges. sudo systemctl stop libvirtd.service #System ...
- 【设计模式】桥接模式 Bridge Pattern
开篇还是引用吕振宇老师的那篇经典的文章<设计模式随笔-蜡笔与毛笔的故事>.这个真是太经典了,没有比这个例子能更好的阐明桥接模式了,这里我就直接盗来用了. 现在市面上卖的蜡笔很多,各种型号, ...
- 【BZOJ】4012: [HNOI2015]开店
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4012 给出一个$n$个点的树,树上每一个点都有一个值$age$,每条边都有边权,每次查询一 ...
- 【ZOJ】3740:Water Level【DP】
Water Level Time Limit: 2 Seconds Memory Limit: 65536 KB Hangzhou is a beautiful city, especial ...
- 【ZOJ】3785 What day is that day? ——KMP 暴力打表找规律
转自:http://www.cnblogs.com/kevince/p/3887827.html 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这么一说大家心里肯定有数了吧,“不就是nex ...
随机推荐
- 内存分析工具 MAT 的使用【转】
转自:http://blog.csdn.net/aaa2832/article/details/19419679/ 1 内存泄漏的排查方法 Dalvik Debug Monitor Server (D ...
- Tkinter 之Combobox下拉
一.参数说明 语法 作用 cv = tk.stringVar() 绑定变量 com = ttk.Combobox(root, textvariable=cv) 创建下拉框 com.pack() 放置下 ...
- 在OpenFOAM中做用户自定义库——编译library【转载】
转载自:http://openfoam.blog.sohu.com/22041538.html OpenFOAM自己提供的标准类都是以库的形式提供的,并且利用头文件给出了库的应用接口.这样一来,用户的 ...
- mac电脑如何快速显示桌面及切换应用
使用mac电脑时,我们习惯打开很多应用,文档等等.如果打开应用非常多,需要操作桌面,却不知如何快速返回桌面和切换应用时,操作就非常不便了,下面简单介绍mac电脑系统如何快速显示桌面及切换应用? 工具/ ...
- ASP.NET MVC传递Model到视图的多种方式之通用方式的使用
ASP.NET MVC传递Model到视图的多种方式总结——通用方式的使用 有多种方式可以将数据传递到视图,如下所示: ViewData ViewBag PartialView TempData Vi ...
- Time类
public class Demo_Timer { /** * @param args * 计时器 * @throws InterruptedException */ public static vo ...
- Qt编写自定义控件39-导航标签
一.前言 在很多菜单导航界面中,当单击了二级菜单或者三级菜单以后,顶部会显示带箭头或者其他标识的导航标签,可以单击该标签快速切换到对应的界面,也作为指示当前处于哪一级菜单下的界面,主要在WEB中大肆流 ...
- 学习TypeScript 笔记
TypeScript 什么是TypeScript TypeScript 是 JavaScript 的一个超集,支持 ECMAScript 6 标准. TypeScript 由微软开发的自由和开源的编程 ...
- Apache配置优化之开启GZip传输
1.确保apache已经编译的模块里有mod_deflate模块 2.确保apache的配置文件里引入了压缩的模块 3.确保要开启Gzip压缩的虚拟主机配置里有如下配置,并重启apache服务:如果要 ...
- Release报错Debug无错
代码在Release模式下会crash,Debug模式下可以运行,最后定位到原因 for (size_t j = 0; j < ids.size()-1; ++j) { } 发现问题是Relea ...