题目描述

The big consignment of t-shirts goes on sale in the shop before the beginning of the spring. In all nn types of t-shirts go on sale. The t-shirt of the ii -th type has two integer parameters — c_{i}ci​ and q_{i}qi​ , where c_{i}ci​ — is the price of the ii -th type t-shirt, q_{i}qi​ — is the quality of the ii -th type t-shirt. It should be assumed that the unlimited number of t-shirts of each type goes on sale in the shop, but in general the quality is not concerned with the price.

As predicted, kk customers will come to the shop within the next month, the jj -th customer will get ready to spend up to b_{j}bj​ on buying t-shirts.

All customers have the same strategy. First of all, the customer wants to buy the maximum possible number of the highest quality t-shirts, then to buy the maximum possible number of the highest quality t-shirts from residuary t-shirts and so on. At the same time among several same quality t-shirts the customer will buy one that is cheaper. The customers don't like the same t-shirts, so each customer will not buy more than one t-shirt of one type.

Determine the number of t-shirts which each customer will buy, if they use the described strategy. All customers act independently from each other, and the purchase of one does not affect the purchase of another.

输入输出格式

输入格式:

The first line contains the positive integer nn ( 1<=n<=2·10^{5}1<=n<=2⋅105 ) — the number of t-shirt types.

Each of the following nn lines contains two integers c_{i}ci​ and q_{i}qi​ ( 1<=c_{i},q_{i}<=10^{9}1<=ci​,qi​<=109 ) — the price and the quality of the ii -th type t-shirt.

The next line contains the positive integer kk ( 1<=k<=2·10^{5}1<=k<=2⋅105 ) — the number of the customers.

The next line contains kk positive integers b_{1},b_{2},...,b_{k}b1​,b2​,...,bk​ ( 1<=b_{j}<=10^{9}1<=bj​<=109 ), where the jj -th number is equal to the sum, which the jj -th customer gets ready to spend on t-shirts.


输出格式:

The first line of the input data should contain the sequence of kk integers, where the ii -th number should be equal to the number of t-shirts, which the ii -th customer will buy.

输入输出样例

输入样例#1:

3
7 5
3 5
4 3
2
13 14
输出样例#1:

2 3
输入样例#2:

2
100 500
50 499
4
50 200 150 100
输出样例#2:

1 2 2 1

说明

In the first example the first customer will buy the t-shirt of the second type, then the t-shirt of the first type. He will spend 10 and will not be able to buy the t-shirt of the third type because it costs 4, and the customer will owe only 3. The second customer will buy all three t-shirts (at first, the t-shirt of the second type, then the t-shirt of the first type, and then the t-shirt of the third type). He will spend all money on it.

Solution:

  本题无旋treap,思路也是ZYYS。

  题意就是$n$个物品,每个都有花费$ci$和价值$pi$,然后有$m$个人每个人有$ai$的钱,每件衣服一个人只能买一次,一个人每次会买他所承担的起且没买过的价值最高的衣服,问最后每个人买了几件衣服。

  首先我们肯定得确定一个买物品的顺序,既然每次是买价值最高的,那么我们以价值对物品从大到小排(相同价值花费小的排前面),再对每个人的构建平衡树,就可以依次枚举每件物品,在平衡树中查它能被那些人买。

  我们用无旋treap写,可以将树按物品的花费$split$成两棵树$x,y$,那么对于权值大于等于花费的$y$树,每个节点的贡献都+1、权值都-花费,我们可以直接懒惰标记。

  那么问题是权值减少后,就不能$merge$分离出的两棵树,因为此时不一定满足右边的树权值严格大于左边的树了。

  解决办法是对于$y$树,我们再按花费-1来$split$成两棵树$y,z$,新形成的$y$由于权值小于花费,所以得和$x$合并,由于可能存在懒惰标记,于是我们只能暴力中序遍历$y$的每个节点并插入到$x$树中,然后因为$z$树权值还是严格大于等于花费,所以直接$merge$新的$x$和$z$。

  暴力重构怎么能AC?现在我们来证明每个节点最多被暴力合并$\log val$次,注意我们重构的标准是对于那些减去花费后权值小于花费的点暴力重构,设原权值为$x$、花费为$y$,那么就有$x-y<y$,即$x<2y$,即每次被重构的点的权值至少减少一半,那么最多就被重构$\log val$次咯。

  于是时间复杂度$O(n\log n)$。

代码:

/*Code by 520 -- 9.27*/
#include<bits/stdc++.h>
#define il inline
#define ll long long
#define RE register
#define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(RE int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=;
int n,m,root,cnt,ch[N][],date[N],num[N],rnd[N],ppx[N],wmz[N];
struct node{
int c,q;
bool operator < (const node &a) const{return q==a.q?c<a.c:q>a.q;}
}a[N]; int gi(){
int a=;char x=getchar();
while(x<''||x>'') x=getchar();
while(x>=''&&x<='') a=(a<<)+(a<<)+(x^),x=getchar();
return a;
} il void down(int rt){
if(ppx[rt]){
date[ch[rt][]]+=ppx[rt],date[ch[rt][]]+=ppx[rt];
ppx[ch[rt][]]+=ppx[rt],ppx[ch[rt][]]+=ppx[rt];
ppx[rt]=;
}
if(wmz[rt]){
num[ch[rt][]]+=wmz[rt],num[ch[rt][]]+=wmz[rt];
wmz[ch[rt][]]+=wmz[rt],wmz[ch[rt][]]+=wmz[rt];
wmz[rt]=;
}
} int merge(int x,int y){
if(!x||!y) return x+y;
if(rnd[x]<rnd[y]){
down(x);
ch[x][]=merge(ch[x][],y);
return x;
}
else{
down(y);
ch[y][]=merge(x,ch[y][]);
return y;
}
} void split(int rt,int k,int &x,int &y){
if(!rt) {x=y=;return;}
down(rt);
if(date[rt]<k) x=rt,split(ch[rt][],k,ch[rt][],y);
else y=rt,split(ch[rt][],k,x,ch[rt][]);
} il int ins(int a,int b){
int x=,y=;
split(a,date[b],x,y);
a=merge(x,merge(b,y));
return a;
} int dfs(int x,int y){
if(!x) return y;
down(x);
y=dfs(ch[x][],y);
y=dfs(ch[x][],y);
ch[x][]=ch[x][]=;
return ins(y,x);
} void getans(int x){
if(!x) return;
down(x);
getans(ch[x][]),getans(ch[x][]);
} int main(){
n=gi();
For(i,,n) a[i].c=gi(),a[i].q=gi();
sort(a+,a+n+);
m=gi();
For(i,,m) date[i]=gi(),rnd[i]=rand(),root=ins(root,i);
For(i,,n) {
int x=,y=,z=,o=;
split(root,a[i].c,x,y);
date[y]-=a[i].c,ppx[y]-=a[i].c;
num[y]++,wmz[y]++;
split(y,a[i].c-,z,o);
x=dfs(z,x);
root=merge(x,o);
}
getans(root);
For(i,,m) printf("%d ",num[i]);
return ;
}

CF702F T-Shirts的更多相关文章

  1. CF702F T-Shirts FHQ Treap

    题意翻译 题目大意: 有n种T恤,每种有价格ci和品质qi.有m个人要买T恤,第i个人有vi元,每人每次都会买一件能买得起的qi最大的T恤.一个人只能买一种T恤一件,所有人之间都是独立的.问最后每个人 ...

  2. MySQL基础

    数据库操作 ---终端使用数据库 mysql -u root -p 之后回车键 输入密码 ---显示所有数据库: show databases; ---默认数据库: mysql - 用户权限相关数据 ...

  3. Swift3.0P1 语法指南——构造器

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  4. [ASP.NET MVC 小牛之路]07 - URL Routing

    我们知道在ASP.NET Web Forms中,一个URL请求往往对应一个aspx页面,一个aspx页面就是一个物理文件,它包含对请求的处理. 而在ASP.NET MVC中,一个URL请求是由对应的一 ...

  5. Mysql操作初级

    Mysql操作初级 本节内容 数据库概述 数据库安装 数据库操作 数据表操作 表内容操作 1.数据库概述 数据库管理系统叫做DBMS 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建 ...

  6. MYSQL(一)

    一,概述: 1,什么是数据库: 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库. 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Serv ...

  7. MVC路由配置

    目录 URL Routing 的定义方式 示例准备 给片段变量定义默认值 定义静态片段 自定义片段变量 自定义片段变量的定义和取值 将自定义片段变量作为Action方法的参数 指定自定义片段变量为可选 ...

  8. MySQL使用详解--根据个人学习总结

    1.安装配置 2.启动mysql服务并配置 mysql> \s(status也行) 查看当前服务器状态 查看编码状态 Server characterset : utf8 Db characte ...

  9. Google140道面试题

    FQ找来,可能历史比较悠久了,慢慢看. 原文连接:http://www.impactinterview.com/2009/10/140-google-interview-questions/ Goog ...

随机推荐

  1. 人在囧途——Java程序猿学习Python

    引言 LZ之前其实一直对python都很好奇,只是苦于平时没有时间去了解它,因此趁着51假期这个机会,便迫不及待的开始了自己的探索.作为一个标准的Java程序猿,在了解python的过程当中,LZ遇到 ...

  2. Docker部署Redis容器

    从仓库下载镜像 sudo docker pull redis   创建容器(前提:将redis.conf文件放入到/Users/chengang/docker/redis目录里面) docker ru ...

  3. JMeter做http接口压力测试

    测前准备 用JMeter做接口的压测非常方便,在压测之前我们需要考虑这几个方面: 场景设定 场景分单场景和混合场景.针对一个接口做压力测试就是单场景,针对一个流程做压力测试的时候就是混合场景,需要多个 ...

  4. Jenkins远程测试

    Jenkins远程测试 网络测试,如,selenium 测试可以通过主从和 selenium 套件插件远程安装在机器上运行.下列步骤显示了如何运行使用此配置来进行远程测试. 第1步 - 确保主从配置到 ...

  5. WebStorm微信小程序单位rpx出现空格问题

    Windows先下载sed 配置Tools->File Watchers->+ 添加下图配置 //windows系统 -i s/"\ rpx"/rpx/g $FileP ...

  6. phpcmsv9广告版位调用方法

    <div class="ya"> <?php // pc:get 使用sql语句获取指定条件的广告版位! ?> {pc:get sql="SELE ...

  7. GitHub笔记(四)——标签管理

    五 标签管理 1 打标签.默认master $ git tag v1.0 要对add merge这次提交打标签,它对应的commit id是f52c633,敲入命令: $ git tag v0.9 f ...

  8. MUI的踩坑笔记

    最近在做公司项目的手机端实现,稍微记录下遇到的坑 1.在app开发中,若要使用HTML5+扩展api,必须等plusready事件发生后才能正常使用,mui将该事件封装成了mui.plusReady( ...

  9. MyCat安装与测试教程 超详细!

    MyCat安装与测试教程 超详细! MyCat基础知识 一.什么是MYCAT? 1. 一个彻底开源的,面向企业应用开发的大数据库集群 2. 支持事务.ACID.可以替代MySQL的加强版数据库 3. ...

  10. 基于C#的机器学习--惩罚与奖励-强化学习

    强化学习概况 正如在前面所提到的,强化学习是指一种计算机以“试错”的方式进行学习,通过与环境进行交互获得的奖赏指导行为,目标是使程序获得最大的奖赏,强化学习不同于连督学习,区别主要表现在强化信号上,强 ...