树状数组2019hdu多校Find the answer

Posted diliiiii

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了树状数组2019hdu多校Find the answer相关的知识,希望对你有一定的参考价值。

题意:每次从1到i-1中选择最少的数减为0,确保1到i的前缀和小于m,我们离散化,从小到大排序,按照输入顺序每遍历一个就插入到树状数组在排序过后的位置,二分位置确保树状数组的前缀和刚好小于/等于m-a[i](当前i必选),因为从小到大排序确保了现有的小的尽量多选,大的都删掉。

#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;

以上是关于树状数组2019hdu多校Find the answer的主要内容,如果未能解决你的问题,请参考以下文章

HDU 2227 Find the nondecreasing subsequences dp思想 + 树状数组

2019 Multi-University Training Contest 3 Find the answer (离散化+二分+树状数组)

HDU 2227 Find the nondecreasing subsequences

hdu多校第三场 1007 (hdu6609) Find the answer 线段树

HDU多校1 - 6959 zoto(莫队+树状数组/值域分块)

2021HDU多校第二场 1004 I love counting 树状数组套01trie