CF1045G AI robots(动态开点线段树)
题意
火星上有$N$个机器人排成一行,第$i$个机器人的位置为$x_{i}$,视野为$r_{i}$,智商为$q_{i}$。我们认为第$i$个机器人可以看到的位置是$[x_{i}-r_{i},x_{i}+r_{i}]$。如果一对机器人相互可以看到,且它们的智商$q_{i}$的差距不大于$K$,那么它们会开始聊天。 为了防止它们吵起来,请计算有多少对机器人可能会聊天。
题解
先膜一下大佬->这里
我们先按视野降序排序,这样一个一个考虑,如果后面的能看到前面,那前面的也肯定能看到后面
这样,就是对于每一个机器人,在他前面有几个智商在$[q-k,q+k]$,位置在$[x-r,x+r]$
那么把这个东西看做一个矩形覆盖就可以了
然后因为智商这一维维数很小,我们可以对每一个智商开一个动态开点线段树,然后一个一个扫过去统计答案就可以了
//minamoto
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#define ll long long
using namespace std;
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[<<],*p1=buf,*p2=buf;
inline int read(){
#define num ch-'0'
char ch;bool flag=;int res;
while(!isdigit(ch=getc()))
(ch=='-')&&(flag=true);
for(res=num;isdigit(ch=getc());res=res*+num);
(flag)&&(res=-res);
#undef num
return res;
}
const int N=1e5+;
int n,k,m,b[N*];ll res;
map<int,int> rt;int L[N<<],R[N<<],sum[N<<],cnt=;
void update(int p,int l,int r,int x){
++sum[p];if(l==r) return;
int mid=(l+r)>>;
if(x<=mid) update(L[p]=L[p]?L[p]:++cnt,l,mid,x);
else update(R[p]=R[p]?R[p]:++cnt,mid+,r,x);
}
int query(int p,int l,int r,int ql,int qr){
if(ql<=l&&qr>=r||p==) return sum[p];
int mid=(l+r)>>,res=;
if(ql<=mid) res+=query(L[p],l,mid,ql,qr);
if(qr>mid) res+=query(R[p],mid+,r,ql,qr);
return res;
}
inline void ins(int q,int x){
if(rt.count(q)==) rt[q]=++cnt;
update(rt[q],,m,x);
}
inline int get(int q,int ql,int qr){
if(rt.count(q)==) return ;
return query(rt[q],,m,ql,qr);
}
struct node{
int x,r,q;
node(){}
node(int x,int r,int q):x(x),r(r),q(q){}
inline bool operator <(const node &b)const
{return r>b.r;}
}a[N];
int main(){
// freopen("testdata.in","r",stdin);
n=read(),k=read();
for(int i=;i<=n;++i){
int x=read(),r=read(),q=read();
a[i]=node(x,r,q);
b[++m]=x,b[++m]=x-r,b[++m]=x+r;
}
sort(b+,b++m),m=unique(b+,b++m)-b-;
sort(a+,a++n);
for(int i=;i<=n;++i){
int l=lower_bound(b+,b++m,a[i].x-a[i].r)-b;
int r=lower_bound(b+,b++m,a[i].x+a[i].r)-b;
int x=lower_bound(b+,b++m,a[i].x)-b;
for(int j=a[i].q-k;j<=a[i].q+k;++j)
res+=get(j,l,r);
ins(a[i].q,x);
}
printf("%lld\n",res);
return ;
}
CF1045G AI robots(动态开点线段树)的更多相关文章
- [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)
题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...
- [bzoj 3531][SDOI2014]旅行(树链剖分+动态开点线段树)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3531 分析: 对于每个颜色(颜色<=10^5)都建立一颗线段树 什么!那么不是M ...
- 【BZOJ-4636】蒟蒻的数列 动态开点线段树 ||(离散化) + 标记永久化
4636: 蒟蒻的数列 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 247 Solved: 113[Submit][Status][Discuss ...
- codeforces 893F - Physical Education Lessons 动态开点线段树合并
https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...
- codeforces 915E - Physical Education Lessons 动态开点线段树
题意: 最大$10^9$的区间, $3*10^5$次区间修改,每次操作后求整个区间的和 题解: 裸的动态开点线段树,计算清楚数据范围是关键... 经过尝试 $2*10^7$会$MLE$ $10^7$会 ...
- CF915E Physical Education Lessons 动态开点线段树
题目链接 CF915E Physical Education Lessons 题解 动态开点线段树 代码 /* 动态开点线段树 */ #include<cstdio> #include&l ...
- 洛谷P3313 [SDOI2014]旅行(树链剖分 动态开节点线段树)
题意 题目链接 Sol 树链剖分板子 + 动态开节点线段树板子 #include<bits/stdc++.h> #define Pair pair<int, int> #def ...
- NOIP2017 列队——动态开点线段树
Description: Sylvia 是一个热爱学习的女♂孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有n×m名学生,方阵的行数为 ...
- 洛谷P3120 [USACO15FEB]牛跳房子(动态开节点线段树)
题意 题目链接 Sol \(f[i][j]\)表示前\(i\)行\(j\)列的贡献,转移的时候枚举从哪里转移而来,复杂度\(O(n^4)\) 然后考虑每一行的贡献,动态开节点线段树维护一下每种颜色的答 ...
随机推荐
- stl_内存基本处理工具
内存基本处理工具 STL定义5个全局函数.作用于初始化空间上.各自是:用于构造的construct(),用于析构的destroy(),uninitialized_copy(),uninitialize ...
- fetch 函数分装
1.fetch /** * 封装 fetch */ import { hashHistory } from 'react-router'; export default function reques ...
- 批量修改文件权限 和所有者 chown nobody:nobody * -R chmod 775 * -R
chown nobody:nobody * -R chmod 775 * -R
- Intel processor brand names-Xeon,Core,Pentium,Celeron----Quark
http://en.wikipedia.org/wiki/Intel_Quark Intel Quark From Wikipedia, the free encyclopedia Intel ...
- SQLDMO注冊
在维护人事系统时.师姐给我们提出一个功能上有问题. 备份数据库时.报黄页.然后须要我们寻找原因,作出解决方式. 一開始我从原先在本机上公布的系统入手,发现没有出现故障.可是.当对程序进行调试时,就出现 ...
- Struts2+Spring+Hibernate step by step 04 整合Spring之二,从数据库验证username和password
注:本系列文章部分内容来自王健老师编写ssh整合开发教程 使用Spring的AOP进行项目的事务管理,已经成为非常多企业的首先,Spring做为优秀的开源项目,其在数据库连接.事务管理方面的优势已经显 ...
- centos7+php7 swoole 安装
下载 swoole 首先下载swoole的源码包,这个操作很简单,没有太多说的. wget -c https://github.com/swoole/swoole-src/archive/v2.0.6 ...
- VUE 之 JS指令
1.v-text的用法: 2.v-html 3.v-for 4.v-if , v-else if ,v-else v-if 每次生成都只有一个标签,即符合条件的标签. 5.v-show v-show ...
- 安装NLTK
在网上找了一圈,没找到几个靠谱的安装流程,在http://nltk.org/install.html上找到各平台下安装流程: Windows平台: 以下操作假定你的机器上还没有安装Python,如果你 ...
- java上下文Context类
Context在Java中的出现是如此频繁,但其中文翻译"上下文"又是如此诡异拗口,因此导致很多人不是很了解Context的具体含义是指什么,所以很有必要来深究一下这词的含义. 先 ...