2019牛客暑期多校训练营(第九场)H Cutting Bamboos(主席树+二分)

题意:n个竹子,有高度,q次询问,询问之间是独立的,每次查询输入l,r,x,y代表砍区间[l,r]]内的竹子砍y次,最后一次要砍成0,每次砍掉的总长度相同,问第x次砍的高度是多少。

 

既然每次要求砍掉的东西都相同,那么就可以直接算出来砍第x次需要砍掉多少(sumh(l~r)/y*x),然后只需要二分这个高度,在主席树中查找大于等于这个高度的竹子总和减去个数乘以高度即可。

因为主席树的本质是由多颗权值线段树

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mod = 998244353;
const int M = 1000000 + 10;
const int N = 200000 + 10;
ll pre[N];
struct no{
    int ls,rs;
    ll sum,num;
}tree[N<<5];
int rt[N],cnt;
void build(int &now , int l , int r){
    now=++cnt;
    tree[now].num=tree[now].sum=0;
    if(l==r) return ;
    int mid=(l+r)>>1;
    build(tree[now].ls,l,mid);
    build(tree[now].rs,mid+1,r);
}
void update(int &now, int pre, int val, int l, int r){
    now=++cnt;
    tree[now]=tree[pre];
    if(l==r){
        tree[now].num++;
        tree[now].sum+=val;
        return ;
    }
    int mid=(l+r)>>1;
    if(val<=mid) update(tree[now].ls,tree[pre].ls,val,l,mid);
    else update(tree[now].rs,tree[now].rs,val,mid+1,r);
    tree[now].num = tree[tree[now].ls].num + tree[tree[now].rs].num;
    tree[now].sum = tree[tree[now].ls].sum + tree[tree[now].rs].sum;
}
ll query_num(int p, int q, int L, int R, int l, int r){
    if(L <= l && r <= R) return tree[q].num - tree[p].num;
    int mid = (l + r) >> 1;
    ll ans = 0;
    if(L <= mid) ans += query_num(tree[p].ls, tree[q].ls, L, R, l, mid);
    if(mid < R) ans += query_num(tree[p].rs, tree[q].rs, L, R, mid + 1, r);
    return ans;
}
ll query_sum(int p, int q, int L, int R , int l , int r)
{
    if(L <= l && r <= R) return tree[q].sum - tree[p].sum;
    int mid = (l + r) >> 1;
    ll  ans = 0;
    if(L <= mid) ans += query_sum(tree[p].ls, tree[q].ls, L, R, l, mid);
    if(mid < R) ans += query_sum(tree[p].rs, tree[q].rs, L, R, mid + 1, r);
    return ans;
}
int main(){
    int n,q;
    scanf("%d%d",&n,&q);
    build(rt[0],1,N);
    for(int i=1 ; i<=n ; i++){
        ll h;
        scanf("%lld",&h);
        pre[i]=pre[i-1]+h;
        update(rt[i],rt[i-1],h,1,N);
    }
    while(q--){
        ll l,r,x,y;
        scanf("%lld%lld%lld%lld",&l,&r,&x,&y);
        double ql=0,qr=N;
        while(qr-ql>1e-7){
            double mid=(ql+qr)/2;
            ll qmid=ceil(mid);
            ll qsum=query_sum(rt[l-1],rt[r],qmid,N,1,N);
            ll qnum=query_num(rt[l-1],rt[r],qmid,N,1,N);
            if(qsum-qnum*mid>=1.0*(pre[r]-pre[l-1])*x/y) ql=mid;
            else qr=mid;
        }
        printf("%.10lf\n", qr);
    }
    return 0;
}
View Code

 

posted @ 2019-08-21 15:32  shuai_hui  阅读(139)  评论(0编辑  收藏  举报