D. Vika and Segments

题目连接:

http://www.codeforces.com/contest/610/problem/D

Description

Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-dimensional coordinate system on this sheet and drew n black horizontal and vertical segments parallel to the coordinate axes. All segments have width equal to 1 square, that means every segment occupy some set of neighbouring squares situated in one row or one column.

Your task is to calculate the number of painted cells. If a cell was painted more than once, it should be calculated exactly once.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of segments drawn by Vika.

Each of the next n lines contains four integers x1, y1, x2 and y2 ( - 109 ≤ x1, y1, x2, y2 ≤ 109) — the coordinates of the endpoints of the segments drawn by Vika. It is guaranteed that all the segments are parallel to coordinate axes. Segments may touch, overlap and even completely coincide.

Output

Print the number of cells painted by Vika. If a cell was painted more than once, it should be calculated exactly once in the answer.

Sample Input

3

0 1 2 1

1 4 1 2

0 3 2 3

Sample Output

8

Hint

题意

在平面上会画n条宽度为1的线

然后问你最后画出来的线的总面积是多少

题解:

把画出来的线当成矩形

然后就是扫描线的裸题了

线段树跑一波就吼了

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
using namespace std; #define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define MID(a,b) (a+((b-a)>>1))
typedef long long LL;
const int N=360005; struct Line
{
int x,y1,y2,flag;
Line(){}
Line(int a,int b,int c,int d)
{ x=a;y1=b;y2=c;flag=d; }
bool operator<(const Line&b)const
{ return x<b.x; }
};
struct node
{
int lft,rht;
int len[12],flag;
int mid(){return MID(lft,rht);}
void init(){memset(len,0,sizeof(len));}
}; int n,k;
vector<int> y;
vector<Line> line;
map<int,int> H; struct Segtree
{
node tree[N*4];
void calu(int ind)
{
if(tree[ind].flag>=k)
{
int tmp=tree[ind].len[0];
tree[ind].init();
tree[ind].len[k]=tree[ind].len[0]=tmp;
}
else if(tree[ind].flag>0)
{
int sum=0,cov=tree[ind].flag;
for(int i=1;i<=k;i++) tree[ind].len[i]=0;
tree[ind].len[cov]=tree[ind].len[0];
if(tree[ind].lft+1==tree[ind].rht) return; for(int i=1;i<=k;i++)
{
if(i+cov>=k) tree[ind].len[k]+=tree[LL(ind)].len[i]+tree[RR(ind)].len[i];
else tree[ind].len[i+cov]=tree[LL(ind)].len[i]+tree[RR(ind)].len[i];
}
for(int i=cov+1;i<=k;i++) sum+=tree[ind].len[i];
tree[ind].len[cov]-=sum;
}
else
{
for(int i=1;i<=k;i++) tree[ind].len[i]=0;
if(tree[ind].lft+1==tree[ind].rht) return;
for(int i=1;i<=k;i++)
tree[ind].len[i]=tree[LL(ind)].len[i]+tree[RR(ind)].len[i];
}
}
void build(int lft,int rht,int ind)
{
tree[ind].lft=lft; tree[ind].rht=rht;
tree[ind].init(); tree[ind].flag=0;
tree[ind].len[0]=y[rht]-y[lft];
if(lft+1!=rht)
{
int mid=tree[ind].mid();
build(lft,mid,LL(ind));
build(mid,rht,RR(ind));
}
}
void updata(int st,int ed,int ind,int valu)
{
int lft=tree[ind].lft,rht=tree[ind].rht;
if(st<=lft&&rht<=ed) tree[ind].flag+=valu;
else
{
int mid=tree[ind].mid();
if(st<mid) updata(st,ed,LL(ind),valu);
if(ed>mid) updata(st,ed,RR(ind),valu);
}
calu(ind);
}
}seg;
int main()
{
scanf("%d",&n);
k=1;
for(int i=0;i<n;i++)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(x1>x2)swap(x1,x2);
if(y1>y2)swap(y1,y2);
x2++;y2++;
line.push_back(Line(x1,y1,y2,1));
line.push_back(Line(x2,y1,y2,-1));
y.push_back(y1); y.push_back(y2);
} sort(line.begin(),line.end());
sort(y.begin(),y.end());
y.erase(unique(y.begin(),y.end()),y.end());
for(int i=0;i<(int)y.size();i++) H[y[i]]=i; seg.build(0,(int)y.size()-1,1); LL res=0;
for(int i=0;i<(int)line.size();i++)
{
if(i!=0) res+=(LL)(line[i].x-line[i-1].x)*seg.tree[1].len[k];
seg.updata(H[line[i].y1],H[line[i].y2],1,line[i].flag);
}
cout<<res<<endl;
return 0;
}

Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线的更多相关文章

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

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

  2. 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 ...

  3. Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

  4. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  5. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

  6. Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash

    E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...

  7. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  8. Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)

    题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...

  9. Codeforces Round #312 (Div. 2) E. A Simple Task 线段树

    E. A Simple Task 题目连接: http://www.codeforces.com/contest/558/problem/E Description This task is very ...

随机推荐

  1. Ajax轮询以及Comet模式—写在Servlet 3.0发布之前(转)

    2008 年的夏天,偶然在网上闲逛的时候发现了 Comet 技术,人云亦云间,姑且认为它是由 Dojo 的 Alex Russell 在 2006 年提出.在阅读了大量的资料后,萌发出写篇 blog ...

  2. css3.0新属性效果在ie下的解决方案(兼容性)

    css3.0增加的新属性,如投影.渐变.旋转.圆角等等!这些新标准属性在ie6.ie7.ie8浏览器版本里得不到很好的支持,相信ie以后的新版本也会支持这些新属性的.目前ie6.ie7.ie8浏览器不 ...

  3. list 容器 排序函数.xml

    pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9; ...

  4. MATLAB 通过二进制读写文件

    这几天在做信息隐藏方面的应用,在读写文本文件时耗费许久,故特别的上网学习一二,这里给出一常用读写,其他的都类似. 很多时候,我们都要将一个.txt以二进制方式读出来,操作后在恢复成.txt文本. ma ...

  5. Start SparkR in RStudio

    Sys.setenv(SPARK_HOME="/usr/spark") .libPaths(c(file.path(Sys.getenv("SPARK_HOME" ...

  6. Using NuGet without committing packages to source control(在没有把包包提交到代码管理器的情况下使用NuGet进行还原 )

    外国老用的语言就是谨慎,连场景都限定好了,其实我们经常下载到有用NuGet引用包包然而却没法编译的情况,上谷歌百度搜又没法使用准确的关键字,最多能用到的就是nuget跟packages.config, ...

  7. C++11 之 &quot; = delete &quot;

    1  缺省函数 设计一个类,没有成员函数 (member function),只有成员数据 (member data) class DataOnly { private: std::string st ...

  8. AutoCompleteTextView使用 监听

    AutoCompleteTextView使用 An editable text view that shows completion suggestions automatically while t ...

  9. 第三百三十五天 how can I 坚持

    晚上回来看了个奥斯卡影片,<疯狂的麦克斯-狂暴之路>,挺震撼的场面.导演确实挺厉害,不知道是怎么想象出来的. 睡觉,明天继续.

  10. Linux操作系统时间与BIOS硬件时间

    注:以Ubuntu为例说明 在Linux计算机上,有两个时间,一个是硬件时间(BIOS中记录的时间,称为hwclock),另一个是操作系统时间(osclock).硬件时钟由BIOS电池供电, 当计算机 ...