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)\) 然后考虑每一行的贡献,动态开节点线段树维护一下每种颜色的答 ...
随机推荐
- FlashBuilder找不到所需要的AdobeFlashPlayer调试器版本的解决方案
这个问题就是因为你所装的FlashPlayer不是调试器版本.如果你的FlashPlayer是调试版,那么你随便打开一个有Flash的页面,然后右键点击Flash,就会有一个调试器,菜单,当然它现在是 ...
- 使用session来存储用户的登录信息
对存在cookie端数据进行加密处理,具体代码如下: <?php session_start(); //假设用户登录成功获得了以下用户数据 $userinfo = array( 'uid' =& ...
- Spark 学习笔记:(二)编程指引(Scala版)
参考: http://spark.apache.org/docs/latest/programming-guide.html 后面懒得翻译了,英文记的,以后复习时再翻. 摘要:每个Spark appl ...
- spring MVC (学习笔记)
web.xml 相关配置 <?xml version="1.0" encoding="UTF-8"?><web-app xmlns=" ...
- ECMAScript学习笔记
1. ECMAScript不存在块级作用域,因此在循环内部定义的变量,在循环外也是可以访问的 eg: var count =10; fpr(var i=0; i<count; i++){ ale ...
- Duplicate Observed Data
在翻看<重构-改善既有代码的设计>这本经典的书,书中就介绍了一个重构方法--Duplicate Observed Data 复制被监视数据的重构方法,使用这种方法能够使界面和对数据的操作隔 ...
- iOS webView的常见属性和方法
一.初始化与三种加载方式 UIWebView继承与UIView,因此,其初始化方法和一般的view一样,通过alloc和init进行初始化,其加载数据的方式有三种: 第一种: - (void)load ...
- POJ3268 Silver Cow Party —— 最短路
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- AutoEventWireup
Page_PreInit & OnPreInit - whats the difference? https://forums.asp.net/t/1095903.aspx?Page_PreI ...
- eclipse创建maven项目出现以下报错: org.apache.maven.archiver.MavenArchiver.getManifest (org.apache.maven.project.MavenProject,org.apache.mav en.archiver.MavenArchiveConfiguration)
解决方法: 更新eclipse中的maven插件 Help -> Install New Software -> add -> http://repo1.maven.org/ma ...