In the lattice points of the coordinate line there are n radio stations, the i-th of which is described by three integers:

  • xi — the coordinate of the i-th station on the line,
  • ri — the broadcasting range of the i-th station,
  • fi — the broadcasting frequency of the i-th station.

We will say that two radio stations with numbers i and j reach each other, if the broadcasting range of each of them is more or equal to the distance between them. In other words min(ri, rj) ≥ |xi - xj|.

Let's call a pair of radio stations (i, j) bad if i < j, stations i and j reach each other and they are close in frequency, that is, |fi - fj| ≤ k.

Find the number of bad pairs of radio stations.

Input

The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 10) — the number of radio stations and the maximum difference in the frequencies for the pair of stations that reach each other to be considered bad.

In the next n lines follow the descriptions of radio stations. Each line contains three integers xiri and fi (1 ≤ xi, ri ≤ 109, 1 ≤ fi ≤ 104) — the coordinate of the i-th radio station, it's broadcasting range and it's broadcasting frequency. No two radio stations will share a coordinate.

Output

Output the number of bad pairs of radio stations.

Examples

Input
3 2
1 3 10
3 2 5
4 10 8
Output
1
Input
3 3
1 3 10
3 2 5
4 10 8
Output
2
Input
5 1
1 3 2
2 2 4
3 2 1
4 2 1
5 3 3
Output
2
Input
5 1
1 5 2
2 5 4
3 5 1
4 5 1
5 5 3
Output
5

https://www.cnblogs.com/mlystdcall/p/6351344.html

【转】:

首先整理一下条件:

每个点有三个属性,x,r,f

统计有多少对点i,j满足 min(ri,rj) >= |xi-xj| 且 |fi-fj| <= k,这样的点对被称作是“坏的”

对r值取min是个烦人的条件,于是我们把点按照r值从大到小排序,按照r值从大到小的顺序依次考虑每个点

这样对于每个点,我们只考虑它之前出现的点,也就是r值比他大的点,和他能不能组成“坏的”点对

这样的话,因为一个点i之前所有的点j的r值都比他大,所以 min(ri,rj) = ri

然后我们重新看一下问题:

按照指定的顺序依次加入点,每次加入一个点i,考虑它之前加入的所有点j,有多少个点满足 |xi-xj| <= ri 且 |fi-fj| <= k

再转化一下:

对于每个点i,考虑有多少个它之前的点j满足 xi-ri <= xj <= xi+ri 且 fi-k <= fj <= fi+k

我们把x和f这两个属性看做二维平面中的横纵坐标,问题就变成了:

向一个平面中添加一个点,查询指定矩形内点的个数

这是一个经典的三维偏序问题,可以用 线段树套线段树 或者 CDQ分治 来做

---------------------------------------------分界线--------

自己:

注意到K比较小,我们也可以用二分暴力求矩形内部有多少个点。

(本来像练习CDQ分治的,但是太懒了,所以暴力了。

而且学到了维护有序的vector的方法,也不错的。

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
vector<int>G[];
struct in{ int x,r,f; }s[maxn];
bool cmp(in w,in v){ return w.r>v.r; }
long long ans;
int main()
{
int N,K,i,j;
scanf("%d%d",&N,&K);
for(i=;i<=N;i++) scanf("%d%d%d",&s[i].x,&s[i].r,&s[i].f);
sort(s+,s+N+,cmp);
for(i=;i<=N;i++){
for(j=max(,s[i].f-K);j<=s[i].f+K;j++)
ans+=upper_bound(G[j].begin(),G[j].end(),s[i].x+s[i].r)-lower_bound(G[j].begin(),G[j].end(),s[i].x-s[i].r);
int pos=upper_bound(G[s[i].f].begin(),G[s[i].f].end(),s[i].x)-G[s[i].f].begin();
G[s[i].f].insert(G[s[i].f].begin()+pos,s[i].x);
}
printf("%I64d\n",ans);
return ;
}

CodeForces - 762E:Radio stations (CDQ分治||排序二分)的更多相关文章

  1. Codeforces 526F Pudding Monsters - CDQ分治 - 桶排序

    In this problem you will meet the simplified model of game Pudding Monsters. An important process in ...

  2. 算法笔记--CDQ分治 && 整体二分

    参考:https://www.luogu.org/blog/Owencodeisking/post-xue-xi-bi-ji-cdq-fen-zhi-hu-zheng-ti-er-fen 前置技能:树 ...

  3. Codeforces 1045G AI robots [CDQ分治]

    洛谷 Codeforces 简单的CDQ分治题. 由于对话要求互相看见,无法简单地用树套树切掉,考虑CDQ分治. 按视野从大到小排序,这样只要右边能看见左边就可以保证互相看见. 发现\(K\)固定,那 ...

  4. 一篇自己都看不懂的CDQ分治&整体二分学习笔记

    作为一个永不咕咕咕的博主,我来更笔记辣qaq CDQ分治 CDQ分治的思想还是比较简单的.它的基本流程是: \(1.\)将所有修改操作和查询操作按照时间顺序并在一起,形成一段序列.显然,会影响查询操作 ...

  5. Cdq分治整体二分学习记录

    这点东西前前后后拖了好几个星期才学会……还是自己太菜啊. Cdq分治的思想是:把问题序列分割成左右两个,先单独处理左边,再处理左边对右边的影响,再单独处理右边.这样可以消去数据结构上的一个log,降低 ...

  6. [学习笔记] CDQ分治&整体二分

    突然诈尸.png 这两个东西好像都是离线骗分大法... 不过其实这两个东西并不是一样的... 虽然代码长得比较像 CDQ分治 基本思想 其实CDQ分治的基本思想挺简单的... 大概思路就是长这样的: ...

  7. CDQ分治&整体二分学习个人小结

    目录 小结 CDQ分治 二维LIS 第一道裸题 bzoj1176 Mokia bzoj3262 陌上花开 bzoj 1790 矩形藏宝地 hdu5126四维偏序 P3157 [CQOI2011]动态逆 ...

  8. Codeforces 848C Goodbye Souvenir [CDQ分治,二维数点]

    洛谷 Codeforces 这题我写了四种做法-- 思路 不管做法怎样,思路都是一样的. 好吧,其实不一样,有细微的差别. 第一种 考虑位置\(x\)对区间\([l,r]\)有\(\pm x\)的贡献 ...

  9. Codeforces 1215F. Radio Stations

    传送门 题目看一半:"woc 裸的 $2-sat$ 白给??" 看完以后:"...???" 如果没有 $f$ 的限制,那就是个白给的 $2-sat$ 问题,但是 ...

随机推荐

  1. Android --修改arr文件

    1. 改为zip文件 2. 修改 3. 改后缀

  2. 【转载】Linux下套接字学习

    感觉这个系列还不错,学习一下. 先看的是第三篇: http://blog.csdn.net/gatieme/article/details/46334337 < Linux下套接字详解(三)-- ...

  3. LeetCode – Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  4. POJ1830开关问题——gauss消元

    题目链接 分析: 第一个高斯消元题目,操作是异或.奇偶能够用0.1来表示,也就表示成bool类型的方程,操作是异或.和加法没有差别 题目中有两个未知量:每一个开关被按下的次数(0.1).每一个开关的转 ...

  5. Ubuntu16.04下屏幕侧边栏的设置

    ubnutu的任务栏都是在左侧: zhang@zhang-virtual-machine:~$ gsettings set com.canonical.Unity.Launcher launcher- ...

  6. Ionic + AngularJS angular-translate 国际化本地化解决方案

    欢迎访问我们的网站,网站上有更多关于技术性的交流:http://www.ncloud.hk/技术分享/ionic-plus-angularjs-angular-translate-国际化本地化解决方案 ...

  7. 如何学习Java?

    一点感悟 java作为一门编程语言,在各类编程语言中作为弄潮儿始终排在前三的位置,这充分肯定了java语言的魅力,在实际项目应用中,我们已经无法脱离javaa(Ps当然你可以选择不使用),但它的高性能 ...

  8. MySQL 存储过程 (3)

    以下介绍下像数据库循环插入数据操作 第一步:建立存储过程用到的信息表

  9. 51 NOD 1753 相似子串 字符串hash

      1735  相似子串  基准时间限制:5 秒 空间限制:131072 KB 分值: 80   两个字符串相似定义为:1.两个字符串长度相等2.两个字符串对应位置上有且仅有至多一个位置所对应的字符不 ...

  10. 九度OJ 1109:连通图 (最小生成树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2783 解决:1432 题目描述: 给定一个无向图和其中的所有边,判断这个图是否所有顶点都是连通的. 输入: 每组数据的第一行是两个整数 n ...