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 ...
随机推荐
- 一次 group by + order by 性能优化分析
一次 group by + order by 性能优化分析 最近通过一个日志表做排行的时候发现特别卡,最后问题得到了解决,梳理一些索引和MySQL执行过程的经验,但是最后还是有5个谜题没解开,希望大家 ...
- Python接受流式输入
随笔记录——Python接受终端入若干行输入 Python接受终端的若干行输入时,比较常用的input()不再好用. 1. 导入sys模块: import sys 2. for循环接受输入: for ...
- Flask初学者:url_for
URL反转:反转是指通过视图函数名称得到其对应的URL(有反转也就有正转,即通过URL得到视图函数返回的内容,也就是我们平时的访问网页了),需要“url_for(endpoint, **values) ...
- 预防跨站脚本(xss)
对xss的防护方法结合在两点上输入和输出,一是严格控制用户表单的输入,验证所有输入数据,有效监测到攻击,go web表单中涉及到.二是对所有输出的数据进行处理,防止已成功注入的脚本在浏览器端运行. 在 ...
- class实现Stack
基于class实现一个存储string类型的Stack 头文件: //stack.h #include<vector> #include<string> class Stack ...
- 販売管理(SD)
SD(販売管理)系のSAP DBテーブル. 随時更新していきます. [得意先マスタ]KNA1: 一般データ KNB1: 会計データ KNBK: 銀行データ KNVV: 販売データ KNVP: 取引先機 ...
- Hadoop Sentry 学习
什么是Sentry? Sentry 是Cloudera 公司发布的一个Hadoop开源组件,它提供细粒度基于角色的安全控制 Sentry下的数据访问和授权 通过引进Sentry,Hadoop目前可在以 ...
- 安装和配置Sentry(收录)
安装和配置Sentry 本文主要记录安装和配置Sentry的过程,关于Sentry的介绍,请参考 Apache Sentry架构介绍 . 1. 环境说明 系统环境: 操作系统:CentOs 6.6 H ...
- Kotlin 1 函数
#2 函数 函数声明和平时我见到的有点不太一样,使用关键字fun来声明.(感觉好欢乐的样子···O(∩_∩)O~~) 下面的示例,简单的声明了一个函数: // 这是函数声明 fun this_is_a ...
- 类和实例属性的查找顺序 mro查找
如果多个类继承父类,然后又被多个类继承这种复杂的问题,可以使用 mro方法 例如: class A: pass class C(D): pass class B(D): pass class A(B, ...