HDU 1828 Picture
- 题意:n个矩形块(有叠加部分),求所有矩形块形成的大的二维图形的周长。
- 思路:首先说求横向边长度 = 当前扫描线覆盖的横向长度 - 上一次扫描线覆盖的横向长度。再就是垂直长度。
新开三个数组
- lc[ rt ]: rt 区间的左端点有没有被扫面线覆盖
- rc[ rt ]: rt 区间的右端点有没有被扫描线覆盖
- num[ rt ]: rt 区间有几条线段
num数组的更新涉及到区间合并:如果左儿子区间的右端点和右儿子区间的左端点都被覆盖了,那么说明两个区间可以合并成一个。所以此时num需要减一
那么垂直长度就是两条扫描线间的高度 * 2 * num[ 1 ]//每个线段有两条边
- 注意:按照y坐标排序时,如果y坐标相同,那么一定要先扫入边,后扫出边。G++过不了就是因为忽略了这个原因,会将实际没用的出边多计算两次贡献。【下面代码最后给的第三组样例就可以把先扫出边、后扫入边的hack掉,可以自己调试蛤儿qwq】
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) x & (-x)
#define MID (l + r ) >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid + 1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define eps 1e-6
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 10000 + 5;
int n;
int ABS(int x)
{
return x >= 0 ? x : -x;
}
struct node{
int lx, rx, high, add;
node() {}
node(int a, int b, int c, int d):lx(a), rx(b), high(c), add(d){}
friend bool operator < (node n1, node n2) { return n1.high == n2.high ? n1.add > n2.add : n1.high < n2.high; }
}info[maxN];
int discx[maxN];
int tree[maxN << 2], cover[maxN << 2];
bool lc[maxN << 2], rc[maxN << 2];
int num[maxN << 2];
void build_tree(int rt, int l, int r)
{
tree[rt] = 0;
cover[rt] = 0;
lc[rt] = rc[rt] = false;
num[rt] = 0;
if(l == r) return ;
int mid = MID;
build_tree(Lson);
build_tree(Rson);
}
void pushup(int rt, int l, int r)
{
if(cover[rt])
{
tree[rt] = discx[r + 1] - discx[l];
lc[rt] = rc[rt] = true;
num[rt] = 1;
}
else if(l == r)
{
tree[rt] = 0;
lc[rt] = rc[rt] = false;
num[rt] = 0;
}
else
{
tree[rt] = tree[lsn] + tree[rsn];
num[rt] = num[lsn] + num[rsn];
lc[rt] = lc[lsn]; rc[rt] = rc[rsn];
if(lc[rsn] && rc[lsn]) num[rt] -- ;
}
}
void update(int rt, int l, int r, int ql, int qr, int val)
{
if(ql <= l && qr >= r)
{
cover[rt] += val;
pushup(rt, l, r);
return ;
}
int mid = MID;
if(qr <= mid) update(QL, val);
else if(ql > mid) update(QR, val);
else { update(QL, val); update(QR, val); }
pushup(rt, l, r);
}
int main()
{
while(~scanf("%d", &n))
{
int tot = 0;
for(int i = 1; i <= n; i ++ )
{
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
info[++ tot] = node(x1, x2, y1, 1);
discx[tot] = x1;
info[ ++ tot] = node(x1, x2, y2, -1);
discx[tot] = x2;
}
sort(info + 1, info + tot + 1);
sort(discx + 1, discx + tot + 1);
int UP = unique(discx + 1, discx + tot + 1) - discx - 1;
build_tree(1, 1, UP - 1);
int ans = 0, last = 0;
for(int i = 1; i <= tot; i ++ )
{
int lx = lower_bound(discx + 1, discx + UP + 1, info[i].lx) - discx;
int rx = lower_bound(discx + 1, discx + UP + 1, info[i].rx) - discx - 1;
update(1, 1, UP - 1, lx, rx, info[i].add);
ans += ABS(tree[1] - last);
last = tree[1];
if(i < tot)
ans += num[1] * 2 * (info[i + 1].high - info[i].high);
}
ans += last;
printf("%d\n", ans);
}
return 0;
}
/*
4
0 0 2 2
1 1 3 3
2 2 4 4
3 3 5 5
ans 20
3
0 2 2 5
1 3 5 7
3 0 6 4
ans 28
2
1 0 2 1
0 1 2 2
ans 8
*/
下面的做法是两边扫描线,一次横向一次纵向,两次加起来就是答案。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) x & (-x)
#define MID (l + r ) >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid + 1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define eps 1e-6
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 10000 + 5;
int n;
struct node{
int lx, rx, high, add;
node() {}
node(int a, int b, int c, int d):lx(a), rx(b), high(c), add(d){}
friend bool operator < (node n1, node n2) { return n1.high == n2.high ? n1.add > n2.add : n1.high < n2.high; }
}info_x[maxN];
struct NODE{
int ly, hy, wide, add;
NODE() {}
NODE(int a, int b, int c, int d):ly(a), hy(b), wide(c), add(d) {}
friend bool operator < (NODE n1, NODE n2) { return n1.wide == n2.wide ? n1.add > n2.add : n1.wide < n2.wide; }
}info_y[maxN];
int discx[maxN];
int discy[maxN];
int tree[maxN << 2], cover[maxN << 2];
void build_tree(int rt, int l, int r)
{
tree[rt] = 0;
cover[rt] = 0;
if(l == r) return ;
int mid = MID;
build_tree(Lson);
build_tree(Rson);
}
void pushup(int rt, int l, int r)
{
if(cover[rt]) tree[rt] = discx[r + 1] - discx[l];
else if(l == r) tree[rt] = 0;
else tree[rt] = tree[lsn] + tree[rsn];
}
void update(int rt, int l, int r, int ql, int qr, int val)
{
if(ql <= l && qr >= r)
{
cover[rt] += val;
pushup(rt, l, r);
return;
}
int mid = MID;
if(qr <= mid) update(QL, val);
else if(ql > mid) update(QR, val);
else { update(QL, val); update(QR, val); }
pushup(rt, l, r);
}
void pushup_y(int rt, int l, int r)
{
if(cover[rt]) tree[rt] = discy[r + 1] - discy[l];
else if(l == r) tree[rt] = 0;
else tree[rt] = tree[lsn] + tree[rsn];
}
void update_y(int rt, int l, int r, int ql, int qr, int val)
{
if(ql <= l && qr >= r)
{
cover[rt] += val;
pushup_y(rt, l, r);
return ;
}
int mid = MID;
if(qr <= mid) update_y(QL, val);
else if(ql > mid) update_y(QR, val);
else { update_y(QL, val); update_y(QR, val); }
pushup_y(rt, l, r);
}
int ansx[maxN], ansy[maxN];
int ABS(int x)
{
return x >= 0 ? x : -x;
}
int main()
{
while(~scanf("%d", &n))
{
int tot = 0;
for(int i = 1; i <= n; i ++ )
{
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
info_x[++ tot] = node(x1, x2, y1, 1);
discx[tot] = x1;
info_y[tot] = NODE(y1, y2, x1, 1);
discy[tot] = y1;
info_x[++ tot] = node(x1, x2, y2, -1);
discx[tot] = x2;
info_y[tot] = NODE(y1, y2, x2, -1);
discy[tot] = y2;
}
sort(info_x + 1, info_x + tot + 1);
sort(discx + 1, discx + tot + 1);
int UP_x = unique(discx + 1, discx + tot + 1) - discx - 1;
build_tree(1, 1, UP_x - 1);
int ans_x = 0;
for(int i = 1; i <= tot; i ++ )
{
int lx = lower_bound(discx + 1, discx + UP_x + 1, info_x[i].lx) - discx;
int rx = lower_bound(discx + 1, discx + UP_x + 1, info_x[i].rx) - discx - 1;
update(1, 1, UP_x - 1, lx, rx, info_x[i].add);
ansx[i] = tree[1];
if(i == 1)
ans_x += ansx[i];
else
ans_x += ABS(ansx[i] - ansx[i - 1]);
}
ans_x += ansx[tot];
sort(info_y + 1, info_y + tot + 1);
sort(discy + 1, discy + tot + 1);
int UP_y = unique(discy + 1, discy + tot + 1) - discy - 1;
build_tree(1, 1, UP_y - 1);
int ans_y = 0;
for(int i = 1; i <= tot; i ++ )
{
int ly = lower_bound(discy + 1, discy + UP_y + 1, info_y[i].ly) - discy;
int hy = lower_bound(discy + 1, discy + UP_y + 1, info_y[i].hy) - discy - 1;
update_y(1, 1, UP_y - 1, ly, hy, info_y[i].add);
ansy[i] = tree[1];
if(i == 1)
ans_y += ansy[i];
else
ans_y += ABS(ansy[i] - ansy[i - 1]);
}
ans_y += ansy[tot];
printf("%d\n", ans_x + ans_y);
}
return 0;
}
/*
4
0 0 2 2
1 1 3 3
2 2 4 4
3 3 5 5
ans 20
3
0 2 2 5
1 3 5 7
3 0 6 4
ans 28
*/
来源:CSDN
作者:Eve_Miracle*
链接:https://blog.csdn.net/weixin_44049850/article/details/104033223