模板题,矩形面积并

#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. [转]php 在各种web服务器的运行模式

    一.php在apache中运行模式 php在apache中一共有三种工作方式:CGI模式.FastCGI模式.Apache 模块DLL) 以下分别比较: 1. CGI模式与模块模式比较: php在ap ...

  2. LNMP环境的安装配置

    0.安装必要的依赖软件 如果已经安装了可能会进行升级,版本完全一致则不会进行任何操作. yum -y install bzip2-devel curl-devel freetype-devel gcc ...

  3. sudo su– user

    [root@localhost ~] # visudo –f /etc/sudoers 在文件中的root账户下添加需要切换root账户的账户 root ALL=(ALL) ALL user ALL= ...

  4. php 常用的JS

      function doit(){ var sel_val=$("#type").val(); if (sel_val=='') { $("#bigClassId1&q ...

  5. VS2013使用技巧汇总

    1. Peek View 在不新建TAB的情况下快速查看.编辑一个函数的代码. 以前要看一个函数的实现,需要在使用的地方点击F12跳转到该函数,实际上这是很浪费时间的.VS2013Peek View便 ...

  6. c++内存流

    1.MemoryStream.h文件内容 ifndef _MEM_STREAM_H_ #define _MEM_STREAM_H_ #include <string> class CMem ...

  7. char与varchar、nvarchar区别

    char     char是定长的,也就是当你输入的字符小于你指定的数目时,char(8),你输入的字符小于8时,它会再后面补空值.当你输入的字符大于指定的数时,它会截取超出的字符.    nvarc ...

  8. HDU 1540<线段树,区间并>

    题目连接 参考 题意: 维护各个点的连续的最大连续长度. 思路: 主要是维护一个区间的三个变量ll,f[i].l为起点向右的最大连续 长度,rl:f[i].r为起点向左的最大连续长度,ml:[l,r] ...

  9. 使用React Native一年后的感受

    转载自:http://www.dobest.me/blog/2016/06/12/%E4%BD%BF%E7%94%A8React%20Native%E4%B8%80%E5%B9%B4%E5%90%8E ...

  10. angularjs model.service vs provider vs factory?

    <!DOCTYPE html> <html ng-app="app"> <head> <script src="http://c ...