[POJ3416]Crossing
Problem
给你n个点,m个询问,每个询问有x, y
问以(x,y)为原点建立的平面直角坐标系分割的第一象限和第三象限的点数和减去第二象限和第四象限的点数和
Solution
用2个树状数组维护一条竖直的线的左右两边的点的分布情况
然后对x轴排序,把一个点从右边的树状数组中拿出,放到左边去(相当那条竖直的线在向右扫描)
然后查询处答案即可。
Notice
树状数组用0的坑
Code
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define lowbit(x) (x & -x)
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 500001;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
struct node
{
int x, y, flag, id;
}T[N + 5];
int ans[N + 5];
struct Node
{
int val[3][N + 5];
inline void build(int l, int r)
{
rep(i, l, r)
val[1][i] = val[2][i] = 0;
}
inline void modify(int x, int y, int v)
{
while (y <= N)
{
val[x][y] += v;
y += lowbit(y);
}
}
int query(int x, int y)
{
int s = 0;
while (y)
{
s += val[x][y];
y -= lowbit(y);
}
return s;
}
}BIT;
int cmp(node X, node Y)
{
return X.x < Y.x;
}
int sqz()
{
int H_H = read();
while(H_H--)
{
BIT.build(1, N);
int n = read();
int m = read();
rep(i, 1, n) T[i].x = read(), T[i].y = read() + 1, T[i].flag = 0, T[i].id = i;
rep(i, 1, m) T[i + n].x = read(), T[i + n].y = read() + 1, T[i + n].flag = 1, T[i + n].id = i;
sort(T + 1, T + n + m + 1, cmp);
rep(i, 1, n + m)
if (!T[i].flag) BIT.modify(2, T[i].y, 1);
rep(i, 1, n + m)
{
if (!T[i].flag)
{
BIT.modify(1, T[i].y, 1);
BIT.modify(2, T[i].y, -1);
}
else
{
int sum = 2 * BIT.query(1, T[i].y) + BIT.query(2, N) - BIT.query(2, T[i].y) * 2 - BIT.query(1, N);
ans[T[i].id] = abs(sum);
}
}
rep(i, 1, m) printf("%d\n", ans[i]);
printf("\n");
}
}
[POJ3416]Crossing的更多相关文章
- [LeetCode] Self Crossing 自交
You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to th ...
- 【LeetCode】Self Crossing(335)
1. Description You are given an array x of n positive numbers. You start at point (0,0) and moves x[ ...
- 还记得高中的向量吗?leetcode 335. Self Crossing(判断线段相交)
传统解法 题目来自 leetcode 335. Self Crossing. 题意非常简单,有一个点,一开始位于 (0, 0) 位置,然后有规律地往上,左,下,右方向移动一定的距离,判断是否会相交(s ...
- Crossing River
Crossing River 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=26251 题意: N个人希望去过 ...
- Leetcode: Self Crossing
You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to th ...
- POJ 1700 Crossing River (贪心)
Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9585 Accepted: 3622 Descri ...
- BZOJ1617: [Usaco2008 Mar]River Crossing渡河问题
1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 654 Solved: 4 ...
- UVA 12230 - Crossing Rivers(概率)
UVA 12230 - Crossing Rivers 题目链接 题意:给定几条河,每条河上有来回开的船,某一天出门,船位置随机,如今要求从A到B,所须要的期望时间 思路:每条河的期望,最坏就是船刚开 ...
- BZOJ 1617: [Usaco2008 Mar]River Crossing渡河问题( dp )
dp[ i ] = max( dp[ j ] + sum( M_1 ~ M_( i - j ) ) + M , sum( M_1 ~ M_i ) ) ( 1 <= j < i ) 表示运 ...
随机推荐
- java 虹软ArcFace 2.0,java SDK使用-进行人脸检测
虹软产品地址:http://ai.arcsoft.com.cn/product/arcface.html虹软ArcFace功能简介 人脸检测人脸跟踪人脸属性检测(性别.年龄)人脸三维角度检测人脸对比 ...
- Android开发 打开已存在的项目(以虹软人脸识别sdk的demo为例)
详细流程参考博客https://blog.csdn.net/z979451341/article/details/79468785 个人遇到的问题与注意点 1.下载Demo后忘记修改appid和sdk ...
- Asp.net core 学习笔记 ( IIS, static file 性能优化 )
更新 : 2019-02-06 最后还是把 rewrite 给替换掉了. 所以 rewrite url 也不依赖 iis 了咯. refer : https://docs.microsoft.com/ ...
- centos 7 安装TensorFlow
查看linux版本 uname -a 查看磁盘大小 准备好python 2.7 查看python版本 import sysprint sys.version print sys.version_in ...
- 安装adt插件后工具栏不显示android相关图标
一:问题 安装好ADT后,工具栏不显示android相关的图标: 二:解决办法 这是ec设置问题,解决办法: Eclipse ->window->Perpective->custom ...
- Redis五种数据结构简介-2
https://www.cnblogs.com/ysocean/p/9102811.html
- 第一章 Lab
关于Lab 教材恶意代码分析实战 课后练习恶意代码样本https://practicalmalwareanalysis.com或https://nostarch.com/malware.htm 以下是 ...
- Spring Batch @EnableBatchIntegration 注解
设置一个远程分块任务需要定义一系列的 beans: 一个连接工程来从消息中间件中获得连接,消息中间件包括有(JMS,AMQP 和其他) 一个 MessagingTemplate 来从主向从发送消息, ...
- Confluence 6 修改空间名字和标识
希望修改空间名称: 在空间名称的边上,选择 图标. 输入新的空间名称,然后单击 保存(Save). 标识(Logo )- 修改空间的名称和标识. 重新组织(Reorder) - 拖动来从新组织快捷链 ...
- Javascript的常见数据类型以及相应操作
JavaScript概述 1 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScript提交给国际标准化组织E ...