【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. 安装less/sass

    安装sass npm i node-sass 安装wepy-compiler-sass插件 npm install wepy-compiler-sass --save-dev 在我的项目中使用才有用.

  2. Python如何import其它.py文件及其函数

    ​ 如上图所示,我想在test_1.py文件中import我在lstm_1.py中定义的LstmParam和 LstmNetwork.我直接采用的是最简单的引用方法:from lstm_1 impor ...

  3. python常用函数1

    map()函数 map()是python 内置 的高届函数 ,接收一个函数  f  和一个list,并通过把函数  f  依次作用在list的每个元素上,得到一个新的 list 并返回. 比如,对于l ...

  4. dsu on tree学习笔记

    前言 一次模拟赛的\(T3\):传送门 只会\(O(n^2)\)的我就\(gg\)了,并且对于题解提供的\(\text{dsu on tree}\)的做法一脸懵逼. 看网上的其他大佬写的笔记,我自己画 ...

  5. ORBSLAM2单目初始化过程

    ORBSLAM2单目初始化过程 转自博客:https://blog.csdn.net/zhubaohua_bupt/article/details/78560966 ORB单目模式的初始化过程可以分为 ...

  6. CodeForces - 1175B Catch Overflow!(栈模拟多重for循环)

    You are given a function ff written in some basic language. The function accepts an integer value, w ...

  7. legend3---开发总结1

    legend3---开发总结1 一.总结 一句话总结: 管它柳绿花红,我自潇洒人间 1.数据库字段命名:比如l_id? 见名知意,字段唯一,方便连表 2.所有方法在服务端都要加验证? 比如先查数据再更 ...

  8. Mysql --- Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

    我使用的5.5的mysql数据库会报这个错, 换成5.7的就可以了

  9. SpringBoot视图层技术

    一.SpringBoot整合jsp 在maven的dependencies的依赖中除了springBoot启动器还要添加对jstl和jsp的依赖. <dependencies> <d ...

  10. LoadRunne遇到的一些问题FAQ(持续更新...)

    1.LR11破解完成,添加License失败,报错License security violation Loadrunner11破解成功后,用deletelicense.exe工具把License全删 ...