Codeforces 1045G AI robots [CDQ分治]
简单的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分治]的更多相关文章
- CF1045G:AI robots(CDQ分治)
Description 火星上有$n$个机器人排成一行,第$i$个机器人的位置为$x_i$,视野为$r_i$,智商为$q_i$.我们认为第$i$个机器人可以看到的位置是$[x_i−r_i,x_i+ ...
- Codeforces 848C Goodbye Souvenir [CDQ分治,二维数点]
洛谷 Codeforces 这题我写了四种做法-- 思路 不管做法怎样,思路都是一样的. 好吧,其实不一样,有细微的差别. 第一种 考虑位置\(x\)对区间\([l,r]\)有\(\pm x\)的贡献 ...
- Codeforces 526F Pudding Monsters - CDQ分治 - 桶排序
In this problem you will meet the simplified model of game Pudding Monsters. An important process in ...
- Educational Codeforces Round 41 967 E. Tufurama (CDQ分治 求 二维点数)
Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama (CDQ分治 求 二维点数) time limit per test 2 ...
- Codeforces 1093E Intersection of Permutations [CDQ分治]
洛谷 Codeforces 思路 一开始想到莫队+bitset,发现要T. 再想到分块+bitset,脑子一抽竟然直接开始写了,当然也T了. 最后发现这就是个裸的CDQ分治-- 发现\(a\)不变,可 ...
- Codeforces 848C (cdq分治)
Codeforces 848C Goodbye Souvenir Problem : 给一个长度为n的序列,有q个询问.一种询问是修改某个位置的数,另一种询问是询问一段区间,对于每一种值出现的最右端点 ...
- Codeforces 848C Goodbye Souvenir(CDQ 分治)
题面传送门 考虑记录每个点的前驱 \(pre_x\),显然答案为 \(\sum\limits_{i=l}^{r} i-pre_i (pre_i \geq l)\) 我们建立一个平面直角坐标系,\(x\ ...
- 【题解】Radio stations Codeforces 762E CDQ分治
虽然说好像这题有其他做法,但是在问题转化之后,使用CDQ分治是显而易见的 并且如果CDQ打的熟练的话,码量也不算大,打的也很快,思维难度也很小 没学过CDQ分治的话,可以去看看我的另一篇博客,是CDQ ...
- Codeforces 1093E Intersection of Permutations (CDQ分治+树状数组)
题意:给你两个数组a和b,a,b都是一个n的全排列:有两种操作:一种是询问区间在数组a的区间[l1,r1]和数组b的区间[l2,r2]出现了多少相同的数字,另一种是交换数组b中x位置和y位置的数字. ...
随机推荐
- java8 新特性 Optional容器类
public class Godness { private String name; public Godness() { } public Godness(String name) { this. ...
- Continuous Design
Continuous Design https://www.martinfowler.com/ieeeSoftware/continuousDesign.pdf T he rising popular ...
- 【bzoj 2527】[Poi2011]Meteors
Description Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby gala ...
- 利用PHP实现登录与注册功能以及使用PHP读取mysql数据库——以表格形式显示数据
登录界面 <body><form action="login1.php" method="post"><div>用户名:&l ...
- APPLE-SA-2019-3-25-2 macOS Mojave 10.14.4,Security Update 2019-002 High Sierra, Security Update 2019-002 Sierra
APPLE-SA-2019-3-25-2 macOS Mojave 10.14.4, Security Update2019-002 High Sierra, Security Update 2019 ...
- vue安装教程总结
转载:https://blog.csdn.net/sunny1660/article/details/78326548 简介: vue.js是一套构建用户界面的渐进式框架.比较简洁,用于解 ...
- PhpStorm+xdebug+postman调试
PhpStorm+xdebug+postman调试 写PHP时,一直用postman做测试,最近发现在测试过程中可以用xdebug来断点调试,比原来手动打exit或者die来断点效率高多了. 下面记录 ...
- 采用shell脚本定时清理Tomcat日志
1 Shell脚本案例 删除超过30天的日志文件 #!/bin/bash log_path=/mnt/software/apache-tomcat-.M22/logs d=`date +%Y-%m-% ...
- angular-file-upload 项目实践踩坑
API文档: https://github.com/nervgh/angular-file-upload/wiki/Module-API 过程中得到昊哥的鼎力帮助,感谢. 需求如下,分别选择多个文件, ...
- MGR架构~高可用架构细节的梳理
一 简介:今天咱们来聊聊mgr的细节原理相关 二 选择新主机制 1 当主节点宕掉,自动会根据服务器的server_uuid变量和group_replication_member_weight变量值 ...