Link
Solution
枚举右端点,然后拿线段树维护选择每个左端点时的答案
Code
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 200010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
struct SegmentTree
{
ll mn[maxn<<2], mx[maxn<<2], sum[maxn<<2], add[maxn<<2], set[maxn<<2], L[maxn<<2], R[maxn<<2];
void maketag_set(ll o, ll v)
{
add[o]=0;
set[o]=v;
mx[o]=mn[o]=v;
sum[o]=(R[o]-L[o]+1)*v;
}
void maketag_add(ll o, ll v)
{
add[o]+=v;
mx[o]+=v, mn[o]+=v;
sum[o]+=(R[o]-L[o]+1)*v;
}
void pushdown(ll o)
{
if(L[o]==R[o])return;
if(~set[o])
{
maketag_set(o<<1,set[o]);
maketag_set(o<<1|1,set[o]);
set[o]=-1;
}
if(add[o])
{
maketag_add(o<<1,add[o]);
maketag_add(o<<1|1,add[o]);
add[o]=0;
}
}
void pushup(ll o)
{
mx[o]=max(mx[o<<1],mx[o<<1|1]);
mn[o]=min(mn[o<<1],mn[o<<1|1]);
sum[o]=sum[o<<1]+sum[o<<1|1];
}
void build(ll o, ll l, ll r, ll* array=NULL)
{
ll mid(l+r>>1);
L[o]=l, R[o]=r;
add[o]=0;
set[o]=-1;
if(l==r)
{
if(array)mn[o]=mx[o]=sum[o]=array[l];
else mn[o]=mx[o]=sum[o]=0;
return;
}
build(o<<1,l,mid,array);
build(o<<1|1,mid+1,r,array);
pushup(o);
}
void Set(ll o, ll l, ll r, ll v)
{
ll mid(L[o]+R[o]>>1);
if(l<=L[o] and r>=R[o]){maketag_set(o,v);return;}
pushdown(o);
if(l<=mid)Set(o<<1,l,r,v);
if(r>mid)Set(o<<1|1,l,r,v);
pushup(o);
}
void Add(ll o, ll l, ll r, ll v)
{
ll mid(L[o]+R[o]>>1);
if(l<=L[o] and r>=R[o]){maketag_add(o,v);return;}
pushdown(o);
if(l<=mid)Add(o<<1,l,r,v);
if(r>mid)Add(o<<1|1,l,r,v);
pushup(o);
}
ll Sum(ll o, ll l, ll r)
{
pushdown(o);
ll mid(L[o]+R[o]>>1), ans(0);
if(l<=L[o] and r>=R[o])return sum[o];
if(l<=mid)ans+=Sum(o<<1,l,r);
if(r>mid)ans+=Sum(o<<1|1,l,r);
return ans;
}
ll Min(ll o, ll l, ll r)
{
ll mid(L[o]+R[o]>>1), ans(linf);
if(l<=L[o] and r>=R[o])return mn[o];
pushdown(o);
if(l<=mid)ans=min(ans,Min(o<<1,l,r));
if(r>mid)ans=min(ans,Min(o<<1|1,l,r));
return ans;
}
ll Max(ll o, ll l, ll r)
{
ll mid(L[o]+R[o]>>1), ans(-linf);
if(l<=L[o] and r>=R[o])return mx[o];
pushdown(o);
if(l<=mid)ans=max(ans,Max(o<<1,l,r));
if(r>mid)ans=max(ans,Max(o<<1|1,l,r));
return ans;
}
}segtree;
ll n, k, l[maxn], r[maxn], p[maxn], bestL, bestR, bestP, id[maxn];
int main()
{
ll i, j;
n=read(), k=read();
rep(i,1,n)l[i]=read(), r[i]=read(), p[i]=read(), id[i]=i;
segtree.build(1,1,2e5);
sort(id+1,id+n+1,[](ll x, ll y){return r[x]<r[y];});
bestP=0;
j=1;
rep(i,1,2e5)
{
segtree.Add(1,1,i,-k);
while(j<=n and r[id[j]]<=i)
{
ll q=id[j];
segtree.Add(1,1,l[q],p[q]);
ll P = segtree.Max(1,1,r[q]);
if(bestP < P)
{
bestP = P;
bestR = i;
}
j++;
}
}
if(bestP==0)
{
printf("0");
return 0;
}
segtree.Set(1,1,2e5,0);
rep(i,1,bestR)segtree.Add(1,1,i,-k);
rep(i,1,n)if(r[i]<=bestR)segtree.Add(1,1,l[i],p[i]);
rep(i,1,bestR)if(segtree.Max(1,i,i)==bestP)bestL=i;
printf("%lld %lld %lld ",bestP,bestL,bestR);
vector<ll> v;
rep(i,1,n)if(l[i]>=bestL and r[i]<=bestR)v.emb(i);
printf("%d\n",v.size());
for(auto x:v)printf("%lld ",x);
return 0;
}