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

  1. [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 ...

  2. 【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[ ...

  3. 还记得高中的向量吗?leetcode 335. Self Crossing(判断线段相交)

    传统解法 题目来自 leetcode 335. Self Crossing. 题意非常简单,有一个点,一开始位于 (0, 0) 位置,然后有规律地往上,左,下,右方向移动一定的距离,判断是否会相交(s ...

  4. Crossing River

    Crossing River 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=26251 题意: N个人希望去过 ...

  5. 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 ...

  6. POJ 1700 Crossing River (贪心)

    Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9585 Accepted: 3622 Descri ...

  7. BZOJ1617: [Usaco2008 Mar]River Crossing渡河问题

    1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 654  Solved: 4 ...

  8. UVA 12230 - Crossing Rivers(概率)

    UVA 12230 - Crossing Rivers 题目链接 题意:给定几条河,每条河上有来回开的船,某一天出门,船位置随机,如今要求从A到B,所须要的期望时间 思路:每条河的期望,最坏就是船刚开 ...

  9. 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 )  表示运 ...

随机推荐

  1. 学习笔记29—Linux基础

    一.简单命令: 1.进入文件夹heyi目录:cd ./heyi 2.查看该目录下内容:ls 二.linux压缩和解压缩命令大全 tar命令 解包:tar zxvf FileName.tar 打包:ta ...

  2. pipenv安装.whl

    windows下很多库安装不方便,主要是编译C之类的. 之前这样做: 1去https://www.lfd.uci.edu/~gohlke/pythonlibs/ 下载各种版本编译好的.whl 2 pi ...

  3. 03-python-装饰器

    装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象.它经常用于有切面需求的场景,比如:插入日志.性能测试.事务处理.缓存.权 ...

  4. seo中的竞价排名是什么

    seo中的竞价排名是什么 一.总结 一句话总结:竞价排名的基本特点是按点击付费,推广信息出现在搜索结果中(一般是靠前的位置),如果没有被用户点击,则不收取推广费. 搜索引擎的一种推广广告的方式 1.竞 ...

  5. win10 java环境变量

    https://jingyan.baidu.com/article/fd8044fa2c22f15031137a2a.html

  6. R语言中知识点总结(一)

    source("http://bioconductor.org/biocLite.R") biocLite("GEOquery") library(Biobas ...

  7. tchart...

    using System;using System.Collections;using System.ComponentModel;using System.Drawing; using System ...

  8. C#异步的世界(重点:新异步)

    http://www.cnblogs.com/zhaopei/p/async_two.html

  9. canvas学习之树叶动画

    项目地址:http://pan.baidu.com/s/1geJgqen 今天用canvas做了一个树叶发芽到凋落的动画,当然还有很多不完善的地方,不过也让我体会到了,做动画技术占2分,算法占8分.这 ...

  10. Bitmap Byte[] 互转

    严正声明:作者:psklf出处: http://www.cnblogs.com/psklf/p/5889978.html欢迎转载,但未经作者同意,必须保留此段声明:必须在文章中给出原文连接:否则必究法 ...