CF762E Radio Stations
题目戳这里。
我还以为是KDtree呢,但是KDtree应该也可以做吧。
这是一道数据结构好题。考虑到由于\(K \le 10\),所以我们用两个大vector——\(Left,Right\),\(Left_i\)记录频率为\(i\)的站,\(Right_i\)记录频率在\([i-K,i+K]\)之间的站。我们将\(Left_i\)里的站按其坐标来排序,\(Right_i\)里的站按其覆盖的左边界坐标排序。然后我们从左往右枚举\(Left_i\)里的元素。对于我们枚举的某个广播站\(x\),我们需要满足\(x \ge Lbound_y\),所以在\(Right_i\)中能与之配对的广播站肯定是一段前缀。但是还要满足\(x \le Rbound_y\),所以对于前缀我们需要用一个堆来维护\(Rbound_y \ge x\)的\(y\)。然后查询按坐标建立一个树状数组即可。
#include<algorithm>
#include<iostream>
#include<set>
#include<vector>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define lowbit(a) (a&-a)
typedef long long ll;
const int maxn = 100010,maxf = 10010;
int N,K,tree[maxn],disc[maxn],tot; ll ans;
inline void ins(int a,int b) { for (;a <= tot;a += lowbit(a)) tree[a] += b; }
inline int calc(int a) { int ret = 0; for (;a;a -= lowbit(a)) ret += tree[a]; return ret; }
inline int gi()
{
char ch; int ret = 0,f = 1;
do ch = getchar(); while (!(ch >= '0'&&ch <= '9')&&ch != '-');
if (ch == '-') f = -1,ch = getchar();
do ret = ret*10+ch-'0',ch = getchar(); while (ch >= '0'&&ch <= '9');
return ret*f;
}
struct Node
{
int x,r,f,lb;
inline void read() { x = gi(); r = gi(); f = gi(); lb = x-r; }
inline int rb() { return x+r; }
}sta[maxn];
vector <Node> Left[maxf],Right[maxf]; set < pair<int,int> > S;
inline bool cmp1(const Node &a,const Node &b) { return a.x < b.x; }
inline bool cmp2(const Node &a,const Node &b) { return a.lb < b.lb; }
int main()
{
freopen("763E.in","r",stdin);
freopen("763E.out","w",stdout);
N = gi(); K = gi();
for (int i = 1;i <= N;++i)
{
sta[i].read(); Left[sta[i].f].push_back(sta[i]);
for (int j = max(1,sta[i].f-K);j <= 10000&&j <= sta[i].f+K;++j) Right[j].push_back(sta[i]);
}
for (int i = 1;i <= 10000;++i)
sort(Left[i].begin(),Left[i].end(),cmp1),sort(Right[i].begin(),Right[i].end(),cmp2);
for (int i = 1;i <= 10000;++i)
{
int n1 = Left[i].size(),n2 = Right[i].size(),now = 0; tot = 0;
for (int j = n2-1;j >= 0;--j) disc[++tot] = Right[i][j].x;
sort(disc+1,disc+tot+1); tot = unique(disc+1,disc+tot+1)-disc-1;
for (int j = 0;j < n1;++j)
{
for (;now < n2&&Right[i][now].lb <= Left[i][j].x;++now)
{
int cor = lower_bound(disc+1,disc+tot+1,Right[i][now].x)-disc;
S.insert(make_pair(Right[i][now].rb(),cor)); ins(cor,1);
}
while (!S.empty()&&S.begin()->first < Left[i][j].x)
ins(S.begin()->second,-1),S.erase(S.begin());
int l = lower_bound(disc+1,disc+tot+1,Left[i][j].lb)-disc,r = upper_bound(disc+1,disc+tot+1,Left[i][j].rb())-disc-1;
ans += calc(r)-calc(l-1);
}
while (!S.empty())
ins(S.begin()->second,-1),S.erase(S.begin());
}
cout << ((ans-N)>>1) << endl;
fclose(stdin); fclose(stdout);
return 0;
}
CF762E Radio Stations的更多相关文章
- CodeForces - 762E:Radio stations (CDQ分治||排序二分)
In the lattice points of the coordinate line there are n radio stations, the i-th of which is descri ...
- 【题解】Radio stations Codeforces 762E CDQ分治
虽然说好像这题有其他做法,但是在问题转化之后,使用CDQ分治是显而易见的 并且如果CDQ打的熟练的话,码量也不算大,打的也很快,思维难度也很小 没学过CDQ分治的话,可以去看看我的另一篇博客,是CDQ ...
- Radio stations CodeForces - 762E (cdq分治)
大意: 给定$n$个三元组$(x,r,f)$, 求所有对$(i,j)$, 满足$i<j, |f_i-f_j|\le k, min(r_i,r_j)\ge |x_i-x_j|$ 按$r$降序排, ...
- Codeforces 1215F. Radio Stations
传送门 题目看一半:"woc 裸的 $2-sat$ 白给??" 看完以后:"...???" 如果没有 $f$ 的限制,那就是个白给的 $2-sat$ 问题,但是 ...
- 【CF1215F】 Radio Stations
题目 比较精妙的\(\text{2-sat}\)建图了 还是按照套路把每个电台拆成\((0/1,i)\)表示不选/选 前两种连边是板子就不解释了 考虑如何限制选择一个唯一的\(f\),并且还能限制不选 ...
- [cf1215F]Radio Stations
这道题如果没有功率的限制,显然就是一个裸的2-sat 考虑将功率的限制也放在图上:如果选择了功率i,那么功率区间不包含它的点只能不选,连边即可 但是这样建图的边数是o(n^2),需要优化 将功率区间分 ...
- Hijacking FM Radio with a Raspberry Pi & Wire
转载:https://null-byte.wonderhowto.com/how-to/hack-radio-frequencies-hijacking-fm-radio-with-raspberry ...
- 【英语魔法俱乐部——读书笔记】 2 中级句型-复句&合句(Complex Sentences、Compound Sentences)
[英语魔法俱乐部——读书笔记] 2 中级句型-复句&合句(Complex Sentences.Compound Sentences):(2.1)名词从句.(2.2)副词从句.(2.3)关系从句 ...
- webrtc 学习资源 http://www.cnblogs.com/lingyunhu/p/3578218.html
Realtime/Working WebRTC Experiments It is a repository of uniquely experimented WebRTC demos; writte ...
随机推荐
- FastDFS轻量级分布式文件系统部署
FastDFS介绍 FastDFS 是一个由 C 语言实现的开源轻量级分布式文件系统,作者余庆,支持 Linux.FreeBSD.AID 等 Unix 系统,解决了大数据存储和读写负载均衡等问题,适合 ...
- Python的matplotlib模块的使用-Github仓库
import matplotlib.pyplot as plt import numpy as np import requests url='https://api.github.com/searc ...
- C语言实例解析精粹学习笔记——43(希尔排序)
实例说明: 用希尔排序方法对数组进行排序.由于书中更关注的实例,对于原理来说有一定的解释,但是对于第一次接触的人来说可能略微有些简略.自己在草稿纸上画了好久,后来发现网上有好多很漂亮的原理图. 下面将 ...
- intellij idea之git执行打标签(tag)和删除标签
intellij idea 版本为2017.2.6 进入Version Control-->log 1.在之前版本中,右键,新建标签 2.输入标签名称,建议输入版本号的方式 3.push标签 由 ...
- 4299: Codechef FRBSUM
4299: Codechef FRBSUM https://www.lydsy.com/JudgeOnline/problem.php?id=4299 分析: 主席树. https://blog.se ...
- 《python核心编程第二版》第8章习题
8–1. 条件语句. 请看下边的代码 # statement Aif x > 0:# statement Bpasselif x < 0:# statement Cpasselse:# s ...
- HTTPS初始
https会话 1客户端 2服务器端 1 ---http三次握手--- 2 1<--------------->2 协商建立ssl会话 选择加密协议 sslv3 1 <------- ...
- deeplearning.ai课程学习(3)
第三周:浅层神经网络(Shallow neural networks) 1.激活函数(Activation functions) sigmoid函数和tanh函数两者共同的缺点是,在z特别大或者特别小 ...
- 安装VMware的一般步骤(安装过程总结)
(安装的是vmwareworkstation11以上) 0.遇到问题为没有权限打开服务“VMAuthdService”时,需要去管理->服务中打开对应的Windows Management In ...
- Go基础篇【第2篇】: 内置库模块 fmt
fmt官方文档说明:https://studygolang.com/pkgdoc import "fmt" mt包实现了类似C语言printf和scanf的格式化I/O.格式化动作 ...