因为是只有最后询问一次和,所以可以离散化之后,区间更新最大值,线段树维护即可。叶子节点l表示[ a[l],a[l+1] )。答案就是每个点最后的值乘上这个点所代表的区间。
#include
using namespace std;
#define N 40010
#define inf 0x3f3f3f3f
#define ll long long
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
int n,a[N<<1],m=0;
ll ans=0;
struct que{
int l,r,k;
}q[N];
inline bool cmp(que x,que y){return x.kstruct node{
int mx,lazy;
}tree[N<<3];
inline void pushup(int p){
tree[p].mx=max(tree[p<<1].mx,tree[p<<1|1].mx);
}
inline void pushdown(int p){
if(!tree[p].lazy) return;
tree[p<<1].lazy=tree[p].lazy;tree[p<<1|1].lazy=tree[p].lazy;
tree[p<<1].mx=tree[p].lazy;tree[p<<1|1].mx=tree[p].lazy;tree[p].lazy=0;
}
void build(int p,int l,int r){
if(l==r) return;
int mid=l+r>>1;
build(p<<1,l,mid);build(p<<1|1,mid+1,r);
}
void change(int p,int l,int r,int x,int y,int val){
if(x<=l&&r<=y){tree[p].mx=val;tree[p].lazy=val;return;}
int mid=l+r>>1;pushdown(p);
if(x<=mid) change(p<<1,l,mid,x,y,val);
if(y>mid) change(p<<1|1,mid+1,r,x,y,val);
pushup(p);
}
int ask(int p,int l,int r,int x){
if(tree[p].mx==0) return 0;
if(l==r) return tree[p].mx;
int mid=l+r>>1;pushdown(p);
if(x<=mid) return ask(p<<1,l,mid,x);
else return ask(p<<1|1,mid+1,r,x);
}
int main(){
// freopen("a.in","r",stdin);
n=read();for(int i=1;i<=n;++i){
a[++m]=q[i].l=read();a[++m]=q[i].r=read();q[i].k=read();
}sort(a+1,a+m+1);m=unique(a+1,a+m+1)-a-1;
build(1,1,m-1);sort(q+1,q+n+1,cmp);
for(int i=1;i<=n;++i)
change(1,1,m-1,lower_bound(a+1,a+m+1,q[i].l)-a,lower_bound(a+1,a+m+1,q[i].r)-a-1,q[i].k);
for(int i=1;i<=m-1;++i)
ans+=(ll)(a[i+1]-a[i])*ask(1,1,m-1,i);
printf("%lld\n",ans);
return 0;
}