Codeforces Round #548 (Div. 2) F splay(新坑) + 思维
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\)
,问每个人分别能买多少道菜
题解
- 转化一下公式
- \(p_i \leq inc_j \leq s_i\)
- 下面两个满足其一即可
- \(b_i + p_i \leq inc_j + pref_j\)
- \(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(新坑) + 思维的更多相关文章
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round 548 (Div. 2)
layout: post title: Codeforces Round 548 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #521 (Div. 3) E. Thematic Contests(思维)
Codeforces Round #521 (Div. 3) E. Thematic Contests 题目传送门 题意: 现在有n个题目,每种题目有自己的类型要举办一次考试,考试的原则是每天只有一 ...
- 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} ...
- Codeforces Round #532 (Div. 2) F 线性基(新坑) + 贪心 + 离线处理
https://codeforces.com/contest/1100/problem/F 题意 一个有n个数组c[],q次询问,每次询问一个区间的子集最大异或和 题解 单问区间子集最大异或和,线性基 ...
- Codeforces Round #549 (Div. 2) F 数形结合 + 凸包(新坑)
https://codeforces.com/contest/1143/problem/F 题意 有n条形如\(y=x^2+bx+c\)的抛物线,问有多少条抛物线上方没有其他抛物线的交点 题解 \(y ...
随机推荐
- 如何解决 快速点击多次触发的bug 期望快速点击只一次生效
var lastClick; lockClick(){ var nowClick = new Date(); if (lastClick === undefined) { lastClick = no ...
- 【原创】访问Linux进程文件表导致系统异常复位的排查记录
前提知识: Linux内核.Linux 进程和文件数据结构.vmcore解析.汇编语言 问题背景: 这个问题出自项目的一个安全模块,主要功能是确定某进程是否有权限访问其正在访问的文件. 实现功能时,需 ...
- C#移除URL上指定的参数
/// <summary> /// 移除URL上指定的参数,不区分参数大小写 /// </summary> public static ...
- JDBC测试计划-连接mysql
一.测试环境准备 mysql:5.5 JDBC驱动:mysql-connector-java-5.1.30.jar 文件复制到JMeter/lib目录下 JMeter:jmeter-3.2 ...
- Django之CBV\FBV
FBV(function base views) 就是在视图里使用函数处理请求. 写一个FBV的实例:在views中代码 :就是写了个函数 def book(request): ''' :param ...
- 域名系统DNS以及跨域问题
域名到Ip地址解析是由分布在因特网上的许多域名服务器程序共同完成的.运行域名服务器程序的机器是域名服务器 域名到ip地址的解析过程: 当一个应用进程需要把主机名解析为ip地址时,该应用就调用解析程 ...
- 第六周博客作业 <西北师范大学| 周安伟>
第六周博客作业 助教博客链接:https://home.cnblogs.com/u/zaw-315/ 本周工作:评阅作业24份点评困难的作业:无作业要求:https://www.cnblogs.com ...
- 混合现实开发教程unity2017
共52节,MP4格式,英字,大小1GB 扫码时备注或说明中留下邮箱 付款后如未回复请至https://shop135452397.taobao.com/ 联系店主
- Mybatis 接口代理的实现(BeanDefinitionRegistryPostProcessor+FactoryBean)
相信在开发中,尤其是mybatis 配置操作中,我们只需要提供一个mapper 接口,然后注入到service 中,就可以进行调用. 按我们的一般逻辑来说,我们并没有进行接口的实现,应该会报空指针异常 ...
- Java多线程01(Thread类、线程创建、线程池)
Java多线程(Thread类.线程创建.线程池) 第一章 多线程 1.1 多线程介绍 1.1.1 基本概念 进程:进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于 ...