【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. Ubuntu输入ifconfig找不到IP地址,只有lo问题

    首先我的问题是这样的 然后一顿神百度,要找到你之前用的网卡或者设备,ifconfig -a,然后好用,出来了 但是并没有IP地址什么的,网上给的重启命令也没有用 输入的命令是:ifconfig ens ...

  2. 生成一张带有logo的二维码图片

    string url = 二维码内容; , , Encoding.UTF8); // 合成活动的LOGO图片 var hasImage = GlobalTools.GetCommonContent(& ...

  3. Python基础之只接收关键字参数的函数

    当我们希望函数的某些参数强制使用关键字参数时,可以将强制关键字参数放到某个*后面就能得到这种效果.比如: def recv(maxsize, *, block): """ ...

  4. Java实现多线程生产者消费者模式的两种方法

    生产者消费者模式:生产者和消费者在同一时间段内共用同一存储空间,生产者向空间里生产数据,而消费者取走数据.生产者生产一个,消费者消费一个,不断循环. 第一种实现方法,用BlockingQueue阻塞队 ...

  5. 怎么样使用vuex

    https://www.cnblogs.com/songrimin/p/7815850.html

  6. 将Excel文件导入到Navicat Premium中日期变为0000-00-00

    在网上查询了一些文章,说要改excel的单元格格式,我试验了好几次,不管是设置成文本,还是日期,都始终是 0000-00-00. 最后改变了办法,把数据库中的date类型设置 成 varchar,等导 ...

  7. 《Glibc内存管理》笔记DAY2

    目录 Ptmalloc内存管理设计 Main_arena 与 non_main_arena chunk 的组织 空闲 chunk 容器 sbrk 与 mmap 内存分配概述 内存回收概述 边界标记法 ...

  8. php实现excel单元格合并,字体加粗居中等操作

    使用的是phpexcel,基本用的原生语法,所见即所得,直接复制下面代码,即可: // 引用phpexcel类 $this->load->library('PHPExcel'); // 创 ...

  9. Linux中显示系统中USB信息的lsusb命令

    来源:Linux中国  原文:https://linux.cn/article-2448-1.html 通用串行总线(USB)被设计成为连接计算机外设的标准,如键盘.鼠标.打印机.数码相机.便携式媒体 ...

  10. Mat转CImage

    uchar *pImg=(uchar *)CI.GetBits();//得到CImage数据区地址 ps=img.ptr<uchar>(i); void MatToCImage( Mat ...