problem D

媽蛋。。贡献了12次wa后终于过了这道水题。。。各种犯2。。。数组竟然少开个0,字母y写成了p。。。。

和别人程序对拍了2个小时。。。 爽。。。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
ll tree[100005<<2];
ll minn(ll a,ll b)
{
    ll ret=a;
    if(b!=-1&&(a==-1||b<a))
        ret=b;
    return ret;
}
ll query(ll L,ll R,ll l,ll r,ll rt)
{
    if(L<=l&&r<=R)
    {
        return tree[rt];
    }
    ll mid=(l+r)>>1;
    if(R<=mid)
        return query(L,R,lson);
    if(L>mid)
        return query(L,R,rson);
    return minn(query(L,R,lson),query(L,R,rson));

}
void update(ll pos,ll val,ll l,ll r,ll rt)
{
    if(l==r)
    {
        tree[rt]=minn(tree[rt],val);
        return ;
    }
    ll mid=(l+r)>>1;
    if(pos<=mid)
        update(pos,val,lson);
    else
        update(pos,val,rson);
    tree[rt]=minn(tree[rt<<1],tree[rt<<1|1]);
    return ;
}
ll mile[100005],cost[100005],price[100005];
ll v2[100005];
ll sum[100005];
int main()
{
 //   freopen("data.in","r",stdin);
    ll fk;
    ll t,i,j,cnt,tot,nowp,nowy,tmp;
    ll ans;
    bool fg;
    cin>>t;
    while(t--)
    {
        cin>>cnt>>tot;
        fg=true;
        for(i=1;i<=cnt;++i)
        {
            cin>>mile[i]>>cost[i]>>price[i];
            if(mile[i]*cost[i]>tot)
                fg=false;
        }
        if(fg==false)
        {
            cout<<"Impossible"<<endl;
            continue;
        }
        sum[cnt+1]=0;
        for(i=cnt;i>=1;--i)
        {
            sum[i]=sum[i+1]+mile[i]*cost[i];
        }
        memset(tree,-1,sizeof(tree));
        for(i=cnt;i>=1;--i)
        {
            tmp=query(1,price[i],1,100005,1);
            if(tmp==-1)
            {
                v2[i]=cnt+1;
            }
            else
            {
                v2[i]=tmp;
            }
            update(price[i],i,1,100005,1);

        }
        ans=0;nowp=1;nowy=0;
        while(nowp<=cnt)
        {
            if(sum[nowp]-sum[v2[nowp]]>tot)
            {
                ans+=price[nowp]*(tot-nowy);
                nowy=tot-mile[nowp]*cost[nowp];
                nowp++;
            }
            else
            {
                if(nowy>=sum[nowp]-sum[v2[nowp]])
                {
                    nowy=nowy-(sum[nowp]-sum[v2[nowp]]);
                    nowp=v2[nowp];
                }
                else
                {
                    ans+=price[nowp]*((sum[nowp]-sum[v2[nowp]])-nowy);
                    nowy=0;
                    nowp=v2[nowp];
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值