标签:线段树
| Time Limit: 2000MS | Memory Limit: 10000K | |
| Total Submissions: 10727 | Accepted: 5662 |
Description


Input
Output
Sample Input
7 -15 0 5 10 -5 8 20 25 15 -4 24 14 0 -6 16 4 2 15 10 22 30 10 36 20 34 0 40 16
Sample Output
228
Source
/*************************************************************************
> File Name: POJ1177.cpp
> Author: ALex
> Mail: 405045132@qq.com
> Created Time: 2015年01月15日 星期四 15时13分56秒
************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 20110;
struct node
{
int l, r;
int len;
int add;
}tree[N << 2];
struct seg
{
int l, r;
int h;
int flag;
}lines[N << 1], lines2[N << 1];
int cmp (seg a, seg b)
{
if (a.h != b.h)
{
return a.h < b.h;
}
return a.flag > b.flag;
}
void build (int p, int l, int r)
{
tree[p].l = l;
tree[p].r = r;
tree[p].len = 0;
tree[p].add = 0;
if (l == r)
{
return;
}
int mid = (l + r) >> 1;
build (p << 1, l, mid);
build (p << 1 | 1, mid + 1, r);
}
void pushup (int p)
{
if (tree[p].add)
{
tree[p].len = tree[p].r - tree[p].l + 1;
}
else if (tree[p].l == tree[p].r)
{
tree[p].len = 0;
}
else
{
tree[p].len = tree[p << 1].len + tree[p << 1 | 1].len;
}
}
void update (int p, int l, int r, int val)
{
if (l == tree[p].l && r == tree[p].r)
{
tree[p].add += val;
pushup (p);
return;
}
int mid = (tree[p].l + tree[p].r) >> 1;
if (r <= mid)
{
update (p << 1, l, r, val);
}
else if (l > mid)
{
update (p << 1 | 1, l, r, val);
}
else
{
update (p << 1, l, mid, val);
update (p << 1 | 1, mid + 1, r, val);
}
pushup (p);
}
int main()
{
int n;
int x1, y1, x2, y2;
while (~scanf("%d", &n))
{
build (1, 1, N);
for (int i = 1; i <= n; ++i)
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
x1 += 10001;
x2 += 10001;
lines[i].flag = 1;
lines[i].l = x1;
lines[i].r = x2;
lines[i].h = y1;
lines[i + n].flag = -1;
lines[i + n].l = x1;
lines[i + n].r = x2;
lines[i + n].h = y2;
y1 += 10001;
y2 += 10001;
lines2[i].flag = 1;
lines2[i].l = y1;
lines2[i].r = y2;
lines2[i].h = x1;
lines2[i + n].flag = -1;
lines2[i + n].l = y1;
lines2[i + n].r = y2;
lines2[i + n].h = x2;
}
sort (lines + 1, lines + 2 * n + 1, cmp);
sort (lines2 + 1, lines2 + 2 * n + 1, cmp);
int ans = 0;
for (int i = 1; i <= 2 * n; ++i)
{
int last = tree[1].len;
update (1, lines[i].l, lines[i].r - 1, lines[i].flag);
ans += abs(last - tree[1].len);
}
build (1, 1, N);
for (int i = 1; i <= 2 * n; ++i)
{
int last = tree[1].len;
update (1, lines2[i].l, lines2[i].r - 1, lines2[i].flag);
ans += abs(last - tree[1].len);
}
printf("%d\n", ans);
}
return 0;
}标签:线段树
原文地址:http://blog.csdn.net/guard_mine/article/details/42742895