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. Nginx配置反向代理服务器

    首先,在阅读<深入理解Nginx模块>后,大体了解了配置反向代理服务器一些常见的配置.如下进行说明:” l  Nginx worker进程个数 语法: worker_processes n ...

  2. Autowried注解和Resource注解的区别

    目录 1.概述 2.Autowried 3.Resource 4.总结 1.概述 在使用Spring框架的过程中, 依赖注入是必须的, 大多时候会使用Autowried注解来进行依赖注入, 但是也可以 ...

  3. [Sw] 使用 Swoole Server task/协程 处理大数据量异步任务时注意

    关于 Buffered Query 和 Unbuffered Query:http://www.php.net/manual/zh/mysqlinfo.concepts.buffering.php 对 ...

  4. channel_v3.json

    channel_v3.json 下载地址:https://pan.baidu.com/s/1qRgQXiYD2-6MjTb3B3mIBg 源文件地址:https://raw.githubusercon ...

  5. hadoop常见问题

    Q1.什么是 Hadoop? Hadoop 是一个开源软件框架,用于存储大量数据,并发处理/查询在具有多个商用硬件(即低成本硬件)节点的集群上的那些数据.总之,Hadoop 包括以下内容: HDFS( ...

  6. Linux中文乱码 - - 更改Linux字符集

     查看当前系统默认采用的字符集: # locale 在RedHat/CentOS系统下,记录系统默认使用语言的文件是/etc/sysconfig/i18n,如果默认安装的是中文的系统,i18n的内容如 ...

  7. OneHot编码

    One-Hot编码 What.Why And When? 一句话概括:one hot编码是将类别变量转换为机器学习算法易于利用的一种形式的过程. 目录: 前言: 通过例子可能更容易理解这个概念. 假设 ...

  8. vue踩坑(二):跨域以及携带cookie

    最近后台需求要在请求的时候传cooki给后台,正常情况下拿到cookie后存在cookie里,同域名下是会自己带到请求头里的,但是因为要在本地调试,那么问题就来了,localhost:8080下面的c ...

  9. 递归可视化之汉诺塔的动画实现(turtle海龟)

    import turtle class Stack: def __init__(self): self.items = [] def isEmpty(self): def push(self, ite ...

  10. CentOS7下安装Redis

    一.下载redis wget http://download.redis.io/releases/redis-4.0.6.tar.gz 二.解压 tar -zxvf redis-4.0.6.tar.g ...