Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-dimensional coordinate system on this sheet and drew n black horizontal and vertical segments parallel to the coordinate axes. All segments have width equal to 1 square, that means every segment occupy some set of neighbouring squares situated in one row or one column.
Your task is to calculate the number of painted cells. If a cell was painted more than once, it should be calculated exactly once.
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of segments drawn by Vika.
Each of the next n lines contains four integers x1, y1, x2 and y2 ( - 109 ≤ x1, y1, x2, y2 ≤ 109) — the coordinates of the endpoints of the segments drawn by Vika. It is guaranteed that all the segments are parallel to coordinate axes. Segments may touch, overlap and even completely coincide.
Print the number of cells painted by Vika. If a cell was painted more than once, it should be calculated exactly once in the answer.
3 0 1 2 1 1 4 1 2 0 3 2 3
8
4 -2 -1 2 -1 2 1 -2 1 -1 -2 -1 2 1 2 1 -2
16
In the first sample Vika will paint squares (0, 1), (1, 1), (2, 1), (1, 2), (1, 3), (1, 4), (0, 3) and (2, 3).
题目链接:点击打开链接
给出n条线段, 问这些线段与坐标轴围成的图形面积是多少.
线段树扫描线模板题, 理解线段树扫描线:点击打开链接
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 6e6 + 5;
struct node
{
int l, r, lson, rson, sum;
/* data */
}t[MAXN];
int n, i;
ll ans;
vector<pair<int, pair<int, int> > > hor, ver, magic;
int Build(int l, int r)
{
static int x = 0;
int v = ++x;
t[v].l = l;
t[v].r = r;
t[v].sum = 0;
return v;
}
void Update(int v, int x, int d)
{
t[v].sum += d;
if(t[v].l == t[v].r) return;
int c = t[v].l + (t[v].r - t[v].l) / 2;
if(x <= c) {
if(t[v].lson == 0) t[v].lson = Build(t[v].l, c);
Update(t[v].lson, x, d);
}
else {
if(t[v].rson == 0) t[v].rson = Build(c + 1, t[v].r);
Update(t[v].rson, x, d);
}
}
int Query(int v, int l, int r)
{
if(l <= t[v].l && t[v].r <= r) return t[v].sum;
int res = 0;
if(t[v].lson > 0 && t[t[v].lson].r >= l) res += Query(t[v].lson, l, r);
if(t[v].rson > 0 && t[t[v].rson].l <= r) res += Query(t[v].rson, l, r);
return res;
}
void Merge(vector<pair<int, pair<int, int> > > &v)
{
if(v.size() == 0) return;
sort(v.begin(), v.end());
int k = 1;
for(int i = 1; i < v.size(); ++i) {
if(v[i].first != v[k - 1].first || v[i].second.first > v[k - 1].second.second) {
v[k++] = v[i];
continue;
}
v[k - 1].second.second = max(v[i].second.second, v[k - 1].second.second);
}
v.resize(k);
}
int main(int argc, char const *argv[])
{
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
if(x1 == x2) hor.push_back(make_pair(x1, make_pair(min(y1, y2), max(y1, y2))));
else ver.push_back(make_pair(y1, make_pair(min(x1, x2), max(x1, x2))));
}
Merge(hor), Merge(ver);
for(auto it : hor) {
magic.push_back(make_pair(it.second.first, make_pair(it.first, +1)));
magic.push_back(make_pair(it.second.second + 1, make_pair(it.first, -1)));
ans += it.second.second - it.second.first + 1;
}
sort(magic.begin(), magic.end());
Build(-1e9, 1e9);
for(auto it : ver) {
while(i < magic.size() && magic[i].first <= it.first) {
Update(1, magic[i].second.first, magic[i].second.second);
i++;
}
ans += it.second.second - it.second.first + 1 - Query(1, it.second.first, it.second.second);
}
printf("%lld\n", ans);
return 0;
}