链接

题意:N个矩形块,交求面积并.

题解

显然对于每个 \(x\),只要求出这个 \(x\) 上面最高的矩形的高度,即最大值

将矩形宽度离散化一下,高度从小到大排序,线段树区间set,然后求和即可

注意它给的矩形区间是 \([L,R)\) 这样在线段树里直接维护 \([L,R)\) 这样的区间即可

#include<bits/stdc++.h>
#define REP(i,a,b) for(int i(a);i<=(b);++i)
using namespace std;
typedef long long ll;
inline int read(){char c;int w;
while(!isdigit(c=getchar()));w=c&15;
while(isdigit(c=getchar()))w=w*10+(c&15);return w;
}
inline char smax(int&x,const int&y){return x<y?x=y,1:0;}
inline char smin(int&x,const int&y){return x>y?x=y,1:0;}
const int N=40005;
struct data{int l,r,h;}a[N];
inline bool cmp(data a,data b){return a.h<b.h;}
int n,b[N<<1],m,tag[N<<3];
ll sum[N<<3];
#define ls o<<1
#define rs o<<1|1
inline void update(int o,int l,int r,int x,int y,int z){
assert(l>=1&&r<=m);
if(x<=l&&r<=y){tag[o]=z,sum[o]=1ll*z*(b[r]-b[l]);return;}
int mid=l+r>>1;
if(tag[o]){
tag[ls]=tag[rs]=tag[o];
sum[ls]=1ll*tag[o]*(b[mid]-b[l]);
sum[rs]=1ll*tag[o]*(b[r]-b[mid]);
tag[o]=0;
}
if(x<mid)update(ls,l,mid,x,y,z);
if(y>mid)update(rs,mid,r,x,y,z);
sum[o]=sum[ls]+sum[rs];
}
signed main(){
n=read();
REP(i,1,n)a[i]=(data){read(),read(),read()},b[++m]=a[i].l,b[++m]=a[i].r;
sort(a+1,a+1+n,cmp);sort(b+1,b+1+m);m=unique(b+1,b+1+m)-b-1;
REP(i,1,n){
a[i].l=lower_bound(b+1,b+1+m,a[i].l)-b;
a[i].r=lower_bound(b+1,b+1+m,a[i].r)-b;
update(1,1,m,a[i].l,a[i].r,a[i].h);
}
printf("%lld\n",sum[1]);
return 0;
}

[BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 线段树的更多相关文章

  1. bzoj1645 [Usaco2007 Open]City Horizon 城市地平线

    Description Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at ...

  2. 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树

    [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...

  3. 1645: [Usaco2007 Open]City Horizon 城市地平线

    1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 315  Solved: ...

  4. BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线

    BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线 Description N个矩形块,交求面积并. Input * Line 1: A single i ...

  5. 【BZOJ】1645: [Usaco2007 Open]City Horizon 城市地平线(线段树+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1645 这题的方法很奇妙啊...一开始我打了一个“离散”后的线段树.............果然爆了. ...

  6. BZOJ 1645: [Usaco2007 Open]City Horizon 城市地平线 扫描线 + 线段树 + 离散化

    Code: #include<cstdio> #include<algorithm> #include<string> #define maxn 1030000 # ...

  7. bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线【线段树+hash】

    bzoj题面什么鬼啊-- 题目大意:有一个初始值均为0的数列,n次操作,每次将数列(ai,bi-1)这个区间中的数与ci取max,问n次后元素和 离散化,然后建立线段树,每次修改在区间上打max标记即 ...

  8. [POJ] 3277 .City Horizon(离散+线段树)

    来自这两篇博客的总结 http://blog.csdn.net/SunnyYoona/article/details/43938355 http://m.blog.csdn.net/blog/mr_z ...

  9. 【BZOJ】1628 && 1683: [Usaco2007 Demo]City skyline 城市地平线(单调栈)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1628 http://www.lydsy.com/JudgeOnline/problem.php?id ...

随机推荐

  1. Httphelper头信息(ContentType)默认为text/html无懈可击

    Httphelper头信息(ContentType)默认为text/html无懈可击转 http://www.sufeinet.com/thread-8623-1-1.html 我发现最近有几个网友提 ...

  2. 洛谷 P1443 马的遍历

    P1443 马的遍历 题目描述 有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步 输入输出格式 输入格式: 一行四个数据,棋盘 ...

  3. hdu 1575 矩阵连乘2

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  4. 进程:linux用户态-内核态

    用户态:Ring3运行于用户态的代码则要受到处理器的诸多检查,它们只能访问映射其地址空间的页表项中规定的在用户态下可访问页面的虚拟地址,且只能对任务状态段(TSS)中I/O许可位图(I/O Permi ...

  5. Android 使用DrawerLayout高速实现側滑菜单

    一.概述 DrawerLayout是一个能够方便的实现Android側滑菜单的组件,我近期开发的项目中也有一个側滑菜单的功能.于是DrawerLayout就派上用场了.假设你从未使用过DrawerLa ...

  6. Linux能ping通IP,ping不通域名

    今天碰到个问题, 能ping通IP地址, ping不通域名, 一直以为是 DNS解析服务器的问题, 找了半天. 问题不在这里. [root@www postfix]# cat /etc/resolv. ...

  7. 八款常用的 Python GUI 开发框架推荐

    作为Python开发者,你迟早都会用到图形用户界面来开发应用.本文将推荐一些 Python GUI 框架,希望对你有所帮助,如果你有其他更好的选择,欢迎在评论区留言. Python 的 UI 开发工具 ...

  8. 酱油记:GDKOI2018

    GDKOI2018,走出机房的第六场考试 DAY0 这一次GDKOI,第一次在广州二中考,第一次住在柏高酒店(住宿条件杠杠的!),晚上就到对面的万达广场吃了顿烤肉,到老师那里开会,然后就回酒店睡了 D ...

  9. 124.C++输出小结

    #include <iostream> #include <iomanip> using namespace std; void main() { ////调用cout的成员函 ...

  10. P3507 [POI2010]GRA-The Minima Game

    题目描述 Alice and Bob learned the minima game, which they like very much, recently. The rules of the ga ...