链接

题意: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. 我持续推动Rust语言支持Windows XP系统

    前言 Rust好像长期以来不支持Windows XP系统.有不少用户发帖提议官方支持XP,基本上都被Rust官方开发人员明白的拒绝了.他们的对话大致上是以这种形式開始和结束的(当中乙方代表官方及其拥趸 ...

  2. vim插件之delimitMate.vim

    delimitMate.vim--这个插件主要是在插入模式下,用来自动补全括号.引号等 下载地址 http://www.vim.org/scripts/script.php?script_id=275 ...

  3. Install the IIS 6.0 Management Compatibility Components in Windows 7 or in Windows Vista from Control Panel

    https://technet.microsoft.com/en-us/library/bb397374(v=exchg.80).aspx Install the IIS 6.0 Management ...

  4. POJ 3271 BFS (大坑)

    被某人拉进了坑 完完全全被坑一天的题-- 题意: 正解思路: 先把每一个点搜一遍 预处理出它能在一步之内到的所有点 并连边 然后用一个类似DP的东西把方案数加起来就搞定了 (其实 也不是很难) 但是 ...

  5. 8种提升 ASP.NET Web API 性能的方法

  6. libc.so.6: version GLIBC_2.14 not found

    https://blog.csdn.net/heylun/article/details/78833050

  7. C# async/await异步编程深入理解

    异步函数简介 一般指 async 修饰符声明得.可包含await表达式得方法或匿名函数. 声明方式 异步方法的声明语法与其他方法完全一样, 只是需要包含 async 关键字.async可以出现在返回值 ...

  8. qt hex to dec

    QString s = "32FE12AD";        quint8 u8vlaue = 0;        sscanf(s.toStdString().c_str(),  ...

  9. vue的指令在webstrom下报错

    Preferences -> Editor -> Inspections找到XML,把 Unbound XML namespace prefix的勾去掉

  10. Javascript函数的基本概念+匿名立即执行函数

    函数声明.函数表达式.匿名函数 函数声明:function fnName () {…};使用function关键字声明一个函数,再指定一个函数名,叫函数声明. 函数表达式 var fnName = f ...