1 #include <iostream>
2 #include <algorithm>
3 #include <cstdio>
4 using namespace std;
5
6 const int MaxSize=90001;
7
8 struct Node
9 { int left,right,mid;
10 int hight;
11 };
12
13
14 struct Building
15 { int left,right,hight;
16 }b[40001];
17 bool cmp(Building a,Building b)
18 { return a.hight>b.hight;}
19
20 Node itree[3*MaxSize];
21
22 void Build(int l,int r,int num)
23 { itree[num].left=l;
24 itree[num].right=r;
25 itree[num].mid=(l+r)/2;
26 itree[num].hight=0;
27
28 if(l+1!=r)
29 { Build(l,itree[num].mid,num<<1);
30 Build(itree[num].mid,r,(num<<1)+1);
31 }
32 }
33
34 void Insert(int l,int r,int h,int num)
35 { if(itree[num].left==l&&itree[num].right==r)
36 { if(h>itree[num].hight)
37 itree[num].hight=h;
38 return;
39 }
40 if(r<=itree[num].mid)
41 Insert(l,r,h,num<<1);
42 else if(l>=itree[num].mid)
43 Insert(l,r,h,(num<<1)+1);
44 else
45 { Insert(l,itree[num].mid,h,num<<1);
46 Insert(itree[num].mid,r,h,(num<<1)+1);
47 }
48 }
49
50
51 int hash[MaxSize];
52
53 long long Calc(int h,int num)
54 { if(h>itree[num].hight)
55 itree[num].hight=h;
56 if(itree[num].left+1==itree[num].right)
57 { return (long long)itree[num].hight*(hash[itree[num].right]-hash[itree[num].left]);
58 }
59 return Calc(itree[num].hight,num<<1)+Calc(itree[num].hight,(num<<1)+1);
60 }
61
62 int BinarySearch(int *from,int *end,int key)
63 { int low=0,high=end-from;
64 int mid=(low+high)/2;
65 while(low<=high)
66 if(from[mid]==key)
67 return mid;
68 else if(from[mid]>key)
69 { high=mid-1;
70 mid=(high+low)/2;
71 }
72 else
73 { low=mid+1;
74 mid=(high+low)/2;
75 }
76 return mid;
77 }
78
79
80 int main()
81 {
82 int N;
83 scanf("%d",&N);
84 for(int i=0;i<N;i++)
85 { scanf("%d%d%d",&b[i].left,&b[i].right,&b[i].hight);
86 hash[i<<1]=b[i].left;
87 hash[(i<<1)+1]=b[i].right;
88 }
89 int hlen=0;
90 sort(hash,hash+2*N);
91 sort(b,b+N,cmp);
92 for(int i=0;i<2*N-1;i++)
93 if(hash[i]!=hash[i+1])
94 hash[++hlen]=hash[i+1];
95 hlen++;
96 Build(0,hlen,1);
97 for(int i=0;i<N;i++)
98 { int l=BinarySearch(hash,hash+hlen,b[i].left);
99 int r=BinarySearch(hash,hash+hlen,b[i].right);
100 Insert(l,r,b[i].hight,1);
101 }
102 cout<<Calc(0,1)<<endl;
103 //printf("%I64d\n",Calc(0,1));
104 return 0;
105 }