模板题,矩形面积并

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define maxn 220000
#define lson l,m,rt << 1
#define rson m + 1,r,rt << 1 | 1
#define eps 1e-15
using namespace std;
struct seg
{
double l,r,y;
int sign;
seg() {}
seg(double a, double b, double h, int s): l(a), r(b),y(h),sign(s) {}
bool operator < (const seg &cmp) const
{
if(cmp.y == y) return sign > cmp.sign;
return y < cmp.y;
}
} p[ (maxn << )+ ]; double sum[ (maxn << )+ ];
int cnt[ (maxn << ) + ]; double X[ (maxn << ) + ]; void push_up(int rt,int l, int r)
{
if(cnt[rt]) sum[rt] = X[r + ] - X[l]; else if(l == r) sum[rt] = 0.0; else sum[rt] = sum[rt << ] + sum[rt << | ];
}
void update(int L, int R, int cc, int l , int r, int rt)
{
if(L <= l && r <= R)
{
cnt[rt] += cc;
push_up(rt,l,r);
return ;
}
int m = (l + r) >> ;
if(L <= m) update(L,R,cc,lson);
if(R > m) update(L,R,cc,rson);
push_up(rt,l,r);
}
int bin(int low, int high, double key)
{
int mid ;
while(low < high)
{
mid = (low + high) >> ;
if(X[mid] < key ) low = mid + ;
else high = mid;
}
return low;
}
int main()
{
int n,m,k,pp,ca = ;
double a,b,c,d,ret;
while(scanf("%d",&n) != EOF && n)
{
memset(sum,,sizeof(sum));
memset(cnt,,sizeof(cnt));
m = k = ;
ret = 0.0;
pp = ; for(int i = ; i < n; i ++)
{
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
if(b==d)
{
if(a>c) swap(a,c);
}
else if(a==c)
{
if(b>d) swap(b,d);
}
c++;
d++;
X[m ++] = a;
X[m ++] = c; p[k ++] = seg(a,c,b,); p[k ++] = seg(a,c,d,-);
}
sort(X,X + m);
sort(p,p + k); for(int i = ; i < m; i ++)
if(X[i - ] != X[i])
{
X[pp ++] = X[i];
} pp --;
for(int i = ; i < k - ; i ++)
{
int l = bin(, pp , p[i].l);
int r = bin(, pp , p[i].r) - ;
if(l <= r) update(l,r,p[i].sign,,pp,);
ret += (p[i + ].y - p[i].y) * sum[];
}
printf("%.0f\n",ret);
}
return ;
}

CodeForces 610D Vika and Segments的更多相关文章

  1. Codeforces 610D Vika and Segments 线段树+离散化+扫描线

    可以转变成上一题(hdu1542)的形式,把每条线段变成宽为1的矩形,求矩形面积并 要注意的就是转化为右下角的点需要x+1,y-1,画一条线就能看出来了 #include<bits/stdc++ ...

  2. 610D - Vika and Segments(线段树+扫描线+离散化)

    扫描线:http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html 看图,图中的数字是横坐标离散后对应的下标,计算时左端点不 ...

  3. codeforces 610D D. Vika and Segments(离散化+线段树+扫描线算法)

    题目链接: D. Vika and Segments time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  4. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线

    D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...

  5. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并

    D. Vika and Segments     Vika has an infinite sheet of squared paper. Initially all squares are whit ...

  6. Codeforces Round #337 Vika and Segments

    D. Vika and Segments time limit per test:  2 seconds     memory limit per test:  256 megabytes input ...

  7. 【20.51%】【codeforces 610D】Vika and Segments

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

  9. Vika and Segments - CF610D

    Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-d ...

随机推荐

  1. html 时间单位

    <style>h1{font-size:16px;} .test{position:absolute;left:8px;width:200px;height:100px;margin:0 ...

  2. 连接SQLServer OLEDB数据库(ACCESS) ODBC Oracle

    web.Config文件中的连接字符串 <configuration> <system.web> <compilation debug="true" ...

  3. wpf 类似TextBlock外观的Button的样式

    <Style x:Key="noborderbtnStyle" TargetType="{x:Type Button}"> <Setter P ...

  4. List<string[]> 如何去重

    List<string[]> 如何去重,代码如下: static void Main(string[] args) { List<string[]> list = new Li ...

  5. 编写Linux/Unix守护进程

    原文: http://www.cnblogs.com/haimingwey/archive/2012/04/25/2470190.html 守护进程在Linux/Unix系统中有着广泛的应用.有时,开 ...

  6. centos7如何关闭防火墙

    1.centos7自带了firewall,而不是iptables: 关闭firewall: service firewalld stop 或者: systemctl stop firewalld 禁止 ...

  7. HDU 2255 奔小康赚大钱 KM算法的简单解释

    KM算法一般用来寻找二分图的最优匹配. 步骤: 1.初始化可行标杆 2.对新加入的点用匈牙利算法进行判断 3.若无法加入新编,修改可行标杆 4.重复2.3操作直到找到相等子图的完全匹配. 各步骤简述: ...

  8. 取得GridView某行的DataKey

    首先绑定DataKeyNames GridView.DataKeyNames = new string[] { "字段名称" }; 取值 string aaa= GridView. ...

  9. socket通信的json数据传输与获取

    本文是基于scoket通信的tcp来进行数据的json格式传输与获取的. 首先,我们先要下载AsyncSockethttps://github.com/robbiehanson/CocoaAsyncS ...

  10. HDU - 1205 I NEED A OFFER!

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1203 题意: 该题要求得到一份offer的最大概率,在例子中的0.44 = 1-(1-0.2)*(1- ...