题目描述
输入格式
输出格式
样例数据
样例输入
4
2 5 1
9 10 4
6 8 2
4 6 3
样例输出
16
样例说明
题目分析
离散化+线段树(维护最大值)
事先拍一遍序,以免出蜜汁错误
源代码
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
inline const LL Get_Int() {
LL num=0,bj=1;
char x=getchar();
while(x<'0'||x>'9') {
if(x=='-')bj=-1;
x=getchar();
}
while(x>='0'&&x<='9') {
num=num*10+x-'0';
x=getchar();
}
return num*bj;
}
const int maxn=100000;
struct Tree { //修改区间 查询点
LL left,right,height;
};
struct Segment_Tree { //统计标记次数(颜色有用么)
Tree tree[maxn*4];
LL sum;
void build(int index,int Left,int Right) {
tree[index].left=Left;
tree[index].right=Right;
tree[index].height=0;
if(Left==Right)return;
int mid=(Left+Right)/2;
build(2*index,Left,mid);
build(2*index+1,mid+1,Right);
}
void push_down(int index) {
if(tree[index].height<=0)return;
tree[index*2].height=tree[index*2+1].height=tree[index].height;
tree[index].height=-1;
}
void push_up(int index) {
if(tree[index*2].height==tree[index*2+1].height)tree[index].height=tree[index*2].height;
}
void modify(int index,int Left,int Right,int height) {
if(Right<tree[index].left||Left>tree[index].right)return; //不相交
if(Left<=tree[index].left&&Right>=tree[index].right) { //完全包含
tree[index].height=height;
return;
}
push_down(index);
modify(index*2,Left,Right,height);
modify(index*2+1,Left,Right,height);
push_up(index);
}
void init() {
sum=0;
}
void Query(int index,int Left,int Right,int* b) {
if(Right<tree[index].left||Left>tree[index].right)return; //不相交
if(tree[index].height>0) { //完全包含
sum+=tree[index].height*(long long)(b[tree[index].right+1]-b[tree[index].left]);
return;
}
Query(index*2,Left,Right,b);
Query(index*2+1,Left,Right,b);
}
};
Segment_Tree st;
struct Building {
int start,end,height;
bool operator < (const Building& b) const {
return height<b.height;
}
} b[40005];
int n,a[100005];
int Discretization(int* a,int n) {
sort(a+1,a+n+1);
int cnt=unique(a+1,a+n+1)-a-1;
for(int i=1; i<=n; i++) {
b[i].start=lower_bound(a+1,a+cnt+1,b[i].start)-a;
b[i].end=lower_bound(a+1,a+cnt+1,b[i].end)-a;
}
return cnt;
}
int main() {
n=Get_Int();
for(int i=1; i<=n; i++) {
b[i].start=Get_Int();
b[i].end=Get_Int();
b[i].height=Get_Int();
a[i]=b[i].start;
a[i+n]=b[i].end;
}
int cnt=Discretization(a,n*2);
sort(b+1,b+n+1);
st.build(1,1,cnt);
for(int i=1; i<=n; i++)st.modify(1,b[i].start,b[i].end-1,b[i].height);
st.init();
st.Query(1,1,cnt,a);
printf("%lld\n",st.sum);
return 0;
}