洛谷

Codeforces


简单的CDQ分治题。

由于对话要求互相看见,无法简单地用树套树切掉,考虑CDQ分治。

按视野从大到小排序,这样只要右边能看见左边就可以保证互相看见。

发现\(K\)固定,那么左右按智商排序、位置离散化之后可以\(two\;pointers\)一下,套个树状数组,就做完了。

由于复杂度瓶颈在树状数组,没必要归并,可以直接\(sort\)。

复杂度应该是\(O(n\log^2 n)\)。


#include<bits/stdc++.h>
namespace my_std{
using namespace std;
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,x,y) for (int i=(x);i>=(y);i--)
#define go(x) for (int i=head[x];i;i=edge[i].nxt)
#define sz 301001
typedef long long ll;
template<typename T>
inline void read(T& t)
{
t=0;char f=0,ch=getchar();
double d=0.1;
while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
if(ch=='.')
{
ch=getchar();
while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();
}
t=(f?-t:t);
}
template<typename T,typename... Args>
inline void read(T& t,Args&... args){read(t); read(args...);}
void file()
{
#ifndef ONLINE_JUDGE
freopen("a.txt","r",stdin);
#endif
}
// inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std; int n,K;
ll ans;
int A[sz];
struct hh{int p,l,r,q,len;}a[sz];
inline bool cmp(const hh &x,const hh &y){return x.len>y.len;}
inline bool cmp2(const hh &x,const hh &y){return x.q<y.q;} int sum[sz];
void add(int x,int v){while (x<=n) sum[x]+=v,x+=(x&(-x));}
int query(int x){int ret=0;while (x) ret+=sum[x],x-=(x&(-x));return ret;}
int query(int l,int r){return query(r)-query(l-1);} void solve(int l,int r)
{
if (l==r) return;
int mid=(l+r)>>1;
solve(l,mid);solve(mid+1,r);
int L=l,R=l-1;
rep(i,mid+1,r)
{
while (L<=mid&&a[i].q-a[L].q>K) add(a[L].p,-1),++L;
while (R<mid&&a[R+1].q-a[i].q<=K) ++R,add(a[R].p,1);
ans+=query(a[i].l,a[i].r);
}
rep(i,L,R) add(a[i].p,-1);
sort(a+l,a+r+1,cmp2);
} int main()
{
file();
read(n,K);
int c;
rep(i,1,n) read(a[i].p,a[i].len,a[i].q),A[i]=a[i].p;
sort(A+1,A+n+1);c=unique(A+1,A+n+1)-A-1;
rep(i,1,n)
a[i].l=lower_bound(A+1,A+c+1,a[i].p-a[i].len)-A,
a[i].r=upper_bound(A+1,A+c+1,a[i].p+a[i].len)-A-1,
a[i].p=lower_bound(A+1,A+c+1,a[i].p)-A;
sort(a+1,a+n+1,cmp);
solve(1,n);
cout<<ans;
return 0;
}

Codeforces 1045G AI robots [CDQ分治]的更多相关文章

  1. CF1045G:AI robots(CDQ分治)

    Description 火星上有$n$个机器人排成一行,第$i$个机器人的位置为$x_i$,视野为$r_i​$,智商为$q_i​$.我们认为第$i$个机器人可以看到的位置是$[x_i−r_i,x_i+ ...

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

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

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

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

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

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

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

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

  6. Codeforces 848C (cdq分治)

    Codeforces 848C Goodbye Souvenir Problem : 给一个长度为n的序列,有q个询问.一种询问是修改某个位置的数,另一种询问是询问一段区间,对于每一种值出现的最右端点 ...

  7. Codeforces 848C Goodbye Souvenir(CDQ 分治)

    题面传送门 考虑记录每个点的前驱 \(pre_x\),显然答案为 \(\sum\limits_{i=l}^{r} i-pre_i (pre_i \geq l)\) 我们建立一个平面直角坐标系,\(x\ ...

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

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

  9. Codeforces 1093E Intersection of Permutations (CDQ分治+树状数组)

    题意:给你两个数组a和b,a,b都是一个n的全排列:有两种操作:一种是询问区间在数组a的区间[l1,r1]和数组b的区间[l2,r2]出现了多少相同的数字,另一种是交换数组b中x位置和y位置的数字. ...

随机推荐

  1. extjs.net list 点击弹出修改页面及初始化

    <SaveMask ShowMask="true" /> <LoadMask ShowMask="true" /> <Listen ...

  2. 【python小练】0001

    第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? # coding ...

  3. oracle 任务备份

    @echo offset hour=%time:~,2%if "%time:~,1%"==" " set hour=0%time:~1,1%set temp=% ...

  4. Linux 查看系统版本和内核

    查看系统内核版本 [root@11e71db4a00e /]# cat /proc/version Linux version -.el7.x86_64 (builder@kbuilder.dev.c ...

  5. Vue中splice的使用

    转载:https://blog.csdn.net/xiha_zhu/article/details/80449339 splice(index,len,[item])它也可以用来替换/删除/添加数组内 ...

  6. redis踩坑记录

    1. 关于redis启动后的warnning: WARNING you have Transparent Huge Pages (THP) support enabled in your kernel ...

  7. TensorFlow从入门到理解(五):你的第一个循环神经网络RNN(回归例子)

    运行代码: import tensorflow as tf import numpy as np import matplotlib.pyplot as plt BATCH_START = 0 TIM ...

  8. spring cloud(学习笔记)高可用注册中心(Eureka)的实现(二)

    绪论 前几天我用一种方式实现了spring cloud的高可用,达到两个注册中心,详情见spring cloud(学习笔记)高可用注册中心(Eureka)的实现(一),今天我意外发现,注册中心可以无限 ...

  9. vue运行说明

    1.cd 到demo里面 如:cd vuedemo(项目名) 2.安装依赖: npm install 3.运行项目 npm run dev

  10. 【python小练习】简单的猜数字游戏

    简单的猜数字游戏 前两天在论坛回答问题时候,看到一个猜数字的游戏,就在原来的基础上改了一下,玩一玩. 此程序,数字范围和尝试次数是事先设定好的,当然可以通过代码修改.经过测试,由于难度过大,我在其中加 ...