Practice: Anyway, the length between A and B is only 100, and the length between DP is enough.
However, at the beginning, the DP array is converted into char and Wa is dead.
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <utility>#include <vector>#define LL long long#define it set<LL>::iteratorconst int LMT=55;using namespace std;struct cricu{ LL a,b; int tag,c; bool operator<(const cricu &x)const { return c<x.c; }}cri[LMT];struct line{ bool wid[LMT<<1]; int len;}dp[LMT][LMT];pair<int,int> pre[LMT][LMT][LMT<<1];pair<int,LL>ans;LL k,sum[LMT][LMT][LMT<<1];int can[LMT];vector<pair<int,LL> >vec;int n,m;int main(){ int p=0; LL have=0; int d; ans=make_pair(-1,0); cin>>n>>m>>k; for(int i=1;i<=m;i++) { cin>>cri[i].a>>cri[i].b>>cri[i].c; cri[i].tag=i; } sort(cri+1,cri+1+m); for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) dp[i][j].len=(int)(cri[i].b-cri[i].a+1); for(int j=0;j<dp[i][1].len;j++) dp[i][1].wid[j]=1; for(int j=0;j<dp[i][1].len;j++) sum[i][1][j]=cri[i].a+j; } for(int i=1;i<=m;i++) { if(p<n&&(i==1||cri[i].c>cri[i-1].c))p++; can[i]=p; for(int j=2;j<=can[i];j++) for(int ii=i-1;can[ii]>=j-1&&ii>0;ii--) { if(cri[ii].c<cri[i].c) for(int t=0;t<dp[ii][j-1].len;t++) if(dp[ii][j-1].wid[t]) { if((cri[ii].a+t)+k>=cri[i].a&&cri[ii].a+t+k<=cri[i].b) { d=(int)((cri[ii].a+t)+k-cri[i].a); dp[i][j].wid[d]|=1; if(sum[i][j][d]<sum[ii][j-1][t]+(cri[ii].a+t)+k) { sum[i][j][d]=sum[ii][j-1][t]+(cri[ii].a+t)+k; pre[i][j][d].first=ii; pre[i][j][d].second=t; } } if((cri[ii].a+t)*k>=cri[i].a&&(cri[ii].a+t)*k<=cri[i].b) { d=(int)((cri[ii].a+t)*k-cri[i].a); dp[i][j].wid[d]|=1; if(sum[i][j][d]<sum[ii][j-1][t]+(cri[ii].a+t)*k) { sum[i][j][d]=sum[ii][j-1][t]+(cri[ii].a+t)*k; pre[i][j][d].first=ii; pre[i][j][d].second=t; } } } } for(int t=dp[i][n].len-1;t>=0;t--) if(dp[i][n].wid[t]) { if(have<sum[i][n][t]) { have=sum[i][n][t]; ans.second=t; ans.first=i; } } } if(ans.first==-1)printf("NO\n"); else { int have=n; pair<int,LL> x=ans; printf("YES\n"); while(have) { vec.push_back(make_pair(x.first,x.second+cri[x.first].a)); x=pre[x.first][have][x.second]; have--; } while(!vec.empty()) { cout<<cri[vec.back().first].tag<<" "<<vec.back().second<<endl; vec.pop_back(); } } return 0;}