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. DAY5 基本数据类型及内置方法

    一.可变与不可变数据类型 1.可变类型:值改变,但是id不变,证明就是在改变原值,是可变类型 2.不可变类型:值改变,但是id也跟着变,证明是产生了新的值,是不可变类型 二.数字类型 1.整型int ...

  2. Qt5模型/视图结构-视图(View)

    实现自定义的View,可继承自QAbstractItemView类,对所需的纯虚函数进行重定义与实现,对于QAbstractItemView类中的纯虚函数,在子类中必须进行重定义,但不一定要实现,可根 ...

  3. Python的特殊成员

    Python 用下划线作为变量前缀和后缀指定特殊变量 _xxx 不能用’from module import *’导入 __xxx__ 系统定义名字 __xxx 类中的私有变量名 核心风格:避免用下划 ...

  4. LeetCode--415--字符串相加

    问题描述: 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num1 和nu ...

  5. 使用Vuex来处理Authentication token

    https://www.cnblogs.com/chentianwei/p/10156459.html 之前博客:建立了一个app.使用loacal storage 来管理tokens(通过clien ...

  6. 通过.frm和.ibd恢复mysql数据

    .frm文件:保存了每个表的元数据,包括表结构的定义等: .ibd文件:InnoDB引擎开启了独立表空间(my.ini中配置innodb_file_per_table = 1)产生的存放该表的数据和索 ...

  7. laravel中连表查询

    $skuList = ProductSkuModel::selectRaw('product_skus.id as sku_id, product_skus.code as code, product ...

  8. react中直接调用子组件的方法(非props方式)

    我们都知道在 react中,若要在父组件调用子组件的方法,通常我们会采用在父组件定义一个方法,作为props转给子组件,然后执行该方法,可以获取到子组件传回的参数以得到我们的目的. 显而易见,这个执行 ...

  9. js异常处理

    <script> function myFunction() { try { var x=document.getElementById("demo").value; ...

  10. Leetcode 150

    class Solution { public: int evalRPN(vector<string>& tokens) { stack<int> st; ;i < ...