矩形面积交
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = 2000+10;
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
struct Rec{
double x1, x2, h;
int d;
bool operator < (const Rec& rhs) const{
return h < rhs.h;
}
}rec[maxn<<2];
struct SegTree{
double sum1, sum2;
int cnt;
}tree[maxn<<2];
double cor[maxn<<2];
void pushup(int l,int r,int rt) {
if(tree[rt].cnt >= 2) {
tree[rt].sum1 = tree[rt].sum2 = cor[r+1]-cor[l];
}
else if(tree[rt].cnt == 1) {
if(l == r)
tree[rt].sum2 = 0;
else
tree[rt].sum2 = tree[rt<<1].sum1+tree[rt<<1|1].sum1;
tree[rt].sum1 = cor[r+1]-cor[l];
}
else {
if(l == r)
tree[rt].sum1 = tree[rt].sum2 = 0;
else {
tree[rt].sum1 = tree[rt<<1].sum1+tree[rt<<1|1].sum1;
tree[rt].sum2 = tree[rt<<1].sum2+tree[rt<<1|1].sum2;
}
}
}
void update(int L,int R,int v,int l,int r,int rt) {
if(L > r || R < l) return;
if(L <= l && r <= R) {
tree[rt].cnt += v;
pushup(l,r,rt);
return;
}
int mid = (l+r)>>1;
update(L,R,v,lson);
update(L,R,v,rson);
pushup(l,r,rt);
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
int n;
memset(tree, 0, sizeof tree);
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
double x1,x2,y1,y2;
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
rec[i] = Rec{x1,x2,y1,1};
rec[i+n] = Rec{x1,x2,y2,-1};
cor[i] = x1; cor[i+n] = x2;
}
n <<= 1;
sort(rec+1,rec+1+n);
sort(cor+1,cor+1+n);
int m = unique(cor+1,cor+1+n)-cor-1;
double ans = 0;
for(int i = 1; i < n; ++i) {
int lb = lower_bound(cor+1,cor+1+m,rec[i].x1)-cor;
int rb = lower_bound(cor+1,cor+1+m,rec[i].x2)-cor;
if(lb < rb) update(lb,rb-1,rec[i].d,1,m,1);
ans += tree[1].sum2*(rec[i+1].h-rec[i].h);
}
printf("%.2lf\n", ans);
}
return 0;
}
矩形面积并
#include <bits/stdc++.h>
using namespace std;
const int maxn = 400+10;
const double EPS = 1e-8;
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
struct Rec{
double x1, x2,y;
int d;
Rec(){}
Rec(double x1, double x2, double y, int d):x1(x1),x2(x2),y(y),d(d){}
bool operator < (const Rec& rhs) const {
return y < rhs.y;
}
}r[maxn];
double X[maxn], sum[maxn*4];
int lazy[maxn*4];
int n;
void pushup(int l,int r,int rt) {
if(lazy[rt]) {
sum[rt] = X[r+1]-X[l];
}
else if(l == r) sum[rt] = 0;
else sum[rt] = sum[rt<<1]+sum[rt<<1|1];
}
void update(int L, int R, int v, int l, int r, int rt) {
if(L <= l && r <= R) {
lazy[rt] += v;
pushup(l, r, rt);
return;
}
int m = l + r >> 1;
if(L <= m) update(L, R, v, lson);
if(R > m) update(L, R, v, rson);
pushup(l, r, rt);
}
int getid(double x,int mm) {
return lower_bound(X+1,X+mm+1,x)-X;
}
void solve() {
double x1,y1,x2,y2;
while(scanf("%d", &n) && n){
memset(sum, 0, sizeof sum);
memset(lazy, 0, sizeof lazy);
for(int i = 1; i <= n; ++i) {
scanf("%lf%lf%lf%lf", &x1,&y1,&x2,&y2);
r[i] = Rec(x1,x2,y1,1);
r[i+n] = Rec(x1,x2,y2,-1);
X[i] = x1; X[i+n] = x2;
}
n <<= 1;
sort(r+1,r+n+1);
sort(X+1,X+n+1);
int m = int(unique(X+1,X+n+1)-X-1);
double ans = 0;
for(int i = 1; i < n; ++i) {
int lb = getid(r[i].x1,m);
int rb = getid(r[i].x2,m);
if(lb < rb)
update(lb,rb-1,r[i].d,1,m,1);
ans += sum[1]*(r[i+1].y-r[i].y);
}
printf("%.2lf\n",ans);
}
}
int main() {
solve();
return 0;
}
矩形周长并
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxn = 20000+10;
struct Rec {
int x1,x2,h,d;
bool operator < (const Rec& rhs) const {
return h == rhs.h ? d > rhs.d : h < rhs.h;
}
}rec[maxn<<2];
struct SegTree {
int len;
int num;
bool lv;
bool rv;
int cover;
}Seg[maxn<<2];
void pushup(int l,int r,int rt) {
if(Seg[rt].cover) {
Seg[rt].num = 2;
Seg[rt].len = r-l+1;
Seg[rt].lv=Seg[rt].rv=1;
}
else if(l == r) {
Seg[rt].len = Seg[rt].num = Seg[rt].lv = Seg[rt].rv = 0;
}
else {
Seg[rt].lv = Seg[rt<<1].lv;
Seg[rt].rv = Seg[rt<<1|1].rv;
Seg[rt].len = Seg[rt<<1].len + Seg[rt<<1|1].len;
Seg[rt].num = Seg[rt<<1].num + Seg[rt<<1|1].num;
if(Seg[rt<<1|1].lv&&Seg[rt<<1].rv) Seg[rt].num -= 2;
}
}
void update(int L,int R,int v,int l,int r,int rt) {
if(L <= l && r <= R) {
Seg[rt].cover += v;
pushup(l,r,rt);
return;
}
int mid = (l+r)>>1;
if(L <= mid)update(L,R,v,lson);
if(R > mid)update(L,R,v,rson);
pushup(l,r,rt);
}
int main() {
int n;
while(~scanf("%d", &n)) {
memset(Seg, 0, sizeof Seg);
int lb = 10000, rb = -10000;
for(int i = 1; i <= n; ++i) {
int x1,x2,y1,y2;
scanf("%d%d%d%d", &x1,&y1,&x2,&y2);
rec[i] = Rec{x1,x2,y1,1};
rec[i+n] = Rec{x1,x2,y2,-1};
lb = min(x1,lb);
rb = max(x2,rb);
}
n <<= 1;
sort(rec+1,rec+1+n);
int ans = 0, pre = 0;
for(int i = 1; i <= n; ++i) {
int l = rec[i].x1, r = rec[i].x2;
if(l < r) update(l,r-1,rec[i].d,lb,rb-1,1);
ans += Seg[1].num*(rec[i+1].h-rec[i].h);
ans += abs(Seg[1].len-pre);
pre = Seg[1].len;
}
printf("%d\n", ans);
}
return 0;
}