更新区间时,左闭右开式更新
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
int n,m1=1e9,m2=-1e9,last,ans;
struct node{
int l,r,f,h;
bool operator < (const node &x)const{
if(h!=x.h)return h<x.h;
return f>x.f;
}
}e[maxn*2];
struct NODE{
int l,r,lc,rc,num,len,val;
}tree[maxn<<2];
void pushup(int rt){
if(tree[rt].val){
tree[rt].len=(tree[rt].r-tree[rt].l+1);
tree[rt].lc=tree[rt].rc=1;
tree[rt].num=1;
}
else {
tree[rt].len=tree[rt<<1].len+tree[rt<<1|1].len;
tree[rt].lc=tree[rt<<1].lc;
tree[rt].rc=tree[rt<<1|1].rc;
tree[rt].num=tree[rt<<1].num+tree[rt<<1|1].num-(tree[rt<<1].rc&tree[rt<<1|1].lc);
}
}
void build(int rt,int l,int r){
tree[rt].l=l,tree[rt].r=r;
if(l==r){
return;
}
int mid=(l+r)>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
}
void update(int rt,int x,int y,int val){
if(x<=tree[rt].l&&tree[rt].r<=y){
tree[rt].val+=val;
pushup(rt);
return;
}
int mid=(tree[rt].l+tree[rt].r)>>1;
if(x<=mid)update(rt<<1,x,y,val);
if(y>mid)update(rt<<1|1,x,y,val);
pushup(rt);
}
int main(){
scanf("%d",&n);
int tot=0;
for(int i=1,x,y,x2,y2;i<=n;i++){
scanf("%d%d%d%d",&x,&y,&x2,&y2);
m1=min(m1,min(x,x2));
m2=max(m2,max(x,x2));
tot++;
e[tot].l=x;
e[tot].r=x2;
e[tot].h=y;
e[tot].f=1;
tot++;
e[tot].l=x;
e[tot].r=x2;
e[tot].h=y2;
e[tot].f=-1;
}
sort(e+1,e+tot+1);
build(1,m1,m2-1);
for(int i=0;i<tot;i++){
update(1,e[i].l,e[i].r-1,e[i].f);
ans+=abs(tree[1].len-last);
ans+=(e[i+1].h-e[i].h)*2*tree[1].num;
last=tree[1].len;
}
printf("%d\n",ans);
}
#define mem(a,x) memset(a,x,sizeof(a))
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#include<stdlib.h>
#include<cctype>
#include<string>
using namespace std;
typedef long long ll;
const int N = 111;
struct Edge
{
double l,r;
double h;
int f;
}e[N<<1];
bool cmp(Edge a,Edge b)
{
return a.h < b.h;
}
struct Node
{
int l,r;
int s;
double len;
}q[N*8];
double x[2*N];
#define ls i<<1
#define rs i<<1|1
#define m(i) ((q[i].l + q[i].r)>>1)
void build(int i,int l,int r)
{
q[i].l = l,q[i].r = r;
q[i].s = 0;q[i].len = 0;
if (l == r) return;
int mid = m(i);
build(ls,l,mid);
build(rs,mid+1,r);
}
void pushup(int i)
{
if (q[i].s)
{
q[i].len = x[q[i].r+1] - x[q[i].l];
}
else if (q[i].l == q[i].r)
{
q[i].len = 0;
}
else
{
q[i].len = q[ls].len + q[rs].len;
}
}
void update(int i,int l,int r,int xx)
{
if (q[i].l == l&&q[i].r == r)
{
q[i].s += xx;
pushup(i);
return;
}
int mid = m(i);
if (r <= mid) update(ls,l,r,xx);
else if (l > mid) update(rs,l,r,xx);
else
{
update(ls,l,mid,xx);
update(rs,mid+1,r,xx);
}
pushup(i);
}
int main()
{
int n;int kas = 0;
while (scanf("%d",&n) == 1&&n)
{
int tot = 0;
for (int i = 0;i < n;++i)
{
double x1,x2,y1,y2;
scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
Edge &t1 = e[tot];Edge &t2 = e[1+tot];
t1.l = t2.l = x1,t1.r = t2.r = x2;
t1.h = y1;t1.f = 1;
t2.h = y2;t2.f = -1;
x[tot] = x1;x[tot+1] = x2;
tot += 2;
}
sort(e,e+tot,cmp);
sort(x,x+tot);
int k = 1;
for (int i = 1;i < tot;++i)
{
if (x[i] != x[i-1])
{
x[k++] = x[i];
}
}
build(1,0,k-1);
double ans = 0.0;
for (int i = 0;i < tot;++i)
{
int l = lower_bound(x,x+k,e[i].l) - x;
int r = lower_bound(x,x+k,e[i].r) - x - 1;
update(1,l,r,e[i].f);
ans += (e[i+1].h - e[i].h)*q[1].len;
}
printf("Test case #%d\n",++kas);
printf("Total explored area: %.2f\n\n",ans);
}
return 0;
}