CodeForces - 762E:Radio stations (CDQ分治||排序二分)
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 xi, ri 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
3 2
1 3 10
3 2 5
4 10 8
1
3 3
1 3 10
3 2 5
4 10 8
2
5 1
1 3 2
2 2 4
3 2 1
4 2 1
5 3 3
2
5 1
1 5 2
2 5 4
3 5 1
4 5 1
5 5 3
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分治||排序二分)的更多相关文章
- Codeforces 526F Pudding Monsters - CDQ分治 - 桶排序
In this problem you will meet the simplified model of game Pudding Monsters. An important process in ...
- 算法笔记--CDQ分治 && 整体二分
参考:https://www.luogu.org/blog/Owencodeisking/post-xue-xi-bi-ji-cdq-fen-zhi-hu-zheng-ti-er-fen 前置技能:树 ...
- Codeforces 1045G AI robots [CDQ分治]
洛谷 Codeforces 简单的CDQ分治题. 由于对话要求互相看见,无法简单地用树套树切掉,考虑CDQ分治. 按视野从大到小排序,这样只要右边能看见左边就可以保证互相看见. 发现\(K\)固定,那 ...
- 一篇自己都看不懂的CDQ分治&整体二分学习笔记
作为一个永不咕咕咕的博主,我来更笔记辣qaq CDQ分治 CDQ分治的思想还是比较简单的.它的基本流程是: \(1.\)将所有修改操作和查询操作按照时间顺序并在一起,形成一段序列.显然,会影响查询操作 ...
- Cdq分治整体二分学习记录
这点东西前前后后拖了好几个星期才学会……还是自己太菜啊. Cdq分治的思想是:把问题序列分割成左右两个,先单独处理左边,再处理左边对右边的影响,再单独处理右边.这样可以消去数据结构上的一个log,降低 ...
- [学习笔记] CDQ分治&整体二分
突然诈尸.png 这两个东西好像都是离线骗分大法... 不过其实这两个东西并不是一样的... 虽然代码长得比较像 CDQ分治 基本思想 其实CDQ分治的基本思想挺简单的... 大概思路就是长这样的: ...
- CDQ分治&整体二分学习个人小结
目录 小结 CDQ分治 二维LIS 第一道裸题 bzoj1176 Mokia bzoj3262 陌上花开 bzoj 1790 矩形藏宝地 hdu5126四维偏序 P3157 [CQOI2011]动态逆 ...
- Codeforces 848C Goodbye Souvenir [CDQ分治,二维数点]
洛谷 Codeforces 这题我写了四种做法-- 思路 不管做法怎样,思路都是一样的. 好吧,其实不一样,有细微的差别. 第一种 考虑位置\(x\)对区间\([l,r]\)有\(\pm x\)的贡献 ...
- Codeforces 1215F. Radio Stations
传送门 题目看一半:"woc 裸的 $2-sat$ 白给??" 看完以后:"...???" 如果没有 $f$ 的限制,那就是个白给的 $2-sat$ 问题,但是 ...
随机推荐
- Android:MVC模式(上)
很多Android的入门书籍,在前面介绍完布局后就会逐个介绍组件,然后开始编写组件使用的例子.每每到此时小伙伴们都可能会有些疑问:是否应该先啃完一本<Java编程思想>学点 Java 知识 ...
- 3D投影
3D投影方式的几大种类: 1.快门式 主动快门式即时分式,不过我们通常用前面的叫法,快门式3D眼镜(3D Shutter Glasses,也称作LC shutter glassesor active ...
- 在c++11中你最惊讶的新feature是什么?
对我来说,我最惊讶竟然把对于多线程的支持加到标准中了.真的想不明确,对于c++这样一种语言.怎么会加进这个东西. 1. 由于各个平台的不同,对于多线程的支持会有很多平台独有的特色.这样c++标准的定义 ...
- vue2.0 + vux (六)NewsList 资讯页 及 NewsDetail 资讯详情页
设置代理,避免出现跨域问题 /*设置代理,避免出现跨域问题*/ proxyTable: { '/api':{ target: 'https://www.oschina.net/action/apiv2 ...
- WMS8_条码界面操作简要说明(包装作业)
说明:条码界面的主要用途是包装作业 这个客户端,完全是JS实现的 可以从 All operation看板视图 Picking的表单视图 All operatio ...
- POJ 2249-Binomial Showdown(排列组合计数)
Binomial Showdown Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18457 Accepted: 563 ...
- [ssh新闻公布系统三]存储新闻
一.存储新闻dao方法 在NewsDao.java中新增存储新闻的saveOrupdate方法 public void saveOrupdate(News news){ getSession().sa ...
- UVA - 11354Bond最小生成树,LCA寻找近期公共祖先
看懂题目意思.他的意思是求将全部的城市走一遍,危急度最小.而且给 你两个s,t后让你求在走的时候,从s到t过程中危急度最大的值,并输出它, 然后就是怎样攻克了,这个题目能够说简单,也能够说难 通过思考 ...
- python datetime获取几分钟、小时、天之前的时间
import datetime print ((datetime.datetime.now()-datetime.timedelta(days=1)).strftime("%Y-%m-%d ...
- cvpr2017-code-etc
cvpr2017: code: http://staffhome.ecm.uwa.edu.au/~00053650/code.html