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. 颜色空间之CIE2000色差公式

    CIEDE2000色差公式   为了进一步改善工业色差评价的视觉一致性,CIE专门成立了工业色差评价的色相和明度相关修正技术委员会TC1-47(Hue and Lightness Dependent ...

  2. JS的基本(原始)数据类型

    1.boolean  true & false 2.null   空值类型 3.undefined  未定义类型 4.number    数值类型 5.string    字符串类型 6.sy ...

  3. 移动端响应式布局--你不知道的CSS3.0媒体查询,解决rem部分情况下无法适配的场景

    媒体查询作为响应式布局的方法之一,实际项目中用途也很广.但是你真的知道怎么用吗? 例如,下面匹配 iphone6/7/8 屏幕 @media screen and (max-width: 375px) ...

  4. 手眼标定eye-to-hand 示例:handeye_stationarycam_calibration

    * * This example explains how to use the hand eye calibration for the case where* the camera is stat ...

  5. 让history显示时间

    如何让history显示时间 linux和unix上都提供了history命令,可以查询以前执行的命令历史记录 但是,这个记录并不包含时间项目 因此只能看到命令,但是不知道什么时间执行的 如何让his ...

  6. superset在 centos 7安装运行

    参考:1.http://blog.csdn.net/u014729236/article/details/76302888?locationNum=2&fps=1 2.https://www. ...

  7. del_cursor 批量删除游标

    declare   cursor [del_cursor] is select a.*, a.rowid row_id from [table_name] a order by a.rowid;   ...

  8. 自己实现ArrayList

    思路: 一 载体 ArrayList是一个集合容器,必然要有一个保存数据的载体. public class MyArraylist { private final static int INIT_CO ...

  9. maven 实用的命令

    查询某个pom的来源: com.taobao.langyan langyan-client 命令: mvn dependency:tree -Dverbose -Dincludes=com.aliba ...

  10. ERC20 token standard issues.