Evanyou Blog 彩带

CF702F T-Shirts

题目描述

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<=2105 ) — 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<=2105 ) — 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=200005;
int n,m,root,cnt,ch[N][2],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=0;char x=getchar();
    while(x<'0'||x>'9') x=getchar();
    while(x>='0'&&x<='9') a=(a<<3)+(a<<1)+(x^48),x=getchar();
    return a;
}

il void down(int rt){
    if(ppx[rt]){
        date[ch[rt][0]]+=ppx[rt],date[ch[rt][1]]+=ppx[rt];
        ppx[ch[rt][0]]+=ppx[rt],ppx[ch[rt][1]]+=ppx[rt];
        ppx[rt]=0;
    }
    if(wmz[rt]){
        num[ch[rt][0]]+=wmz[rt],num[ch[rt][1]]+=wmz[rt];
        wmz[ch[rt][0]]+=wmz[rt],wmz[ch[rt][1]]+=wmz[rt];
        wmz[rt]=0;
    }
}

int merge(int x,int y){
    if(!x||!y) return x+y;
    if(rnd[x]<rnd[y]){
        down(x);
        ch[x][1]=merge(ch[x][1],y);
        return x;
    }
    else{
        down(y);
        ch[y][0]=merge(x,ch[y][0]);
        return y;
    }
}

void split(int rt,int k,int &x,int &y){
    if(!rt) {x=y=0;return;}
    down(rt);
    if(date[rt]<k) x=rt,split(ch[rt][1],k,ch[rt][1],y);
    else y=rt,split(ch[rt][0],k,x,ch[rt][0]);
}

il int ins(int a,int b){
    int x=0,y=0;
    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][0],y);
    y=dfs(ch[x][1],y);
    ch[x][0]=ch[x][1]=0;
    return ins(y,x);
}

void getans(int x){
    if(!x) return;
    down(x);
    getans(ch[x][0]),getans(ch[x][1]);
}

int main(){
    n=gi();
    For(i,1,n) a[i].c=gi(),a[i].q=gi();
    sort(a+1,a+n+1);
    m=gi();
    For(i,1,m) date[i]=gi(),rnd[i]=rand(),root=ins(root,i);
    For(i,1,n) {
        int x=0,y=0,z=0,o=0;
        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-1,z,o);
        x=dfs(z,x);
        root=merge(x,o);
    }
    getans(root);
    For(i,1,m) printf("%d ",num[i]);
    return 0;
}

 

posted @ 2018-09-30 08:29  five20  阅读(244)  评论(0编辑  收藏  举报
Live2D