题目戳这里

我还以为是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的更多相关文章

  1. 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 ...

  2. 【题解】Radio stations Codeforces 762E CDQ分治

    虽然说好像这题有其他做法,但是在问题转化之后,使用CDQ分治是显而易见的 并且如果CDQ打的熟练的话,码量也不算大,打的也很快,思维难度也很小 没学过CDQ分治的话,可以去看看我的另一篇博客,是CDQ ...

  3. 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$降序排, ...

  4. Codeforces 1215F. Radio Stations

    传送门 题目看一半:"woc 裸的 $2-sat$ 白给??" 看完以后:"...???" 如果没有 $f$ 的限制,那就是个白给的 $2-sat$ 问题,但是 ...

  5. 【CF1215F】 Radio Stations

    题目 比较精妙的\(\text{2-sat}\)建图了 还是按照套路把每个电台拆成\((0/1,i)\)表示不选/选 前两种连边是板子就不解释了 考虑如何限制选择一个唯一的\(f\),并且还能限制不选 ...

  6. [cf1215F]Radio Stations

    这道题如果没有功率的限制,显然就是一个裸的2-sat 考虑将功率的限制也放在图上:如果选择了功率i,那么功率区间不包含它的点只能不选,连边即可 但是这样建图的边数是o(n^2),需要优化 将功率区间分 ...

  7. Hijacking FM Radio with a Raspberry Pi & Wire

    转载:https://null-byte.wonderhowto.com/how-to/hack-radio-frequencies-hijacking-fm-radio-with-raspberry ...

  8. 【英语魔法俱乐部——读书笔记】 2 中级句型-复句&合句(Complex Sentences、Compound Sentences)

    [英语魔法俱乐部——读书笔记] 2 中级句型-复句&合句(Complex Sentences.Compound Sentences):(2.1)名词从句.(2.2)副词从句.(2.3)关系从句 ...

  9. webrtc 学习资源 http://www.cnblogs.com/lingyunhu/p/3578218.html

    Realtime/Working WebRTC Experiments It is a repository of uniquely experimented WebRTC demos; writte ...

随机推荐

  1. ruby 第三方模块unirest使用

    Creating Requests require 'unirest' response = Unirest.post 'http://httpbin.org/post', headers:{ Acc ...

  2. dz论坛Discuz_X3.4最新网站漏洞

    近期我们sinesafe安全部门审计discuz最新版的时候发现配置文件写入导致代码执行的问题.cms安装的时候一般会分为几个步骤去进行,其中有对配置文件config进行写入的步骤,当写入的时候未严格 ...

  3. 预防跨站脚本(xss)

    对xss的防护方法结合在两点上输入和输出,一是严格控制用户表单的输入,验证所有输入数据,有效监测到攻击,go web表单中涉及到.二是对所有输出的数据进行处理,防止已成功注入的脚本在浏览器端运行. 在 ...

  4. Failed to read candidate component class错误分析

    org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component c ...

  5. ABAP CDS - Syntax

    The syntax of the DDL and of the DCL of the ABAP CDS comprises elements of the general DDL and DCL o ...

  6. linux c 出错集

    2018.7.8 1.声明结构体时,结构体内部不能赋值.比如 struct student{ char id=0; char score=0; }; 这样大错特错! 2.字符数组 char a[10] ...

  7. LeetCode:24. Swap Nodes in Pairs(Medium)

    1. 原题链接 https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2. 题目要求 给定一个链表,交换相邻的两个结点.已经交换 ...

  8. thrift安装

    yum -y install gcc-c++ autoconf automake sysconftool boost boost-devel libtool perl-ExtUtils-MakeMak ...

  9. SVN被锁定如何解决?

    报错: “E:/SVN被锁定” 请进行清理操作 解决方式: 1.试着右击进行解锁 没有解决 2.试着进行清理,清理后再做更新操作 好的成功. OK能正常使用了. 2017年10月26日更新 报错: s ...

  10. CTS测试笔记

    电脑安装12.4乌班图系统 更新源 (1) 打开ubuntu software center (2) 电脑左上角选择edit→software sources…→点击download from,选择o ...