hdu 3634 线段树求矩形面积并
2015-08-09 14:13
411 查看
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> using namespace std; #define L(x) ( x << 1 ) #define R(x) ( x << 1 | 1 ) typedef long long ll; const int N = 1000; int y ; struct Line { int x, y1, y2, flag; } line ; struct Node { int l, r, cover, lf, rf, len; } node ; bool cmp ( Line a, Line b ) { return a.x < b.x; } void length ( int u ) { if ( node[u].cover > 0 ) { node[u].len = node[u].rf - node[u].lf; return; } else if ( node[u].l + 1 == node[u].r ) { node[u].len = 0; } else { node[u].len = node[L(u)].len + node[R(u)].len; } } void build ( int u, int l, int r ) { node[u].l = l; node[u].r = r; node[u].lf = y[l]; node[u].rf = y[r]; node[u].len = node[u].cover = 0; if ( l + 1 == r ) return; int mid = ( l + r ) / 2; build ( L(u), l, mid ); build ( R(u), mid, r ); } void update ( int u, Line e ) { if ( e.y1 == node[u].lf && e.y2 == node[u].rf ) { node[u].cover += e.flag; length ( u ); return; } if ( e.y1 >= node[R(u)].lf ) { update ( R(u), e ); } else if ( e.y2 <= node[L(u)].rf ) { update ( L(u), e ); } else { Line temp = e; temp.y2 = node[L(u)].rf; update ( L(u), temp ); temp = e; temp.y1 = node[R(u)].lf; update ( R(u), temp ); } length ( u ); } const int M = 50; struct Stc { int x1, y1, x2, y2, value; bool operator < ( const Stc & o ) const { return value > o.value; } } stc[M]; int main() { int t; scanf("%d", &t); for ( int _case = 1; _case <= t; _case++ ) { int n; scanf("%d", &n); for ( int i = 1; i <= n; i++ ) { scanf("%d%d%d%d%d", &stc[i].x1, &stc[i].y1, &stc[i].x2, &stc[i].y2, &stc[i].value); } sort( stc + 1, stc + 1 + n ); ll total = 0, pre = 0; for ( int i = 1; i <= n; i++ ) { int p = 1; for ( int j = 1; j <= i; j++ ) { line[p].x = stc[j].x1; line[p].y1 = stc[j].y1; line[p].y2 = stc[j].y2; line[p].flag = 1; y[p] = stc[j].y1; p++; line[p].x = stc[j].x2; line[p].y1 = stc[j].y1; line[p].y2 = stc[j].y2; line[p].flag = -1; y[p] = stc[j].y2; p++; } sort ( line + 1, line + p, cmp ); sort ( y + 1, y + p ); build ( 1, 1, p - 1 ); update ( 1, line[1] ); ll cur = 0; for ( int k = 2; k < p; k++ ) { cur += node[1].len * ( line[k].x - line[k - 1].x ); update ( 1, line[k] ); } total += ( cur - pre ) * stc[i].value; pre = cur; } printf("Case %d: %I64d\n", _case, total); } return 0; }
相关文章推荐
- buffer
- 在建设未来网站
- 关于bgiError:graphics not initialized的问题
- 开发工具之MyEclipse用法基本总结(断点测试,快捷键,Junit测试)
- hash(哈希,散列)
- UVa - 424 Integer Inquiry
- Dcloud开发套件初学
- (medium)LeetCode 207.Course Schedule
- 第20章 多任务和多线程(1)
- 操作系统——中断、异常和系统调用比较
- NYOJ -11--奇偶数分离
- C语言:串匹配的一般方法
- 冷知识:你知道每个视窗都有的 [x] 是怎麽来的吗?
- c++ socket
- 02 俄罗斯方块
- windows下mysql忘记root密码的解决方法
- 安卓程序崩溃时的错误信息log至本地
- 【剑指offer】数字数组中只出现一次(2)
- 空页面的背景提示原理
- 为什么String要设计成不可变的?