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. leetcode125. Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  2. UE4 Virtual Reality Input输入配置表导入

    [/Script/Engine.InputSettings] AxisConfig=(AxisKeyName="OculusTouch_Right_FaceButton2",Axi ...

  3. Linux 用户与组

    在 Linux 操作系统下,如何添加一个新用户到一个特定的组中?如何同时将用户添加到多个组中?又如何将一个已存在的用户移动到某个组或者给他增加一个组?对于不常用 Linux 的人来讲,记忆 Linux ...

  4. 云笔记项目-Spring事务学习-传播NESTED

    接下来测试事务传播属性NESTED Service层 Service层方法事务传播属性都设置为NESTED. LayerT层代码 package LayerT; import javax.annota ...

  5. C# File API

    [C# File API] 1.System.IO.File Provides static methods for the creation, copying, deletion, moving, ...

  6. c#遍历一个对象中所有的属性和值

    SpDictItem sp = GetCFHObject.GetSpItem("); PropertyInfo[] propertys = sp.GetType().GetPropertie ...

  7. Linux - Ubuntu 图形界面入门

    Ubuntu 图形界面入门 目标 熟悉 Ubuntu 图形界面的基本使用 01. Ubuntu 的任务栏 02. 窗口操作按钮 03. 窗口菜单条 ——本文源自<黑马程序员>

  8. nginx多域名、多证书

    环境: 一台nginx服务器 192.168.10.251 两台windowsserver2012 IIS服务器 (192.168.10.252.192.168.10.253) 从阿里云上下载ssl证 ...

  9. paloalto防火墙安装内容和软件更新

    1.为了确保您始终不会受到最新威胁(包括尚未发现的威胁)的攻击,您必须确保防火墙始终具有 Palo Alto Networks 发布的最新更新内容及软件. • Antivirus(防病毒)— 包括新的 ...

  10. paloalto防火墙内存使用率高

    上述内存使用率是正常的,实际使用的是buffers.