https://codeforces.com/contest/1139/problem/F

题意

有m个人,n道菜,每道菜有\(p_i\),\(s_i\),\(b_i\),每个人有\(inc_j\),\(pref_j\),一个人可以买一道菜的条件是

1. \(p_i \leq inc_j \leq s_i\)

2. \(|b_i - pref_j| \leq inc_j-p_i\)

,问每个人分别能买多少道菜

题解

  • 转化一下公式

    1. \(p_i \leq inc_j \leq s_i\)
    • 下面两个满足其一即可
    1. \(b_i + p_i \leq inc_j + pref_j\)
    2. \(p_i - b_i \leq inc_j - pref_j\)
  • 对于每个j只需要找出有多少个i满足要求就行
  • 按\(p_i \leq inc_j \leq s_i\)建立时间节点并排序,然后从前往后扫一遍,用两颗splay维护后两个式子并计数

代码

#include<bits/stdc++.h>
#define ls(x) tr[x].son[0]
#define rs(x) tr[x].son[1]
#define MAXN 200005
using namespace std;
struct SPLAY{
int root=0,tot=0;
struct node{
int son[2],par;
int sz,tsz,v;
void init(int _v=0,int _sz=0){
v=_v;
tsz=sz=_sz;
son[0]=son[1]=par=0;
}
}tr[MAXN];
void push_up(int x){
tr[x].tsz=tr[x].sz+tr[ls(x)].tsz+tr[rs(x)].tsz;
}
int chk(int x){
return rs(tr[x].par)==x;
}
void rot(int x){
int y=tr[x].par,z=tr[y].par,k=chk(x),w=tr[x].son[k^1];
tr[y].son[k]=w;tr[w].par=y;
tr[z].son[chk(y)]=x;tr[x].par=z;
tr[x].son[k^1]=y;tr[y].par=x;
push_up(y);
push_up(x);
}
void splay(int x,int goal){
while(tr[x].par!=goal){
int y=tr[x].par,z=tr[y].par;
if(z!=goal){
if(chk(x)==chk(y))rot(y);
else rot(x);
}
rot(x);
}
if(!goal)root=x;
}
void insert(int v,int sz){
int p=root,ff=0;
while(p&&tr[p].v!=v){
ff=p;
p=tr[p].son[v>tr[p].v];
}
if(p){
tr[p].sz+=sz;
}else{
p=++tot;
if(ff)tr[ff].son[v>tr[ff].v]=p;
tr[p].init(v,sz);
tr[p].par=ff;
}
splay(p,0);
}
int count(int v){
insert(v,1);
int re=tr[rs(root)].tsz;
insert(v,-1);
return re;
}
}V[2];
int n,m,p[MAXN],s[MAXN],b[MAXN],inc[MAXN],pref[MAXN],ans[MAXN];
int cnt=0,tot=0; struct node{
int v,id,kd;
}A[MAXN*4];
bool cmp(node x,node y){
if(x.v==y.v)return x.kd<y.kd;
return x.v<y.v;
}
void add(int v,int id,int kd){
A[++tot].v=v;
A[tot].id=id;
A[tot].kd=kd;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)scanf("%d",&p[i]),add(p[i],i,1);
for(int i=1;i<=n;i++)scanf("%d",&s[i]),add(s[i],i,3);
for(int i=1;i<=n;i++)scanf("%d",&b[i]);
for(int i=1;i<=m;i++)scanf("%d",&inc[i]),add(inc[i],i,2);
for(int i=1;i<=m;i++)scanf("%d",&pref[i]);
sort(A+1,A+tot+1,cmp);
for(int i=1;i<=tot;i++){
int id=A[i].id;
if(A[i].kd==1){
cnt++;
V[0].insert(b[id]+p[id],1);
V[1].insert(p[id]-b[id],1);
}else if(A[i].kd==2){
ans[id]=cnt-V[0].count(inc[id]+pref[id])-V[1].count(inc[id]-pref[id]);
}else{
cnt--;
V[0].insert(b[id]+p[id],-1);
V[1].insert(p[id]-b[id],-1);
}
}
for(int i=1;i<=m;i++)printf("%d ",ans[i]);
}

Codeforces Round #548 (Div. 2) F splay(新坑) + 思维的更多相关文章

  1. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  2. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  3. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  4. Codeforces Round 548 (Div. 2)

    layout: post title: Codeforces Round 548 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  5. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  6. Codeforces Round #521 (Div. 3) E. Thematic Contests(思维)

    Codeforces Round #521 (Div. 3)  E. Thematic Contests 题目传送门 题意: 现在有n个题目,每种题目有自己的类型要举办一次考试,考试的原则是每天只有一 ...

  7. Codeforces Round #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂

    https://codeforces.com/contest/1106/problem/F 题意 数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k} ...

  8. Codeforces Round #532 (Div. 2) F 线性基(新坑) + 贪心 + 离线处理

    https://codeforces.com/contest/1100/problem/F 题意 一个有n个数组c[],q次询问,每次询问一个区间的子集最大异或和 题解 单问区间子集最大异或和,线性基 ...

  9. Codeforces Round #549 (Div. 2) F 数形结合 + 凸包(新坑)

    https://codeforces.com/contest/1143/problem/F 题意 有n条形如\(y=x^2+bx+c\)的抛物线,问有多少条抛物线上方没有其他抛物线的交点 题解 \(y ...

随机推荐

  1. 20175213 2018-2019-2 《Java程序设计》第4周学习总结

    ## 教材学习内容总结 在第四周的学习过程中,我学习了第五章的内容. 第五章内容总结: 1.子类继承的方法只能操作子类继承和隐藏的成员变量. 2.子类和父类在同一包的继承性 子类自然继承了其父类中不是 ...

  2. Windows10安装pycocotools方法,亲测可用!

    如果遇到:No module named 'pycocotools' 错误,说明你的环境需要安装pycocotools,以下介绍在Windows10下安装pycocotools的方法,这是本人结合看过 ...

  3. xml嵌套防止解析

    举个例子 <?xml version="1.0" encoding="UTF-8"?><Messages><Message typ ...

  4. rsync镜像命令

    rsync -e 'ssh -p 19809' -av wwwroot root@3.3.3.3:/home/download/ 参数详解 编辑 -v, --verbose 详细模式输出 -q, -- ...

  5. “AS3.0高级动画编程”学习:第二章转向行为(上)

    因为这一章的内容基本上都是涉及向量的,先来一个2D向量类:Vector2D.as (再次强烈建议不熟悉向量运算的童鞋,先回去恶补一下高等数学-07章空间解释几何与向量代数.pdf) 原作者:菩提树下的 ...

  6. IDEA 中tomcat日志位置

    参考 https://blog.csdn.net/dela_/article/details/78555977 /home/dela/.IntelliJIdea2017.1/system/tomcat ...

  7. centos7上安装zabbix4.0

    zabbix4.0已经推出有一段时间了,针对之前版本做了很多优化配置,易用性得到提高,特别lts(long team support)长技术支持版本,官方说提供5年的稳定技术支持,在商业化运用上,是比 ...

  8. Python开发【第十篇】:RabbitMQ队列

    简介 RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQP(高级消息队列协议)的标准实现. 安装 首先安装erlang环境. 官网:http://www.erl ...

  9. 切面编程AOP之KingAOP

    1. 在Nuget上安装KingAOP 2. 创建一个新的类 public class Test : IDynamicMetaObjectProvider { public DynamicMetaOb ...

  10. functools 之 partial(偏函数)

    当函数的参数个数太多,需要简化时,使用functools.partial可以创建一个新的函数,这个新函数可以固定住原函数的部分参数,从而在调用时更简单.当然,decorator(装饰器) 也可以实现, ...