题面传送门
线段树离散或者动态开点都可过。
动态开点线段树只要排个序然后区间覆盖即可。
代码实现:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,k,tot=1;
struct yyy{int x,y;int z;}s[40039];
struct tree{int l,r,f,siz;long long sum;}t[800039];
inline bool cmp(yyy x,yyy y){return x.z>y.z;}
inline void get(int x,int y,int z,int l,int r,int now){
int m=(l+r)>>1;
if(x<=l&&r<=y){t[now].sum+=(long long)(r-l+1-t[now].siz)*z;t[now].f=1;t[now].siz=r-l+1;return;}
if(x<=m){
if(!t[now].l) t[now].l=++tot,get(x,y,z,l,m,t[now].l);
else if(!t[t[now].l].f)get(x,y,z,l,m,t[now].l);
}
if(y>m) {
if(!t[now].r) t[now].r=++tot,get(x,y,z,m+1,r,t[now].r);
else if(!t[t[now].r].f)get(x,y,z,m+1,r,t[now].r);
}
t[now].f=t[t[now].l].f&t[t[now].r].f;
t[now].sum=t[t[now].l].sum+t[t[now].r].sum;
t[now].siz=t[t[now].l].siz+t[t[now].r].siz;
}
int main(){
memset(t,0,sizeof(t));
register int i;
scanf("%d",&n);
for(i=1;i<=n;i++) scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].z);
sort(s+1,s+n+1,cmp);
for(i=1;i<=n;i++)get(s[i].x,s[i].y-1,s[i].z,1,1e9,1);
printf("%lld\n",t[1].sum);
}