POJ 3277 City Horizon【map模板】【stl练习】

来源:互联网 发布:北京通州淘宝运营培训 编辑:程序博客网 时间:2024/06/06 19:31

http://poj.org/problem?id=3277

题目大意:求N个矩形面积并(底固定)


看到网上许多犇们都是线段树+离散化搞,表示太强了啊,蒟蒻只会map。。。

于是便成了一道MAP模板题,维护当前矩形们的最大高度即可。

不嫌麻烦可以写平衡树。。

#include <map>#include <cstdio>#include <algorithm>#include <functional>using namespace std;typedef long long LL;LL ans;int n, x, y, z, tot, lstpos;map<int, int, greater<int> > M;struct hjl_t { int pos, ins, h; } hjl[80005];bool cmppos(hjl_t a, hjl_t b) { return a.pos < b.pos; }int main() {scanf("%d", &n);for (int i = 1; i <= n; ++i) {scanf("%d %d %d", &x, &y, &z);hjl[++tot].h = z;hjl[tot].pos = x;hjl[tot].ins = 1;//左边hjl[++tot].h = z;hjl[tot].pos = y;hjl[tot].ins = 0;//右边}sort(hjl + 1, hjl + tot + 1, cmppos);for (int i = 1; i <= tot; ++i) {if (!M.empty()) ans += LL(M.begin()->first) * LL(hjl[i].pos - lstpos);//M中的最great的那个元素。。if (hjl[i].ins)  ++M[hjl[i].h];else if (--M[hjl[i].h] == 0) M.erase(hjl[i].h);lstpos = hjl[i].pos;}printf("%lld\n", ans);}


1 0
原创粉丝点击