题目链接:http://poj.org/problem?id=3277
题意:给你n个矩形,就面积并
注意输入就好
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define ll long long
using namespace std;
const int maxn = 400000 + 10;
struct node
{
ll x, y, h;
int flag;
} pos[maxn];
int cnt[maxn << 3];
ll sum[maxn << 3],X[maxn];
bool cmp(node a, node b){
return a.h < b.h;
}
void pushup(int cur,int l,int r)
{
if(cnt[cur]) sum[cur] = X[r + 1] - X[l];
else sum[cur] = sum[cur<<1] + sum[cur<< 1 | 1];
}
void update(int l, int r, int val, int cur, int cl,int cr)
{
if(l <= cl && cr <= r)
{
cnt[cur] += val;
pushup(cur, cl, cr);
return ;
}
int mid = cl + cr >> 1;
if(l <= mid)update(l, r, val, cur << 1, cl, mid);
if(r > mid)update(l, r, val, cur << 1 | 1, mid + 1, cr);
pushup(cur, cl, cr);
}
int main()
{
int n;
int t=1;
while(~scanf("%d", &n))
{
memset(cnt, 0, sizeof(cnt));
memset(sum, 0, sizeof(sum));
int xx = 0, hh = 0;
for(int i = 1; i <= n; i++)
{
ll x1, y1, x2, y2;
ll a1, b1, h1;
scanf("%lld%lld%lld", &a1, &b1, &h1);
x1 = a1; x2 = b1;
y1 = 0; y2 = h1;
X[++xx] = x1;
X[++xx] = x2;
pos[++hh] = node{x1, x2, y1, 1};
pos[++hh] = node{x1, x2, y2,-1};
}
sort(X + 1, X + 1 + xx);
sort(pos + 1, pos + 1 + hh, cmp);
xx=unique(X + 1, X + xx + 1) - X - 1;
ll ans = 0;
for(int i = 1; i <= hh; i++){
int l=lower_bound(X + 1, X + xx + 1, pos[i].x) - X;
int r=lower_bound(X + 1, X + xx + 1, pos[i].y) - X;
r--;
update(l, r, pos[i].flag, 1, 1, xx - 1);
ans += sum[1] * (pos[i + 1].h - pos[i].h);
}
printf("%lld\n", ans);
}
return 0;
}