@codeforces - 702F@ T-Shirts
@description@
有 n 件 T-shirt,第 i 件 T-shirt 有一个 ci 和 qi,分别表示费用与质量。
同时有 k 个顾客,第 j 个顾客准备了 bj 的金钱去购买 T-shirt。
每个顾客的购买策略是相同的:
他会买他的资金范围内 q 值最大的一件,如果有多个选 c 最小的一件,每种 T-shirt 只买1 次。
重复购买,直到所有的 T-shirt 他都买不起或者他都买过了。
求每位顾客最终可以购买的 T-shirt 数量。
Input
第一行一个整数 n (1 ≤ n ≤ 2·10^5),表示 T-shirt 的种类数。
接下来 n 行,每行两个整数 ci 与 qi (1 ≤ ci, qi ≤ 10^9),含义如上。
接下来一行一个正整数 k (1 ≤ k ≤ 2·10^5) ,表示顾客数。
接下来 k 个正整数 b1, b2, ..., bk (1 ≤ bj ≤ 10^9)。
Output
输出一行 k 个整数,表示第 i 个人买的 T-shirt。
Examples
Input
3
7 5
3 5
4 3
2
13 14
Output
2 3
Input
2
100 500
50 499
4
50 200 150 100
Output
1 2 2 1
@solution@
假如我们把人当作询问,T-shirt 当作元素,则至少我是想不出什么办法快速维护求出询问的。
但是反过来——我们按照 T-shirt 的优先级从大到小排序,把每个 T-shirt 当作一次修改,然后对人进行统一地修改,就很有戏了。
假如一个 T-shirt 的价格是 c,则我们的修改步骤就是先找出 >= c 的人,将这些人所拥有的钱减去 c,将这些人所拥有的 T-shirt 数量加上 1。
貌似可以用平衡树来 merge 和 split。但是注意,我们只能 merge 两个值域没有交集的平衡树,而减去 c 过后可能就产生了交集。
我们将 >= c 的人分为两类。第一类是 >= 2*c 的,这一部分减去 c 依然可以直接 merge;第二类是 < 2*c 的,这一类减去 c 过后只能一个个暴力插入。
因为第二类的人一定满足 c <= x < 2*c,每一次 x 至少折半,这样每个人只会被暴力插入 O(log)
于是就 O(nlog^2n) 的总复杂度。因为是 codeforces 所以能过。
@accepted code@
#include<cstdlib>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 200000;
struct treap{
struct node{
node *ch[2];
int key, tg1;
int sum, tg2;
unsigned int pri;
}pl[MAXN + 5], *ncnt, *NIL;
#define mp make_pair
#define fi first
#define se second
typedef pair<node*, node*> Droot;
unsigned int get_rand() {return (rand() << 16) | rand();}
treap() {
NIL = ncnt = &pl[0];
NIL->ch[0] = NIL->ch[1] = NIL;
NIL->key = NIL->tg1 = NIL->sum = NIL->tg2 = 0;
NIL->pri = -1;
}
node *newnode(int k) {
node *p = (++ncnt);
p->ch[0] = p->ch[1] = NIL;
p->key = k, p->tg1 = p->sum = p->tg2 = 0;
p->pri = get_rand();
return p;
}
void maintain1(node *x, int k) {if( x != NIL ) x->key += k, x->tg1 += k;}
void maintain2(node *x, int k) {if( x != NIL ) x->sum += k, x->tg2 += k;}
void pushdown(node *rt) {
if( rt->tg1 ) {
maintain1(rt->ch[0], rt->tg1), maintain1(rt->ch[1], rt->tg1);
rt->tg1 = 0;
}
if( rt->tg2 ) {
maintain2(rt->ch[0], rt->tg2), maintain2(rt->ch[1], rt->tg2);
rt->tg2 = 0;
}
}
node *merge(node *x, node *y) {
if( x == NIL ) return y;
if( y == NIL ) return x;
pushdown(x), pushdown(y);
if( x->pri < y->pri ) {
x->ch[1] = merge(x->ch[1], y);
return x;
}
else {
y->ch[0] = merge(x, y->ch[0]);
return y;
}
}
Droot split(node *rt, int k) {
if( rt == NIL ) return mp(NIL, NIL);
pushdown(rt);
if( rt->key < k ) {
Droot p = split(rt->ch[1], k);
rt->ch[1] = p.fi; return mp(rt, p.se);
}
else {
Droot p = split(rt->ch[0], k);
rt->ch[0] = p.se; return mp(p.fi, rt);
}
}// < k, >= k
node *insert(node *rt, node *x) {
Droot p = split(rt, x->key);
return merge(p.fi, merge(x, p.se));
}
void push(node *&rt, node *x) {
if( x == NIL ) return ;
pushdown(x);
push(rt, x->ch[0]), push(rt, x->ch[1]);
x->ch[0] = x->ch[1] = NIL;
rt = insert(rt, x);
}
node *update(node *rt, int k) {
Droot p = split(rt, k);
if( p.se == NIL ) return merge(p.fi, p.se);
p.se->key -= k, p.se->tg1 -= k;
p.se->sum += 1, p.se->tg2 += 1;
Droot q = split(p.se, k);
if( q.fi != NIL ) push(p.fi, q.fi);
return merge(p.fi, q.se);
}
void dfs(node *x) {
if( x == NIL ) return ;
pushdown(x);
dfs(x->ch[0]), dfs(x->ch[1]);
}
}T;
treap::node *nd[MAXN + 5], *rt;
struct Tshirt{
int c, q;
Tshirt(int _c=0, int _q=0):c(_c), q(_q) {}
friend bool operator < (Tshirt a, Tshirt b) {
return (a.q == b.q) ? a.c < b.c : a.q > b.q;
}
}t[MAXN + 5];
int ans[MAXN + 5];
int read() {
int x = 0; char ch = getchar();
while( ch > '9' || ch < '0' ) ch = getchar();
while( '0' <= ch && ch <= '9' ) x = 10*x + ch - '0', ch = getchar();
return x;
}
int main() {
int n = read(), k;
srand(n ^ 0307);
for(int i=1;i<=n;i++)
t[i].c = read(), t[i].q = read();
sort(t + 1, t + n + 1);
k = read(), rt = T.NIL;
for(int i=1;i<=k;i++)
rt = T.insert(rt, nd[i] = T.newnode(read()));
for(int i=1;i<=n;i++)
rt = T.update(rt, t[i].c);
T.dfs(rt);
for(int i=1;i<=k;i++)
printf("%d ", nd[i]->sum);
}
@details@
思路本身还是很巧妙的。
而且也印证了非旋 treap 是一种非常优美的数据结构,因为基本上没有什么实现难度。
@codeforces - 702F@ T-Shirts的更多相关文章
- Codeforces 702F - T-shirts(平衡树+势能分析)
题面传送门 首先肯定将所有物品排个序. 考虑暴力做法,对于每个询问,枚举所有物品,能买就买.不过扫一眼就知道无法直接优化. 不妨换个角度,暴力做法是枚举询问,这次我们枚举物品.从左到右依次枚举所有物品 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- JavaScript 实例、构造函数、原型对象关系图
详细介绍:深入理解javascript原型和闭包(5)——instanceof 图片来源:https://www.ibm.com/developerworks/cn/web/1306_jiangjj_ ...
- [jnhs]使用netbeans生成的webapp发布到tomcat是需要改名字的,不然就是404Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
2018-12-21更新 退出tomcat然后删除解压之后的文件夹,然后再启动tomcat也可以解决(安装版tomcat) 2018-12-9更新 有时候这样也可以解决 第一次使用tomcat发布we ...
- 常见任务&基本工具 1 软件包管理
打包系统主要有两个阵营 包文件的简介 Package files are created by a person known as a package maintainer, often (but n ...
- JS Ajax跨域访问
js ajax跨域访问报"No 'Access-Control-Allow-Origin' header is present on the requested resource 如果请求的 ...
- python中操作json
1.导入json包 import json 2.打开json文档 fp = open(jsonpath) 3.读取json文件 data=json.load(fp) 4.获取json的值 data[' ...
- POJ1182 NOI2001 食物链
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 77428 Accepted: 23067 Description ...
- 【教程】5分钟在PAI算法市场发布自定义算法
概述 在人工智能领域存在这样的现象,很多用户有人工智能的需求,但是没有相关的技术能力.另外有一些人工智能专家空有一身武艺,但是找不到需求方.这意味着在需求和技术之间需要一种连接作为纽带. 今天PAI正 ...
- web前端学习(四)JavaScript学习笔记部分(4)-- JavaScriptDOM对象
1.Javascript-DOM简介 1.1.HTML DOM 1.2.DOM操作HTML 1.2.1.JavaScript能够改变页面中的所有HTML元素 1.2.2.JavaScript能够改变页 ...
- void 运算符
void 是 javascript 的操作符,意思是:只执行表达式,但没有返回值.该表达式会被计算但是不会在当前文档处装入任何内容,void其实是javascript中的一个函数,接受一个参数,返回值 ...
- Hdu 3887树状数组+模拟栈
题目链接 Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...