http://acm.nuaa.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1040
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; #define MAX 40004 int a[MAX],b[MAX],h[MAX]; int ls[2*MAX],s[2*MAX],num=0,N=0,n; struct node { int l,r,h; }tree[MAX*7]; int cmp(const void *a,const void *b){ return *(int*)a-*(int*)b; } void build(int root,int l,int r) { tree[root].l=l;tree[root].r=r; if(l+1==r)return ; int mid=(l+r)>>1; build(root*2,l,mid); build(root*2+1,mid,r); } void insert(int root,int l,int r,int h) { if(s[tree[root].l]==l && s[tree[root].r]==r){ if(tree[root].h<h)tree[root].h=h;return; } int mid=(tree[root].l+tree[root].r)>>1; if(s[mid]>=r)insert(root*2,l,r,h); else if(s[mid]<=l)insert(root*2+1,l,r,h); else{ insert(2*root,l,s[mid],h); insert(2*root+1,s[mid],r,h); } } long long count(int root,int h){ if(tree[root].h<h)tree[root].h=h; if(tree[root].r-tree[root].l==1) return (long long)(s[tree[root].r]-s[tree[root].l])*tree[root].h; else return count(root*2,tree[root].h)+count(root*2+1,tree[root].h); } int main(int argc, char *argv[]) { scanf("%d",&n); for(int i=0;i<n;++i){ scanf("%d%d%d",&a[i],&b[i],&h[i]); ls[N++]=a[i];ls[N++]=b[i]; } qsort(ls,N,sizeof(int),cmp); for(int i=0;i<N;++i){ s[++num]=ls[i]; while(ls[i+1]==ls[i])i++; } build(1,1,num); for(int i=0;i<n;++i){ insert(1,a[i],b[i],h[i]); } cout<<count(1,0)<<endl; return 0; }