RT,第二个样例只有第三个测试点没过。
#include<bits/stdc++.h>
#define int long long
#define mid (l+(r-l>>1))
#define ls (mid<<1)
#define rs (mid<<1|1)
using namespace std;
const int N=6e5+5,inf=1e9;
int c,t,n,m,k,d,ans;
int b[N],L,R,K;
struct node{
int l,r,w;
bool operator<(const node &t)
const{return r==t.r?l<t.l:r<t.r;}
}nd[N];
struct Tree{
int w,fl;
}tr[N<<1];
inline void chgw(int x,int k){tr[x].w+=k,tr[x].fl+=k;}
inline void pushdown(int l,int r,int id){
int x=tr[id].fl;
if(x)chgw(ls,x),chgw(rs,x),tr[id].fl=0;
}
void upd(int l,int r,int id){
if(L<=l&&r<=R)return chgw(id,K);
pushdown(l,r,id);
if(L<=mid)upd(l,mid,ls);
if(mid<R)upd(mid+1,r,rs);
tr[id].w=max(tr[ls].w,tr[rs].w);
}
signed main(){
// freopen("run2.in","r",stdin);
// freopen("run.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>c>>t;while(t--){
cin>>n>>m>>k>>d;n=0;
for(int i=1; i<=m; ++i)
cin>>nd[i].r>>nd[i].l>>nd[i].w,
nd[i].l=nd[i].r-nd[i].l+1,
b[++n]=nd[i].l,b[++n]=nd[i].r;
for(int i=1,t=n; i<=t; ++i)
b[++n]=b[i]-1,b[++n]=b[i]+1;
sort(b+1,b+1+n);
n=unique(b+1,b+1+n)-b-1;
for(int i=1; i<=m; ++i)
nd[i].l=lower_bound(b+1,b+1+n,nd[i].l)-b,
nd[i].r=lower_bound(b+1,b+1+n,nd[i].r)-b;
sort(nd+1,nd+1+m);
for(int i=1,t; i<=m; ++i){
t=lower_bound(b+1,b+1+n,b[nd[i].r]-k+1)-b;
for(int j=max(nd[i-1].r+1,t); j<=nd[i].r; ++j){
L=R=j+1,K=max(tr[1].w,0ll);if(R<=n)upd(1,n,1);
L=t,R=j-1,K=-d*(b[j]-b[j-1]);
if(L<=R)upd(1,n,1);
L=R=j,K=-d,upd(1,n,1);
ans=max(ans,tr[1].w);
}
L=t,R=nd[i].l,K=nd[i].w;if(L<=R)upd(1,n,1);
ans=max(ans,tr[1].w);
}cout<<ans<<'\n';ans=0;++n;
memset(tr,0,sizeof*tr*n<<1);
}
return 0;
}