媽蛋。。贡献了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;
}