题目描述

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. Python中的注释

    1.1 注释的目的 通过用自己熟悉的语言,在程序中对某些代码进行标注说明,这就是注释的作用,能够大大增强程序的可读性. 1.2 注释的分类 1.2.1 单行注释 以#开头,#右边的所有东西当做说明,而 ...

  2. java.io.tmpdir指定的路径在哪?

    Java.io.tmpdir介绍 System.getproperty(“java.io.tmpdir”)是获取操作系统缓存的临时目录,不同操作系统的缓存临时目录不一样, 在Windows的缓存目录为 ...

  3. C#中字符串 "驻留"与Lock(转载)

    class TestWorker 2 {         3     public void DoMultiThreadedWork(object someParameter) 4     { 5   ...

  4. k倍区间:前缀和

    [蓝桥杯][2017年第八届真题]k倍区间 题目描述 给定一个长度为N的数列,A1, A2, ... AN,如果其中一段连续的子序列Ai, Ai+1, ... Aj(i <= j)之和是K的倍数 ...

  5. XSS-DVWA

    1.反射型 LOW: 没有过滤,直接键入PAYLOAD 查看源码 这里没有任何过滤,使用htmlspecialchars()过滤 结果不弹窗 MEDIUM: LOW等级的方法不奏效了 观察输出可能是过 ...

  6. 基于Neutron的Kubernetes SDN实践经验之谈

    首先,向大家科普下Kubernetes所选择的CNI网络接口,简单介绍下网络实现的背景. CNI即Container Network Interface,是一套容器网络的定义规范,包括方法规范.参数规 ...

  7. 在虚拟机下安装Ubuntu

    目录: 1.安装虚拟机 2.在虚拟下安装Ubuntu 本文将按照目录分两步来讲一下在虚拟机下安装Ubuntu.第一步是安装虚拟机,第二步是在虚拟机下安装Ubuntu. 安装虚拟机 下载虚拟机链接以及激 ...

  8. Microsoft Visual Studio 2013 的安装及单元测试

    题目:练习教科书第22~25页单元测试练习,要求自行安装Visual Studio开发平台,版本至少在2010以上,要求把程序安装过程和练习过程写到博客上,越详细越好,要图文并茂. 安装过程: 1.下 ...

  9. 【Alpha版本发布】爬虫队长正在待命!

    一.基础功能简介 本团队的爬虫能够从网上搜索相关内容, 并归类,把所爬到的网页或各种类型的文档下载到本地上. 上届团队Beta版本爬虫的主要功能如下: a)可爬取网页,问答页并进行问答文件分类. b) ...

  10. 为什么要读这本书《java编程思想》

    在学校学习编程语言只是浅显的了解,不理解的的东西有很多 ,想通过这本书对java语言有更深的理解,对于以后的学习和工作会有很大的好处.与其了了草草的看你一些书,知其然而不知其所以然,这不是我想要的学习 ...