Rectangles Area Sum

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<math.h>
 4 #include<string.h>
 5 #include<algorithm>
 6 using namespace std;
 7 #define re(i,n) for(int i=0;i<n;i++)
 8 const int maxn=1e4+7;
 9 const int mod=1e9+7;
10 typedef long long ll;
11 int x[maxn*2],y[maxn*2],xsz,ysz;
12 struct Node{
13     int fy,ty;
14     int x;
15     int type;
16 }a[maxn*2];
17 bool cmp(const Node&m,const Node&n){
18     return m.x<n.x;
19 }
20 struct Data{
21     int cnt,len;
22 }da[maxn*16];
23 int fy,ty,type;
24 void insert(int id,int f,int t){
25     //printf("inserting node %d ,from %d,to %d,fy %d ,ty %d,type %d\n",id,y[f],y[t],fy,ty,type);
26     if(fy<=y[f]&&ty>=y[t]){
27         da[id].cnt+=type;
28         if(da[id].cnt==0){
29             if(f==t)da[id].len=0;
30             else da[id].len=da[id<<1].len+da[id<<1|1].len;
31         }else da[id].len=y[t]-y[f];
32         return;
33     }
34     int mid=(f+t)>>1;
35     if(ty>y[mid])insert(id<<1|1,mid,t);
36     if(fy<y[mid])insert(id<<1,f,mid);
37     if(da[id].cnt==0)da[id].len=da[id<<1].len+da[id<<1|1].len;
38     else da[id].len=y[t]-y[f];
39 }
40 int main(){
41     int n;
42     while(cin>>n&&n){
43         re(i,n){
44             scanf("%d%d%d%d",&x[i*2],&y[i*2],&x[i*2+1],&y[i*2+1]);
45             a[i*2].fy=a[i*2+1].fy=min(y[i*2],y[i*2+1]);
46             a[i*2].ty=a[i*2+1].ty=max(y[i*2],y[i*2+1]);
47             a[i*2].x=min(x[i*2],x[i*2+1]);
48             a[i*2+1].x=max(x[i*2],x[i*2+1]);
49             a[i*2].type=1;
50             a[i*2+1].type=-1;
51         }
52         sort(y,y+n*2),ysz=unique(y,y+n*2)-y;
53         sort(a,a+n*2,cmp);
54         ll ans=0;
55         memset(da,0,sizeof(da));
56         fy=a[0].fy,ty=a[0].ty,type=a[0].type;
57         insert(1,0,ysz-1);
58         //cout<<"The first time "<<da[1].cnt<<endl;
59         //cout<<"ans is "<<ans<<endl;
60         for(int i=1;i<n*2;i++){
61             ans+=(ll)(a[i].x-a[i-1].x)*da[1].len;
62             ans%=mod;
63             fy=a[i].fy,ty=a[i].ty,type=a[i].type;
64             //printf("i=%d\t ans=%lld\t ",i,ans);
65             insert(1,0,ysz-1);
66         }
67         cout<<ans<<endl;
68     }
69     return 0;
70 }
时间: 2024-08-26 13:48:19

Rectangles Area Sum的相关文章

SPOJ CIRU The area of the union of circles (计算几何)

题意:求 m 个圆的并的面积. 析:就是一个板子题,还有要注意圆的半径为0的情况. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstrin

sum(),max(),avg(),RATIO_TO_REPORT()--分组统计

select id,area, sum(1) over() as 总记录数, sum(1) over(partition by id) as 分组记录数, sum(score) over() as 总计 , sum(score) over(partition by id) as 分组求和, sum(score) over(order by id) as  分组连续求和, sum(score) over(partition by id,area) as 分组ID和area求和, sum(score

day05_oracle分析函数

create table earnings -- 打工赚钱表 ( earnmonth varchar2(6), -- 打工月份 area varchar2(20), -- 打工地区 sno varchar2(10), -- 打工者编号 sname varchar2(20), -- 打工者姓名 times int, -- 本月打工次数 singleincome number(10,2), -- 每次赚多少钱 personincome number(10,2) -- 当月总收入 ); 然后插入实验数

order_by_、group_by_、having的用法区别

写于 2012-11-20 22:14  doc文档上. Having 这个是用在聚合函数的用法.当我们在用聚合函数的时候,一般都要用到GROUP BY 先进行分组,然后再进行聚合函数的运算.运算完后就要用到HAVING 的用法了,就是进行判断了,例如说判断聚合函数的值是否大于某一个值等等. select customer_name,sum(balance) from balance group by customer_name having balance>200; yc_rpt_getnew

UVALive 7261 Xiongnu&#39;s Land(二分)

题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5273 思路:二分位置(无需考虑总坐标,仅考虑横坐标即可),使得2*area >= sum,在满足该条件的情况下,尽量右移使得左侧面积尽量大. #include<cstdio> #include<cstring> #include<

oracle - 作报表需要的一些sql

作报表需要的一些oracle sql 天寒地冻,呆在家里又读完了<Mastering Oracle SQL>2nd,发现Oracle的功能还是很强悍,光函数就有两百个,那些面向对象的查询语言很难模拟,特别是SQL2003里针对OLAP的windows function等.     幸好Hibernate3.0也支持SQL了. 1.报表合计专用的Rollup函数 销售报表   广州     1月      2000元   广州     2月      2500元   广州            

Oracle分析函数介绍

Oracle的分析函数功能非常强大,工作这些年来经常用到.这次将平时经常使用到的分析函数整理出来,以备日后查看. 我们拿案例来学习,这样理解起来更容易一些. 1.建表 create table earnings -- 打工赚钱表 ( earnmonth varchar2(6), -- 打工月份 area varchar2(20), -- 打工地区 sno varchar2(10), -- 打工者编号 sname varchar2(20), -- 打工者姓名 times int, -- 本月打工次

SPOJ CIRU SPOJ VCIRCLE 圆的面积并问题

SPOJ VCIRCLE SPOJ CIRU 两道题都是给出若干圆 就面积并,数据规模和精度要求不同. 求圆面积并有两种常见的方法,一种是Simpson积分,另一种是几何法. 在这里给出几何方法. PS.以下算法基于正方向为逆时针 考虑上图中的蓝色圆,绿色的圆和蓝色的圆交于 A,B 2个交点 ,我们在逆时针系下考虑,那么 可以知道 对于蓝色的圆,它对应的某个 角度区间被覆盖了 假设 区间为 [A, B], 并且角度是按照 圆心到交点的 向量的 极角来定义 (为了方便,我一般都把角度转化到 [0,

oracle 10g函数大全--分析函数

oracle分析函数--SQL*PLUS环境 一.总体介绍 12.1 分析函数如何工作 语法 FUNCTION_NAME(<参数>,…) OVER (<PARTITION BY 表达式,…> <ORDER BY 表达式 <ASC DESC> <NULLS FIRST NULLS LAST>> <WINDOWING子句>) PARTITION子句 ORDER BY子句 WINDOWING子句 缺省时相当于RANGE UNBOUNDED