题意
-
种菜和 个人
-
每种菜 有三个属性
-
每个人 有两个属性
-
一个人 能品尝菜 当且仅当 且
-
求每个人能品尝多少种菜
-
,其他数据均为 级别
算法:扫描线 + 树状数组
-
此题有一个非常套路的 做法( CDQ 分治)和一个比较妙的官方题解做法,复杂度少一个 ,下面只讨论后者
-
首先, 就相当于 且
-
把问题抽象到二维平面上,一个点 表示一个人的 为 , 为
-
整理一下得出条件
-
(1)
-
(2)
-
(3)
-
这相当于点 位于如下图所示的三角形内(含边界)
-
考虑扫描线,把所有人离线按照 排序,按照 坐标递增的顺序扫描,当扫到 时加入第 个三角形的影响,扫到 时去除第 个三角形的影响
-
我们对于每个人 需要询问这个点在多少个三角形内
-
考虑差分,也就是说当 时,如果直线 经过了第 个三角形且 坐标范围 被第 个三角形覆盖,则在 处贡献 , 处贡献 ,则当 时,单点询问 转化成询问前缀和
-
于是我们考虑第 个三角形的影响,可以看成
-
(1)在直线 上贡献
-
(2)在直线 上贡献
-
消除第 个三角形的影响则同理
-
询问相当于询问横坐标为 ,纵坐标不超过 的贡献之和
-
用两个数据结构 和 分别记录 和 的贡献
-
直线 能为 贡献当且仅当
-
所以加入第 个三角形时,在数据结构 的 处进行单点加
-
直线 能为询问贡献当且仅当
-
所以加入第 个三角形时,在数据结构 的 处进行单点加
-
这样第 个询问的答案就是当 扫描到 时,数据结构 的前缀和 减去数据结构 的前缀和
-
可以用树状数组等实现
-
复杂度
代码
#include <bits/stdc++.h>
// 20030830
inline int read()
{
int res = 0; bool bo = 0; char c;
while (((c = getchar()) < '0' || c > '9') && c != '-');
if (c == '-') bo = 1; else res = c - 48;
while ((c = getchar()) >= '0' && c <= '9')
res = (res << 3) + (res << 1) + (c - 48);
return bo ? ~res + 1 : res;
}
const int N = 1e5 + 5, M = N << 2;
int n, m, p[N], s[N], b[N], inc[N], pref[N], tot, a[M], A[M], B[M], cnt, ans[N];
struct node
{
int tm, x, y, op, id;
} que[M];
inline bool comp(node a, node b)
{
return a.tm < b.tm || (a.tm == b.tm && a.op < b.op);
}
void changeA(int x, int v)
{
for (; x <= tot; x += x & -x)
A[x] += v;
}
int askA(int x)
{
int res = 0;
for (; x; x -= x & -x)
res += A[x];
return res;
}
void changeB(int x, int v)
{
for (; x <= tot; x += x & -x)
B[x] += v;
}
int askB(int x)
{
int res = 0;
for (; x; x -= x & -x)
res += B[x];
return res;
}
int main()
{
n = read(); m = read();
for (int i = 1; i <= n; i++) p[i] = read();
for (int i = 1; i <= n; i++) s[i] = read();
for (int i = 1; i <= n; i++) b[i] = read();
for (int i = 1; i <= m; i++) inc[i] = read();
for (int i = 1; i <= m; i++) pref[i] = read();
for (int i = 1; i <= n; i++)
a[++tot] = p[i] + b[i], a[++tot] = b[i] - p[i] + 1;
for (int i = 1; i <= m; i++)
a[++tot] = inc[i] + pref[i], a[++tot] = pref[i] - inc[i];
std::sort(a + 1, a + tot + 1);
int tt = std::unique(a + 1, a + tot + 1) - a - 1;
tot = tt;
for (int i = 1; i <= n; i++)
que[++cnt] = (node) {p[i], p[i], b[i], 1, 0},
que[++cnt] = (node) {s[i] + 1, p[i], b[i], -1, 0};
for (int i = 1; i <= m; i++)
que[++cnt] = (node) {inc[i], inc[i], pref[i], 2, i};
std::sort(que + 1, que + cnt + 1, comp);
for (int i = 1; i <= cnt; i++)
if (que[i].op <= 1)
{
changeA(std::lower_bound(a + 1, a + tot + 1,
que[i].x + que[i].y) - a, que[i].op);
changeB(std::lower_bound(a + 1, a + tot + 1,
que[i].y - que[i].x + 1) - a, que[i].op);
}
else ans[que[i].id] = askA(std::lower_bound(a + 1, a + tot + 1,
que[i].x + que[i].y) - a) -
askB(std::lower_bound(a + 1, a + tot + 1,
que[i].y - que[i].x) - a);
for (int i = 1; i <= m; i++) printf("%d ", ans[i]);
return puts(""), 0;
}![(p[i],b[i])(s[i],b[i]+p[i]-s[i])(s[i],b[i]+s[i]-p[i])三点构成的三角形](https://img-blog.csdnimg.cn/20190326213144721.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3h5ejMyNzY4,size_16,color_FFFFFF,t_70)