Fenwick tree [] [] 2019hdu multi-school Find the answer

The meaning of problems: every 1 to i-1 from the least number of selection reduced to 0, and to ensure a prefix to less than 1 m i, we discretization, from small to large, in accordance with the input sequence is inserted into a tree traversal each after sorting position array, the two arrays location prefix tree and ensure just less than / equal to mA [i] (i currently required), since the small to large to ensure that as much of the existing small election, are large deleted.

#include<bits/stdc++.h>
#define ll long long 
#define ull unsigned long long
using namespace std;
const int N = 200000 + 5;
int q,n;
ll m;
struct node
{
    int num;
    ll sum;
}c[200005];
struct nod
{
    int id;
    ll w;
}a[200005];
int mp[200005];
bool cmp(nod a,nod b)
{
    if(a.w==b.w)
        return a.id<b.id;
    return a.w<b.w;
}
void add(int x,ll y)
{
    for(;x<=n;x+=x&-x)
    {
        c[x].sum+=y;
        c[x].num++;
    }
}
ll ask_sum(int x)
{
    ll ret=0;
    for(;x;x-=x&-x)
    {
        ret+=c[x].sum;
        //cout<<x<<endl;
    }
    return ret;
}
ll ask_num(int x)
{
    ll ret=0;
    for(;x;x-=x&-x)
    {
        ret+=c[x].num;
    }
    return ret;
}
int half_find(ll x)
{
    int l=0,r=n;
    int mid=(l+r)>>1;
    while(l<=r)
    {
        //cout<<mid<<endl;
        mid=(l+r)>>1;
        if(ask_sum(mid)>x)
        {
            r=mid-1;
        }
        else
        {
            l=mid+1;
        }
        //cout<<l<<" "<<r<<" "<<mid<<endl;
    }
    //cout<<r<<endl;
    return ask_num(r);
}
int main()
{
    int x=3;
    //for(;x;x-=x&-x)
        //cout<<x<<endl;
    scanf("%d",&q);
    //cout<<ask_num(7)<<endl;
    while(q--)
    {
        scanf("%d %lld",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i].w);
            a[i].id=i;
            c[i].num=c[i].sum=0;
        }
        sort(a+1,a+n+1,cmp);
        for(int i=1;i<=n;i++)
        {
            //cout<<a[i].id<<endl;
            mp[a[i].id]=i;
        }
        printf("0");
        add(mp[1],a[mp[1]].w);
        for(int i=2;i<=n;i++)
        {
            printf(" %lld",i-1-half_find(m-a[mp[i]].w));
            add(mp[i],a[mp[i]].w);
        }
        printf("\n");
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Diliiiii/p/11266167.html