题目描述
Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silhouettes formed by the rectangular buildings.
The entire horizon is represented by a number line with N (1 ≤ N ≤ 40,000) buildings. Building i's silhouette has a base that spans locations Ai through Bi along the horizon (1 ≤ Ai < Bi ≤ 1,000,000,000) and has height Hi (1 ≤ Hi ≤ 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by all N buildings.
有一个数列,初始值均为0,他进行N次操作,每次将数列[ai,bi)这个区间中所有比Hi小的数改为Hi,他想知道N次操作后数列中所有元素的和。
输入输出格式
输入格式:
第一行一个整数N,然后有N行,每行三个正整数ai、bi、Hi。
输出格式:
一个数,数列中所有元素的和。
输入输出样例
4 2 5 1 9 10 4 6 8 2 4 6 3
16
说明
N<=40000 , a、b、k<=10^9 。
线段树省选题。。。
附上丧心病狂的代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#define LSON rt<<1
#define RSON rt<<1|1
#define DATA(x) a[x].data
#define LSIDE(x) a[x].l
#define RSIDE(x) a[x].r
#define WIDTH(x) (lsh[RSIDE(x)]-lsh[LSIDE(x)])
#define MAXN 40010
using namespace std;
int n,c=1;
long long ans=0,lsh[MAXN<<1];
struct node1{
long long data;
int l,r;
}a[MAXN<<3];
struct node2{
int left,right,h;
}b[MAXN];
inline int read(){
int date=0,w=1;char c=0;
while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
return date*w;
}
bool cmp(const node2 &x,const node2 &y){
return x.h<y.h;
}
int find(int x){
int l=1,r=2*n,mid;
while(l<=r){
mid=l+r>>1;
if(lsh[mid]==x)return mid;
else if(lsh[mid]>x)r=mid-1;
else l=mid+1;
}
return 0;
}
void pushdown(int rt){
DATA(LSON)=DATA(rt);
DATA(RSON)=DATA(rt);
DATA(rt)=0;
}
void buildtree(int l,int r,int rt){
int mid;
LSIDE(rt)=l;
RSIDE(rt)=r;
DATA(rt)=0;
if(l==r-1)return;
mid=l+r>>1;
buildtree(l,mid,LSON);
buildtree(mid,r,RSON);
}
void update(int l,int r,long long c,int rt){
int mid;
if(l>RSIDE(rt)||LSIDE(rt)>r)return;
if(l<=LSIDE(rt)&&RSIDE(rt)<=r){
DATA(rt)=c;
return;
}
if(DATA(rt))pushdown(rt);
mid=LSIDE(rt)+RSIDE(rt)>>1;
if(r<=mid)update(l,r,c,LSON);
else if(mid<=l)update(l,r,c,RSON);
else{
update(l,r,c,LSON);
update(l,r,c,RSON);
}
}
void query(int rt){
if(DATA(rt)){
ans+=WIDTH(rt)*DATA(rt);
return;
}
if(LSIDE(rt)==RSIDE(rt)-1)return;
query(LSON);
query(RSON);
}
int main(){
n=read();
buildtree(1,n*2,1);
for(int i=1;i<=n;i++){
b[i].left=read();b[i].right=read();b[i].h=read();
lsh[c++]=b[i].left;
lsh[c++]=b[i].right;
}
sort(lsh+1,lsh+2*n+1);
sort(b+1,b+n+1,cmp);
for(int i=1;i<=n;i++){
int x=find(b[i].left),y=find(b[i].right);
update(x,y,b[i].h,1);
}
query(1);
printf("%lld\n",ans);
return 0;
}