[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 ) 表示运 ...
随机推荐
- Codeforces 233 D - Table
D - Table 思路:dp 首先,第i列的个数肯定和第i - n列个数一样,假设[i - n + 1, i - 1] 之间的个数之和为x,那么第i列和第i-n列的个数应该是n - x 那么我们可以 ...
- 加速cin的技巧
ios::sync_with_stdio(false); cin.tie(0); 把cin变得和scanf一样快.
- audio进度条
如上图所示:为效果图 代码如下: <!doctype html><html> <head> <meta name="author" con ...
- C#特性-表达式树
表达式树ExpressionTree 表达式树基础 转载需注明出处:http://www.cnblogs.com/tianfan/ 刚接触LINQ的人往往觉得表达式树很不容易理解.通过这篇文章我希 ...
- 微信小程序选择视频,视频上传,视频播放
请查看链接地址看具体详情: 选择视频: https://mp.weixin.qq.com/debug/wxadoc/dev/api/media-video.html#wxchoosevideoobje ...
- lanmp环境中php版本的升级为7.1
查看php版本的信息 vim ./lib/phps.sh 设置权限 chmod 755 ./lib/phps.sh 下载版本 ./lib/phps.sh 7.1.4 查看版本 php -v ...
- CF1114E Arithmetic Progression
给定一个打乱的等差数列,每次两种操作. 1.查询一个位置. 2.查询是否有比x大的数字. 一共60次操作. sol: 30次操作即可二分出首项. 剩下30次操作查询出30个位置然后两两做差取gcd即可 ...
- Educational Codeforces Round 48 (Rated for Div. 2)G. Appropriate Team
题意:求满足条件的(i,j)对数:\(gcd(v,a_i)=x,lcm(v,a_j)=y\) 题解:\(x|a_i,a_j|y\),\(x|y\),考虑质因子p,假设a_i中p次数为a,x中次数为b, ...
- 【其他】【Restful】【1】简单了解Restful概念
内容: REST是一种设计风格,不是一种标准,是一种思想.符合REST原则的架构方式即可称为RESTful. 在Restful之前的操作:http://127.0.0.1/user/query/1 G ...
- yarn基本命令
参考文章:https://blog.csdn.net/mjzhang1993/article/details/70092902 1.安装 windows: 下载地址 mac: brew install ...