大意: 给定$n$个三元组$(x,r,f)$, 求所有对$(i,j)$, 满足$i<j, |f_i-f_j|\le k, min(r_i,r_j)\ge |x_i-x_j|$

按$r$降序排, 去掉$min$, 然后就是个裸的三维数点问题, 可以用$CDQ$分治解决.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, k, tot;
struct _ {int x,r,f;} a[N];
struct __ {
int type,x,y;
bool operator < (const __ & rhs) const {
if (x!=rhs.x) return x<rhs.x;
if (y!=rhs.y) return y<rhs.y;
return type<rhs.type;
}
} e[N];
ll ans;
int b[N], c[N], L, R;
void add(int x, int v) {
for (; x<=*b; x+=x&-x) c[x]+=v;
}
int qry(int x) {
int r = 0;
for (; x; x^=x&-x) r+=c[x];
return r;
}
void qry(int x1, int y1, int x2, int y2) {
e[++tot] = {1,x2,y2};
e[++tot] = {1,x1-1,y1-1};
e[++tot] = {2,x1-1,y2};
e[++tot] = {2,x2,y1-1};
b[++*b]=y2,b[++*b]=y1-1;
}
void ins(int x, int y) {
e[++tot] = {0,x,y};
}
void merge(int l, int r) {
if (l==r) return;
merge(l,mid),merge(mid+1,r);
int now = l;
REP(i,mid+1,r) {
while (now<=mid&&e[now].x<=e[i].x) {
if (e[now].type==0) add(e[now].y,1);
++now;
}
if (e[i].type==1) ans+=qry(e[i].y);
else if (e[i].type==2) ans-=qry(e[i].y);
}
while (now!=l) {
if (e[--now].type==0) add(e[now].y,-1);
}
inplace_merge(e+l,e+mid+1,e+r+1);
} int main() {
scanf("%d%d", &n, &k);
REP(i,1,n) scanf("%d%d%d",&a[i].x,&a[i].r,&a[i].f);
sort(a+1,a+1+n,[](_ a,_ b){return a.r>b.r;});
REP(i,1,n) {
qry(a[i].f-k,a[i].x-a[i].r,a[i].f+k,a[i].x+a[i].r);
ins(a[i].f,a[i].x);
}
sort(b+1,b+1+*b),*b=unique(b+1,b+1+*b)-b-1;
REP(i,1,tot) e[i].y=lower_bound(b+1,b+1+*b,e[i].y)-b;
merge(1,tot);
printf("%lld\n", ans);
}

Radio stations CodeForces - 762E (cdq分治)的更多相关文章

  1. 【题解】Radio stations Codeforces 762E CDQ分治

    虽然说好像这题有其他做法,但是在问题转化之后,使用CDQ分治是显而易见的 并且如果CDQ打的熟练的话,码量也不算大,打的也很快,思维难度也很小 没学过CDQ分治的话,可以去看看我的另一篇博客,是CDQ ...

  2. Codeforces 669E cdq分治

    题意:你需要维护一个multiset,支持以下操作: 1:在某个时间点向multiset插入一个数. 2:在某个时间点在multiset中删除一个数. 3:在某个时间点查询multiset的某个数的个 ...

  3. Tufurama CodeForces - 961E (cdq分治)

    题面 One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series " ...

  4. AI robots CodeForces - 1045G (cdq分治)

    大意: n个机器人, 位置$x_i$, 可以看到$[x_i-r_i,x_i+r_i]$, 智商$q_i$, 求智商差不超过$k$且能互相看到的机器人对数. 这个题挺好的, 关键是要求互相看到这个条件, ...

  5. CodeForces - 762E:Radio stations (CDQ分治||排序二分)

    In the lattice points of the coordinate line there are n radio stations, the i-th of which is descri ...

  6. codeforces 762E(cdq分治)

    题意: n个电台,每个电台有三个属性xi, ri, fi.分别代表电台的坐标,电台的播报范围,以及播报的频率. 对于一对电台i, j,若min(ri, rj) >= |xi - xj|,那么他们 ...

  7. Codeforces 1093E Intersection of Permutations [CDQ分治]

    洛谷 Codeforces 思路 一开始想到莫队+bitset,发现要T. 再想到分块+bitset,脑子一抽竟然直接开始写了,当然也T了. 最后发现这就是个裸的CDQ分治-- 发现\(a\)不变,可 ...

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

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

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

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

随机推荐

  1. 给string定义一个扩展方法

    创建一个 static 的类,并且里面的方法也必须是static的,第一个参数是被扩展的对象,必须标注为this,使用时,必须保证namespace using进来了. 实例: using Syste ...

  2. webpack搭建多页面系统(一):对webpack 构建工具的理解

    为什么使用webpack构建工具? 1.开发效率方面: 在一般的开发过程中,分发好任务后,每个人完成自己单独的页面,如果有的人开发完成之后,接手别人的任务,就有可能造成开发时候的冲突. 如果利用模块化 ...

  3. C++入门经典-例6.21-比较string字符串,比较两个字符串

    1:使用“>”.“!=”.“>=”等比较运算符可以比较两个字符串的内容.比较的方法是将两个string字符串从头开始比较每一个字符,直到出现两者不一致.比较这两个不相同的字符的字面值,得出 ...

  4. UEFI和GPT

    好就没用linux了,这几天在win8笔记本上用虚拟机装了下,也准备装到硬盘上和win8双系统使用,发现一些概念已经跟不上时代了. 一个是在虚拟机中装的时候,分配了虚拟硬盘分区时,提示选择分区表类型, ...

  5. Ubuntu16.04安装nginx(并启用SSL)

    一.安装环境介绍 需要预先安装gcc,通常ubuntu默认自带,所以默认已经有这个环境了,后续步骤默认是使用root账户进行的 二.下载及安装nginx相关组件 1.进入任意目录,我选用的是通常选用的 ...

  6. Xcode使用介绍之一:Xcode简介+创建App应用

    一.Xcode简介 Xcode是苹果官方的集成开发环境IDE,开发者可以使用其开发基于iPhone,iPad,Apple Watch以及Mac平台的应用.Xcode具有如下特点: 一站式开发工具.所有 ...

  7. 学习笔记01:《开放平台产品成长之路-POP》

    当看到一些比较好的书籍和视频内容时,希望自己可以总价归纳下来,一方面是好的东西希望可以分享给大家,另一方面希望自己能很好的吸收并转化成掌握的知识,所以有了这个系列的学习笔记,共勉,今天的你比昨天更博学 ...

  8. 安装 redis manager

    安装文档在redis manager 的官方文档上有,这里做个笔记.官网地址:https://snapcraft.io/redis-desktop-manager 截图1: 可以选择自己的系统,根据里 ...

  9. 将json文件转化为可视化

    利用python转化为可视化: python -mjson.tool

  10. springboot mybatis 下使用注解组织查询语句(有查询条件传入)

    @Select("<script>" + "select cab.brandpre_id,cab.brandpre_num_app,cab.id,cab.br ...