Gym The 17th Zhejiang Provincial Collegiate Programming Contest E.Easy DP Problem(主席树)

 Gym The 17th Zhejiang Provincial Collegiate Programming Contest E.Easy DP Problem(主席树)

Gym The 17th Zhejiang Provincial Collegiate Programming Contest E.Easy DP Problem(主席树) 

const int N=1e5+5;

    int n,m,_;
    int i,j,k;
    int a[N];
    struct Node
    {
        int l,r;
        int sz;
        ll sum;
    }t[N<<5];
    int root[N],tot=0;
    vector<int> v;

void update(int &x,int y,int l,int r,int pos)
{
    x=++tot;
    t[x]=t[y];
    t[x].sz++;
    t[x].sum+=pos;
    if(l==r) return ;
    else{
        int mid=l+r>>1;
        if(mid>=pos) update(t[x].l,t[y].l,l,mid,pos);
        else update(t[x].r,t[y].r,mid+1,r,pos);
    }
}

ll query(int x,int y,int k,int l,int r)
{
    if(l==r) return l*1ll*k;
    else{
        int mid=l+r>>1;
        int sz=t[t[y].r].sz-t[t[x].r].sz;
        if(sz>=k) return query(t[x].r,t[y].r,k,mid+1,r);
        else return query(t[x].l,t[y].l,k-sz,l,mid)+t[t[y].r].sum-t[t[x].r].sum;
    }
}

int main()
{
    rush(){
        sd(n);
        tot=0;
        for(int i=1;i<=n;i++) sd(a[i]);
        for(int i=1;i<=n;i++) update(root[i],root[i-1],1,1e6,a[i]);
        sd(m);
        for(int i=1;i<=m;i++){
            int l,r,k;
            sddd(l,r,k);
            ll ans=query(root[l-1],root[r],k,1,1e6);
            ll ll=(r-l+1);
            ans+=(ll+1ll)*(2*ll+1ll)*ll/6;
            pll(ans);
        }
    }
    //PAUSE;
    return 0;
}

 

上一篇:The 17th Zhejiang Provincial Contest E. Easy DP Problem(主席树)


下一篇:F. Fair Distribution——The 18th Zhejiang Provincial Collegiate Programming Contest