标签:nat tput sam read blank sar should 次数 enter
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 27764 | Accepted: 10003 |
Description
Input
Output
Sample Input
2 10 10 20 20 15 15 25 25.5 0
Sample Output
Test case #1 Total explored area: 180.00
Source
求矩形面积并,扫描线模板题。n这么小,估计是最初的版本。
线段树不打标记维护,每个节点维护len和cnt,表示该节点代表的区间被矩形覆盖的长度为len,该节点自身被覆盖的次数为cnt。这题的题目性质保证了cnt非负。
时间复杂度\(O(n \log n)\)
#include<iostream>
#include<map>
#include<algorithm>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;
co int N=101;
int n,m,num=0;
struct P{
double x,y,z;
int k;
bool operator<(co P&w)co {return x<w.x;}
}a[N*2];
double raw[N*2];
map<double,int> val;
struct T{
int l,r,cnt;
double len;
}t[N*8];
void build(int p,int l,int r){
t[p].l=l,t[p].r=r;
t[p].cnt=0,t[p].len=0;
if(l==r) return;
int mid=(l+r)>>1;
build(p<<1,l,mid),build(p<<1|1,mid+1,r);
}
void change(int p,int l,int r,double k){
if(l<=t[p].l&&t[p].r<=r) return t[p].len=(t[p].cnt+=k)?raw[t[p].r+1]-raw[t[p].l]:(t[p].l==t[p].r?0:t[p<<1].len+t[p<<1|1].len),void();
int mid=t[p].l+t[p].r>>1;
if(l<=mid) change(p<<1,l,r,k);
if(r>mid) change(p<<1|1,l,r,k);
t[p].len=t[p].cnt?raw[t[p].r+1]-raw[t[p].l]:t[p<<1].len+t[p<<1|1].len;
}
void Atlantis(){
for(int i=1;i<=n;++i){
int k=i<<1;
double y,z;
scanf("%lf %lf %lf %lf",&a[k-1].x,&y,&a[k].x,&z);
raw[k-1]=a[k-1].y=a[k].y=y;
raw[k]=a[k-1].z=a[k].z=z;
a[k-1].k=1,a[k].k=-1;
}
n<<=1;
sort(raw+1,raw+n+1);
int m=unique(raw+1,raw+n+1)-raw-1;
for(int i=1;i<=m;++i) val[raw[i]]=i;
sort(a+1,a+n+1);
build(1,1,m-1);
double ans=0;
for(int i=1;i<n;++i){
int y=val[a[i].y],z=val[a[i].z]-1;
change(1,y,z,a[i].k);
ans+=t[1].len*(a[i+1].x-a[i].x);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n",++num,ans);
}
int main(){
// freopen(".in","r",stdin),freopen(".out","w",stdout);
while(read(n)) Atlantis();
return 0;
}
标签:nat tput sam read blank sar should 次数 enter
原文地址:https://www.cnblogs.com/autoint/p/10589026.html