HDU 1264 Counting Squares(线段树+面积并)

来源:互联网 发布:bpm软件排名 编辑:程序博客网 时间:2024/06/08 02:58







http://acm.hdu.edu.cn/showproblem.php?pid=1264








题目大意:

RT





分析:

转换了下坐标      左下右上










AC代码:

#include <iostream>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <math.h>#include <vector>#include <stack>#include <queue>#include <map>#include <set>#include<list>#include <bitset>#include <climits>#include <algorithm>#define gcd(a,b) __gcd(a,b)#define mset(a,n) memset(a,n,sizeof(a))#define FINfreopen("input.txt","r",stdin)#define FOUT freopen("output.txt","w",stdout)typedef long long LL;const LL mod=1e9+7;const int INF=0x3f3f3f3f;const int MAX=40000;const double PI=acos(-1.0);using namespace std;struct node{//     存储矩形边的信息int x1,x2,h;int flag;//                    出边还是入边}a[MAX];struct Tree{int l,r;LL len;//    区间长度int s; //                      出边入边}T[MAX];LL ls[MAX];//               离散化bool cmp(node x,node y){return x.h<y.h;}void pushUP(int rt){//        标记上推if(T[rt].s) T[rt].len=ls[T[rt].r+1]-ls[T[rt].l];else if (T[rt].l==T[rt].r) T[rt].len=0;else T[rt].len=T[rt*2].len+T[rt*2+1].len;}void build(int rt,int l,int r){//     建树T[rt].s=0;T[rt].len=0;T[rt].l=l;T[rt].r=r;if (l==r) return ;int mid=(l+r)/2;build(rt*2,l,mid);build(rt*2+1,mid+1,r);}void update(int rt,int l,int r,int e){//      修改区间if (T[rt].l>=l&&T[rt].r<=r){T[rt].s+=e;pushUP(rt);return ;}int mid=(T[rt].l+T[rt].r)/2;if (r<=mid) update(rt*2,l,r,e);//     如果在左侧左侧更新else if (l>mid) update(rt*2+1,l,r,e);//    如果在右侧右侧更新else{//                                两侧都更新update(rt*2,l,mid,e);update(rt*2+1,mid+1,r,e);}pushUP(rt);}int main (){    //FIN;while (1){int len=0;        int n=0;int flag=0;        LL x1,y1,x2,y2;        while (scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2)){            if (x1==-1&&x2==-1&&y1==-1&&y2==-1) break;            if (x1==-2&&x2==-2&&y1==-2&&y2==-2) {flag=1;break;}            //  坐标转换为左下角和右上角if (x1>x2) swap(x1,x2);if (y1>y2) swap(y1,y2);a[len].x1=x1;a[len].x2=x2;a[len].h=y1;a[len].flag=1;ls[len]=x1;len++;a[len].x1=x1;a[len].x2=x2;a[len].h=y2;a[len].flag=-1;ls[len]=x2;len++;n++;        }sort(ls,ls+len);sort(a,a+len,cmp);len=unique(ls,ls+len)-ls;//        去重build(1,0,len-1);LL ans=0;for (int i=0;i<2*n;i++){int l=lower_bound(ls,ls+len,a[i].x1)-ls;            int r=lower_bound(ls,ls+len,a[i].x2)-ls-1;            update(1,l,r,a[i].flag);            ans+=T[1].len*(a[i+1].h-a[i].h);}        printf("%lld\n",ans);        if (flag) break;}return 0;}


原创粉丝点击