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 ...
随机推荐
- Delphi 过程类型
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- MySQL 主从服务器配置
在主服务器Ubuntu上进行备份,执行命令: mysqldump -uroot -p --all-databases --lock-all-tables > ~/master_db.sql -u ...
- 函数名前加 & 符号的深入理解 C++
#include <iostream> using namespace std; int& test_str() { ; return a; //通过返回 a 的地址来进行 值的返 ...
- C# 实现窗口无边框,可拖动效果
#region 无边框拖动效果 [DllImport("user32.dll")]//拖动无窗体的控件 public static extern bool ReleaseCaptu ...
- 嵌入式框架Zorb Framework搭建五:事件的实现
我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮. 嵌入式框架Zorb Framework搭建过程 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建.调试输出和建立时间系 ...
- Java——自动生成30道四则运算---18.09.27
package chuti;import java.io.PrintWriter;import java.util.Scanner;import java.io.FileNotFoundExcepti ...
- 关于PHP性能提升踩过的一些坑
性能这个东西,在网站规模到达一定程度后,会是一个永恒的主题.关于这方面,本人有一些拙见,现在拿出来,大家一起探讨下. 1.编码过程中,传递参数时,尽量少使用‘引用传参’.这是一个巨坑啊 ...
- 第5模块闯关Bootstrap
“行(row)”必须包含在 .container (固定宽度)或 .container-fluid (100% 宽度)中,以便为其赋予合适的排列(aligment)和内补(padding). 通过“行 ...
- LeetCode:22. Generate Parentheses(Medium)
1. 原题链接 https://leetcode.com/problems/generate-parentheses/description/ 2. 题目要求 给出一个正整数n,请求出由n对合法的圆括 ...
- LeetCode:3.Longest Substring Without Repeating Characters
思路:看到题目首先想到最大字符串匹配KMP算法 public static int lengthOfLongestSubstring(String s) { int maxLength = 0; St ...