题意翻译

题目大意:

有n种T恤,每种有价格ci和品质qi。有m个人要买T恤,第i个人有vi元,每人每次都会买一件能买得起的qi最大的T恤。一个人只能买一种T恤一件,所有人之间都是独立的。问最后每个人买了多少件T恤?

题目描述

The big consignment of t-shirts goes on sale in the shop before the beginning of the spring. In all n n n types of t-shirts go on sale. The t-shirt of the i i i -th type has two integer parameters — ci c_{i} ci​ and qi q_{i} qi​ , where ci c_{i} ci​ — is the price of the i i i -th type t-shirt, qi q_{i} qi​ — is the quality of the i i i -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, k k k customers will come to the shop within the next month, the j j j -th customer will get ready to spend up to bj 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 n n n ( 1<=n<=2⋅105 1<=n<=2·10^{5} 1<=n<=2⋅105 ) — the number of t-shirt types.

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

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

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

输出格式:

The first line of the input data should contain the sequence of k k k integers, where the i i i -th number should be equal to the number of t-shirts, which the i i i -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.

朴素算法很好想,但TLE;

考虑用平衡树维护;

我们先将衬衫按quality排序;

然后对于每一件衬衫,我们在用人的钱构成的树中操作;

对于>=C[i] 的进行标记,然后下传;

但是-C[i]后,不一定满足merge的条件;

我们可以暴力对于重复的部分进行merge;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii; inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int n, m, root;
int lson[maxn], rson[maxn], val[maxn], rnd[maxn];
int cnt[maxn], add[maxn], ans[maxn]; void pushdown(int v) {
if (add[v]) {
add[lson[v]] += add[v]; add[rson[v]] += add[v];
val[lson[v]] += add[v]; val[rson[v]] += add[v];
add[v] = 0;
}
if (ans[v]) {
ans[lson[v]] += ans[v]; ans[rson[v]] += ans[v];
cnt[lson[v]] += ans[v]; cnt[rson[v]] += ans[v];
ans[v] = 0;
}
} void split(int k, int &x, int &y, int v) {
if (!k) {
x = y = 0; return;
}
pushdown(k);
if (val[k] < v) {
x = k; split(rson[k], rson[x], y, v);
}
else {
y = k; split(lson[k], x, lson[y], v);
}
} int merge(int x, int y) {
if (!x || !y)return x + y;
if (rnd[x] < rnd[y]) {
pushdown(x); rson[x] = merge(rson[x], y);
return x;
}
else {
pushdown(y); lson[y] = merge(x, lson[y]);
return y;
}
} int ins(int x, int y) {
int r1 = 0, r2 = 0;
split(x, r1, r2, val[y]);
x = merge(merge(r1, y), r2);
return x;
} int build(int v, int y) {
if (!v)return y;
pushdown(v);
y = build(lson[v], y); y = build(rson[v], y);
lson[v] = rson[v] = 0;
return ins(y, v);
} void dfs(int v) {
if (!v)return;
pushdown(v);
dfs(lson[v]); dfs(rson[v]);
}
pii a[maxn]; int main()
{
//ios::sync_with_stdio(0);
n = rd(); int c, q;
for (int i = 1; i <= n; i++) {
c = rd(); q = rd();
a[i] = make_pair(-q, c);
}
sort(a + 1, a + 1 + n);
m = rd();
for (int i = 1; i <= m; i++) {
val[i] = rd();
rnd[i] = rand();
root = ins(root, i);
}
for (int i = 1; i <= n; i++) {
int c = a[i].second;
int r1 = 0, r2 = 0, r3 = 0, r4 = 0;
split(root, r1, r2, c);
val[r2] -= c; add[r2] -= c;
cnt[r2]++; ans[r2]++;
split(r2, r3, r4, c - 1);
r1 = build(r3, r1);
root = merge(r1, r4);
}
dfs(root);
for (int i = 1; i <= m; i++)printf("%d ", cnt[i]);
return 0;
}

CF702F T-Shirts FHQ Treap的更多相关文章

  1. FHQ Treap及其可持久化与朝鲜树式重构

    FHQ Treap,又称无旋treap,一种不基于旋转机制的平衡树,可支持所有有旋treap.splay等能支持的操作(只有在LCT中会比splay复杂度多一个log).最重要的是,它是OI中唯一一种 ...

  2. fhq treap最终模板

    新学习了fhq treap,厉害了 先贴个神犇的版, from memphis /* Treap[Merge,Split] by Memphis */ #include<cstdio> # ...

  3. NOI 2002 营业额统计 (splay or fhq treap)

    Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...

  4. 【POJ2761】【fhq treap】A Simple Problem with Integers

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  5. 【fhq Treap】bzoj1500(听说此题多码上几遍就能不惧任何平衡树题)

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 15112  Solved: 4996[Submit][Statu ...

  6. 「FHQ Treap」学习笔记

    话说天下大事,就像fhq treap —— 分久必合,合久必分 简单讲一讲.非旋treap主要依靠分裂和合并来实现操作.(递归,不维护fa不维护cnt) 合并的前提是两棵树的权值满足一边的最大的比另一 ...

  7. FHQ Treap摘要

    原理 以随机数维护平衡,使树高期望为logn级别 不依靠旋转,只有两个核心操作merge(合并)和split(拆分) 因此可持久化 先介绍变量 ; int n; struct Node { int v ...

  8. FHQ Treap小结(神级数据结构!)

    首先说一下, 这个东西可以搞一切bst,treap,splay所能搞的东西 pre 今天心血来潮, 想搞一搞平衡树, 先百度了一下平衡树,发现正宗的平衡树写法应该是在二叉查找树的基础上加什么左左左右右 ...

  9. 在平衡树的海洋中畅游(四)——FHQ Treap

    Preface 关于那些比较基础的平衡树我想我之前已经介绍的已经挺多了. 但是像Treap,Splay这样的旋转平衡树码亮太大,而像替罪羊树这样的重量平衡树却没有什么实际意义. 然而类似于SBT,AV ...

  10. 浅谈fhq treap

    一.简介 fhq treap 与一般的treap主要有3点不同 1.不用旋转 2.以merge和split为核心操作,通过它们的组合实现平衡树的所有操作 3.可以可持久化 二.核心操作 代码中val表 ...

随机推荐

  1. Make Cents

    Every year, an elephant qualifies to the Arab Collegiate Programming Competition. He graduated this ...

  2. 关于jquery在页面初始化时radio控件选定默认值的问题

    网上找了很多资料,都是比较旧版本的方法,新版的jquery都已经抛弃了. 正确的代码是 $('input:radio[name="statusRadios"][value=&quo ...

  3. Windchill 设计变更流程卡死查询方法

    设计变更流程卡死查询方法 1. 导出设计变更表单查看填写了“需要”和“是”字眼的文本框  2.打开进程管理器显示流程卡死的地方,确定哪里出错导致没法执行下一步  3.打开设计变更流程图,里面可以查看有 ...

  4. linux加入windows域之完美方案

    运行setup工具 认证配置 选择: “use winbind” “use kerberos” “use winbind authertication” 改为: 删除admin server 其余的改 ...

  5. Gym-101128D:Dice Cup

    题意 给你两个骰子,一个有n面,一个有m面,分别仍一次,求和的概率最大的值 分析 签到题 模拟就行 凑数才把这个题也发到博客上···· #include <cstdio> #include ...

  6. 在aspx页动态加载ascx页面内容,给GridView控件绑定数据

    在aspx页动态加载ascx页面内容 //加载ascx页面内容Control c1 = this.Page.LoadControl("WebUserControl1.ascx"); ...

  7. WDCP/wdlinux安装php_zip扩展教程

    linux服务器安装wdcp之后,php的路径默认是/www/wdlinux/php,有些网友按照网上的教程安装的时候总出错,原因就是php的路径不对,我们知道了php的路径之后就可以开始安装了> ...

  8. Nginx 模块开发

    Nginx 模块概述 Nginx 模块有三种角色: 处理请求并产生输出的 Handler 模块 : 处理由  Handler  产生的输出的 Filter (滤波器)模块: 当出现多个后台 服务器时, ...

  9. Luogu 4473 [国家集训队]飞飞侠

    BZOJ 2143 新技能:并查集优化最短路. 暴力最短路是$O(n^4)$的,然后拿个线段树优化一下连边就$O($能过$)$了. 但是这样都太慢了. 我们考虑一个点如果之前被更新过了,那么之后就不会 ...

  10. 正则表达式复习 (?<=) (?=)

    1.首先值得一说的是"<" 和">" 不是元字符 "."是元字符 ,连接字符"-",即使在字符组内部也不一定 ...