题目描述

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. angularjs中audio/video 路径赋值问题

    之前解决这个问题都是通过js的attr赋值解决的,但是也一直不明白为什么audio直接在HTML中赋值报错.解决方法就是通过添加$sce过滤效果 app.filter("trustUrl&q ...

  2. Cisco Packet Tracer中通过集线器组网

    Cisco Packet Tracer中可以通过集线器将多台电脑完成通信. Cisco Packet Tracer 6.2.0 一.添加三台电脑设备 1.按照下图1.2步骤操作,2步骤执行三次,拖拽P ...

  3. CentOS删除安装的程序

    以mysql举例: 首先查询安装包: rpm -qa|grep mysql 查询到的一个结果为:mysql-community-libs-5.7.13-1.el6.x86_64 yum 删除 yum ...

  4. Sony深度学习框架 - Neural Network Console - 教程(1)- 原来深度学习可以如此简单

    “什么情况!?居然不是黑色背景+白色文字的命令行.对,今天要介绍的是一个拥有白嫩的用户界面的深度学习框架.” 人工智能.神经网络.深度学习,这些概念近年已经涌入每个人的生活中,我想很多人早就按捺不住想 ...

  5. PHP 包含文件

    1.require test123.php <?php $a=1; 运行文件: <?php require('test123.php'); echo 'Hello!'; echo '< ...

  6. md5sum命令详解

    基础命令学习目录首页 原文链接:https://blog.csdn.net/cbbbc/article/details/48563023 前言 在网络传输.设备之间转存.复制大文件等时,可能会出现传输 ...

  7. 互评Beta版本——杨老师粉丝群——Pinball

    互评beta版本    杨老师粉丝群——<PinBall> 一.基于NABCD评论作品,及改进建议 1.根据(不限于)NABCD评论作品的选题 (1)N(Need,需求) 随着年龄的增长, ...

  8. Scrum Meeting 13 -2014.11.19

    最近数据库和编译的实验课也开始了,大家晚上的时间直接被砍掉了大部分. 希望大家能顺利完成项目吧.剩下时间也不多了,如果程序还存在一些特别的问题和需要优化修改的地方也应该考虑留到下阶段进行了. Memb ...

  9. (小组)第六次作业:NABCD模型分析。产品Backlog。

    NABCD模型分析: NABCD模型分析 1.N——need需求 随着时代的进步,人们生活水平的提高,现在手机的普及率已经非常高了,而且现在的家长很多时候会忙于工作,很少会花时间出来给自己读小学的孩子 ...

  10. Spring 中使用Properties文件

    Spring提供了加载Properties文件的工具类:org.springframework.beans.factory.config.PropertyPlaceholderConfigurer. ...