【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的更多相关文章

  1. 【ZOJ】【3329】One Person Game

    概率DP/数学期望 kuangbin总结题目中的第三道 看来还是没有进入状态啊……都说是DP了……当然是要找[状态之间的转移关系]了…… 本题中dp[i]跟 dp[i-(k1+k2+k3)] 到dp[ ...

  2. 【有上下界网络流】【ZOJ】2314 Reactor Cooling

    [算法]有上下界网络流-无源汇(循环流) [题解]http://www.cnblogs.com/onioncyc/p/6496532.html //未提交 #include<cstdio> ...

  3. 【设计模式】—— 桥接模式Bridge

    前言:[模式总览]——————————by xingoo 模式意图 这个模式使用的并不多,但是思想确实很普遍.就是要分离抽象部分与实现部分. 实现弱关联,即在运行时才产生依赖关系. 降低代码之间的耦合 ...

  4. 【ZOJ】3785 What day is that day? ——浅谈KMP在ACM竞赛中的暴力打表找规律中的应用

    转载请声明出处:http://www.cnblogs.com/kevince/p/3887827.html    ——By Kevince 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这 ...

  5. 【Centos7】Delete virtual bridge

    Previously,Stop service which controls virtual bridges. sudo systemctl stop libvirtd.service #System ...

  6. 【设计模式】桥接模式 Bridge Pattern

    开篇还是引用吕振宇老师的那篇经典的文章<设计模式随笔-蜡笔与毛笔的故事>.这个真是太经典了,没有比这个例子能更好的阐明桥接模式了,这里我就直接盗来用了. 现在市面上卖的蜡笔很多,各种型号, ...

  7. 【BZOJ】4012: [HNOI2015]开店

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4012 给出一个$n$个点的树,树上每一个点都有一个值$age$,每条边都有边权,每次查询一 ...

  8. 【ZOJ】3740:Water Level【DP】

    Water Level Time Limit: 2 Seconds      Memory Limit: 65536 KB Hangzhou is a beautiful city, especial ...

  9. 【ZOJ】3785 What day is that day? ——KMP 暴力打表找规律

    转自:http://www.cnblogs.com/kevince/p/3887827.html 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这么一说大家心里肯定有数了吧,“不就是nex ...

随机推荐

  1. DEA使用git提交代码时,点了commit之后卡死在performing code analysis部分,或者performing code analysis结束后没有进入下一步操作。

    把"Perform code analysis" 和 "Check TODO" 复选框前面的勾去掉就好了. 这个可能是因为所分析的目标文件太大了,造成一直分析不 ...

  2. javascript 闭包(closure)

    <script type="text/javascript">    //闭包(closure):内层函数可以引用存在于包围它的函数内的变量,即使外层函数的执行已经结束 ...

  3. 下面的代码在Python2中的输出是什么?解释你的答案

    python2 def div1(x,y): print "%s/%s = %s" % (x, y, x/y) def div2(x,y): print "%s//%s ...

  4. elasticsearch update方法报错: Too many dynamic script compilations within, max: [75/5m]

    PUT _cluster/settings    {        "transient" : {            "script.max_compilations ...

  5. 查看日志tail命令

    打开终端,连接jboss: 命令: tail -f -n 500 /var/log/wildfly/wrapper.log

  6. LUA 在C函数中保存状态:registry、reference

    1 背景 lua的值一般都是保存在栈里面,调用函数完毕值在栈会被清掉,从而被GC回收.但有时候C函数需要在函数体的作用域之外保存某些Lua数据,这些数据不能存放在栈里面,有没有全局变量之类的可以存放. ...

  7. ML_Review_PCA(Ch4)

    Note sth about PCA(Principal Component Analysis)   ML6月20日就要考试了,准备日更博客,来记录复习一下这次ML课所学习的一些方法. 博客是在参考老 ...

  8. qt mvc1

    mvc是经典的三层结构,将数据,视图和逻辑分离.Qt中的Model/View框架,实现了这个模式.在Qt中这个模式设计到三个类,model类,view类和delegate类.model类保存数据,vi ...

  9. 微信小程序填坑之路(三):布局适配方案(rpx、px、vw、vh)

    因为小程序是以微信为平台运行的,可以同时运行在android与ios的设备上,所以不可避免的会遇到布局适配问题,特别是在iphone5上,因为屏幕尺寸小的缘故,也是适配问题最多的机型,下面就简单介绍几 ...

  10. windows2012域控

    此次主要是使用服务器搭建域控制器,我主要是用于使用office web apps 和office online server搭建在线Word预览编辑! 一.准备工作 首先准备1台比较干净的服务器,推荐 ...