大意: n个机器人, 位置$x_i$, 可以看到$[x_i-r_i,x_i+r_i]$, 智商$q_i$, 求智商差不超过$k$且能互相看到的机器人对数.

这个题挺好的, 关键是要求互相看到这个条件, 直接求的话是个四维数点问题, 但是可以发现按照$r$排序后, $r$小的能看到的一定能互相看到, 所以就是一个简单的三维数点了.

#include <iostream>
#include <iostream>
#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, b[N];
struct _ {
int type,x,y;
void pr() {
printf("tp=%d,x=%d,y=%d\n",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];
struct __ {
int x,r,q;
} a[N]; ll ans;
int c[N], tim[N], clk;
void add(int x) {
for (; x<=*b; x+=x&-x) tim[x]==clk?++c[x]:c[x]=1,tim[x]=clk;
}
int qry(int x) {
int r = 0;
for (; x; x^=x&-x) tim[x]==clk?r+=c[x]:0;
return r;
}
void merge(int l, int r) {
if (l==r) return;
merge(l,mid),merge(mid+1,r);
int now = l;
++clk;
REP(i,mid+1,r) {
while (now<=mid&&e[now].x<=e[i].x) {
if (e[now].type==0) add(e[now].y);
++now;
}
if (e[i].type==1) ans+=qry(e[i].y);
else if (e[i].type==2) ans-=qry(e[i].y);
}
inplace_merge(e+l,e+mid+1,e+r+1);
}
int id(int x) {
return lower_bound(b+1,b+1+*b,x)-b;
} void add(int x, int y) {
y = id(y);
e[++tot] = {0,x,y};
}
void qry(int x1, int y1, int x2, int y2) {
y1 = id(y1), y2 = id(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};
} int main() {
scanf("%d%d", &n, &k);
REP(i,1,n) {
scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].q);
b[++*b]=a[i].q,b[++*b]=a[i].q+k,b[++*b]=a[i].q-k-1;
}
sort(a+1,a+1+n,[](__ a,__ b){return a.r>b.r;});
sort(b+1,b+1+*b),*b=unique(b+1,b+1+*b)-b-1;
REP(i,1,n) {
qry(a[i].x-a[i].r,a[i].q-k,a[i].x+a[i].r,a[i].q+k);
add(a[i].x,a[i].q);
}
merge(1,tot);
printf("%lld\n", ans);
}

AI robots CodeForces - 1045G (cdq分治)的更多相关文章

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

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

  2. Radio stations CodeForces - 762E (cdq分治)

    大意: 给定$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$降序排, ...

  3. Codeforces 669E cdq分治

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

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

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

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

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

  6. Educational Codeforces Round 41 967 E. Tufurama (CDQ分治 求 二维点数)

    Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama (CDQ分治 求 二维点数) time limit per test 2 ...

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

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

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

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

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

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

随机推荐

  1. 我对于B-树索引的内部结构与索引类型所做的笔记

    图 3-1 B-树索引的内部结构 分支块和页块 B-树索引有两种类型的块: 用于查找的分支块和用于存储值的叶块.B-树索引的上层分支块包含指向下层索引块的索引数据.在图 3-1 中,根分支块包含条目 ...

  2. Java之事务的基本应用

    基本介绍 事务是数据一致性最基本的保证,也就是说一个事务中的操作要么都成功,要么都失败,不允许部分成功.我们常说的事务就是jdbc事务,当然Java中还有其他事务,并且在使用jdbc事务有很多注意点, ...

  3. Centos使用natapp教程

    官网:https://natapp.cn/ 首先在Natapp站注册账号 点击注册 登录后,点击左边 购买隧道,免费/付费均可 根据需要选择隧道协议,这里以web演示,购买隧道 在 https://n ...

  4. C工程 交互 ceph 分布式存储系统

    网上看到有人问,如何在C项目里调用ceph系统对外提供的API,实现分布式存储. 我在网上搜到了相关信息,但是因为不是会员无法追加答案,故而,贴于此. 赠予有缘人:) ———————————————— ...

  5. iPhone IOS10安装APP没提示连接网络(无法联网)的解决办法

    iPhone升级ios10之后,遇到如标题所述问题时: 1.退出APP,设置-蜂窝移动网络-无线局域网助理-开启 2.进入APP,这时候就回提示连接网络了. 提醒: 数据流量有限的朋友,平时请关闭&q ...

  6. 关于.net里面的静态html页面和接口组合使用的网站

    在网站的根目录下,主要有三部分组成.①接口里面的bin文件夹②接口③html里面的页面. html里面有ajax请求接口的js代码.另外接口里面的web.config不需要拷贝到网站根目录去. 如下截 ...

  7. JS函数预解析(小记)

    一,预解析:寻找var  function  形参     a=undefined    fn = function fn(){ alert(); } 二,逐行解读代码:    1.表达式    2. ...

  8. 用 MoveTowards实现多点移动

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class MoveCube ...

  9. poj3133 插头dp

    #include <iostream> #include <cstdio> #include <string.h> #include <vector> ...

  10. samba服务器笔记 (一)

    Samba安装 samba:主服务包:samba-client:客户端:samba-common:通用工具:samba4-libs:库:samba-winbind:windows域映射:samba-w ...