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 ...
随机推荐
- Thinkphp5所有页面验证用户是否登陆
新建Base.php控制器,所有的页面继承自它 <?php namespace app\index\controller; use think\Controller; class Base ex ...
- STM32(7)——通用定时器PWM输出
1.TIMER输出PWM基本概念 脉冲宽度调制(PWM),是英文“Pulse Width Modulation”的缩写,简称脉宽调制,是利用微处理器的数字输出来对模拟电路进行控制的一种 ...
- Leecode刷题之旅-C语言/python-67二进制求和
/* * @lc app=leetcode.cn id=67 lang=c * * [67] 二进制求和 * * https://leetcode-cn.com/problems/add-binary ...
- POJ1236 tarjan
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19613 Accepted: 77 ...
- java 第三章 流程控制语句
1.条件语句 (1)if 语句 ( 单一条件) if (表达式){ 执行语句块 } (2)执行流程 · if 语句 条件表达式可以是任何一种逻辑表达式 如果表达式值为true,则执行花括号的内容后 ...
- WPF ItemsControl 手动刷新
原文:WPF ItemsControl 手动刷新 遇到这样一个问题, 我的ItemsSource是绑定到一个ObservableCollection<T>类型的实力上去的. 但是T类型没有 ...
- java练习题——类与对象
一.请依据代码的输出结果,自行总结Java字段初始化的规律 public static void main(String[] args) { InitializeBlockClass obj=new ...
- C#的委托Delegate
一.委托基础 1.什么是委托 委托(Delegate) 是存有对某个方法的引用的一种引用类型变量,用关键字delegate申明,实现相同返回值和参数的函数的动态调用,提供了对方法的抽象. 委托(Del ...
- Java - 问题集 - 导出csv文件中文乱码
微软的excel文件需要通过文件头的bom来识别编码,所以写文件时,需要先写入bom头. FileOutputStream fos = new FileOutputStream(new File(&q ...
- 「日常训练」 不容易系列之(3)—— LELE的RPG难题 (HDU 2045)
题目简述 有排成一行的n" role="presentation">nn个方格,用红(Red).粉(Pink).绿(Green)三色涂每个格子,每格涂一色,要求任何 ...