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)\) 然后考虑每一行的贡献,动态开节点线段树维护一下每种颜色的答 ...
随机推荐
- invlpg 指令简单介绍
invlpg 指令简单介绍 void tlb_invalidate(pde_t *pgdir, void *va) { // Flush the entry only if we're modifyi ...
- opencms 安装出现以下的问题:Your 'max_allowed_packet' variable is set to less than 16777216 Byte (16MB).
一.问题 在安装opencms是会出现例如以下错误: MySQL system variable 'max_allowed_packet' is set to 1048576 Byte (1MB). ...
- java开始到熟悉62
(说明:昨天网络出现了问题导致昨天的没有按时上传,这篇算是昨天的,今天晚上照常上传今天的内容) 本次主题:数组拷贝.排序.二分法 1.数组拷贝 a.java.lang中System 类包含一些有用的类 ...
- 互斥锁和条件变量(pthread)相关函数
互斥锁 #include <pthread.h> // 若成功返回0,出错返回正的Exxx值 // mptr通常被初始化为PTHREAD_MUTEX_INITIALIZER int pth ...
- 翻译:A Tutorial on the Device Tree (Zynq) -- Part V
A Tutorial on the Device Tree (Zynq) -- Part V Application-specific data 之前提过,设备树中是一些特殊信息,这样一个驱动可以管理 ...
- 转载:用python爬虫抓站的一些技巧总结
原文链接:http://www.pythonclub.org/python-network-application/observer-spider 原文的名称虽然用了<用python爬虫抓站的一 ...
- Effective C++ 条款42
本节条款我们讨论一下class 关键字和typename关键字的不同以及对于模板函数(template function)的影响. 例如以下代码: template<class T> T ...
- JUNO eclipse Version: 4.2.0 添加svn插件
1.下载最新的这个版本的SVN http://www.eclipse.org/subversive/latest-releases.php 实际的下载地址 http://www.eclipse.org ...
- SWT.Shell
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class SWT_Shell ...
- 关于div li 等标签之间自带间距
可以用float来清除标签之间的间距. ps :ul使用font-size:0 唯一的缺点就是要再次设置LI的font-size