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 #737 (Div. 2) D. Ezzat and Grid 线段树动态开点

    传送门 文章目录 题意: 思路: 题意: 思路: 比较套路的一个题,我们维护一个dp[i]dp[i]dp[i]表示到了第iii行能保留的区间最多是多少. 转移比较明显:dp[i]=max(dp[j]) ...

  2. Codeforces Round #742 (Div. 2) E. Non-Decreasing Dilemma (线段树维护区间连续问题)

    题意: 操作1:把x位置的数字修改成y. 操作2:查询[l,r]之间不下降序列的个数. 题解: 线段树维护区间和问题 (这是套路,想不到只能说做题少别打我) . 用五个变量进行维护. sum区间总个数 ...

  3. Codeforces Round #686 (Div. 3) F. Array Partition 二分 + 线段树

    传送门 文章目录 题意: 思路: 题意: 化简一下题意就是求满足max(1,x)=min(x+1,y)=max(y+1,n)max(1,x)=min(x+1,y)=max(y+1,n)max(1,x) ...

  4. Codeforces Round #193 (Div. 2) B. Maximum Absurdity(线段树+思维)

    题目要求我们找到两个长度为 k 的不相交的子段,使得这两个子段之和最大,输出这两个子段的左端点 先将 [k,n] 的长度为 k 的子段和求出来,将其放入线段树中,然后枚举区间 i∈[k,n],使 i ...

  5. Codeforces Round #245 (Div. 1) E. Points and Segments 欧拉回路 + 建模

    传送门 文章目录 题意: 思路: 题意: 思路: 考虑对于线段,如何建模. 我们考虑先将线段转换成左闭右开的形式,将左右点连起来. 再考虑每个点,将所有离散化后的点拿出来,每个点都有一个度,现在问题就 ...

  6. Codeforces Round #716 (Div. 2) D. Cut and Stick 主席树 + 思维

    传送门 文章目录 题意: 思路: 题意: 给你个长为nnn的数组aaa,定义好的区间为这个区间中每个数出现的次数≤⌈n2⌉\le \left \lceil \frac{n}{2} \right \rc ...

  7. [Codeforces Round #254 div1] C.DZY Loves Colors 【线段树】

    题目链接:CF Round #254 div1 C 题目分析 这道题目是要实现区间赋值的操作,同时还要根据区间中原先的值修改区间上的属性权值. 如果直接使用普通的线段树区间赋值的方法,当一个节点表示的 ...

  8. Codeforces Round #337 (Div. 2) C. Harmony Analysis

    题目链接:http://codeforces.com/contest/610/problem/C 解题思路: 将后一个矩阵拆分为四个前一状态矩阵,其中三个与前一状态相同,剩下一个直接取反就行.还有很多 ...

  9. 主席树 | 莫队 ---- Codeforces Round #716 (Div. 2) D. Cut and Stick [主席树or莫队优化] 区间众数问题(静态)

    题目链接 题目大意: 就是给你nnn个数,和q次询问,每次询问给你一个区间[l,r][l,r][l,r],问你把区间里面的数分配成最少多少块,使得块内出现最多次数的数不超过区间长度的一半(除不尽向上取 ...

最新文章

  1. 2019 浙江大学 计算机 科目,2019考研大纲:浙江大学2019年《计算机学科专业基础综合》(单考)(科目代码907)...
  2. [转]将C#程序嵌入资源中(C# 调用嵌入资源的EXE文件方法)
  3. keepalive 配合mysql主主复制
  4. Latex ! Missing $ inserted error 解决方法
  5. php写出个人所得税,PHP计算个人所得税示例【不使用速算扣除数】
  6. Sublime Es6教程1-环境搭建
  7. 【错误记录】Android 内存泄漏 错误排查记录 ( FinalizerReference 内存泄漏 )
  8. 论文盘点:GAN生成对抗样本的方法解析
  9. SENetSKNet 解读
  10. ATS读小文件(内存命中)
  11. bo耳机h5使用说明_真香时刻·性价比大旗——红米AirDots2 TWS真无线耳机体验测评...
  12. jrebel、JavaRebel
  13. BugkuCTF-Crypto题给你私钥吧
  14. 分解得到的时频域特征_AI大语音(四)| MFCC特征提取(深度解析)
  15. 500状态码_教你玩转HTTP—状态码
  16. vue引入阿里云图标
  17. 史上最搞笑的程序员段子,有图有真相!
  18. CDH6.3.3 paywall版之前自定义http服务器放置parcels安装数据
  19. Stanford Alpaca (羊驼):ChatGPT 学术版开源实现
  20. 【论文笔记】Adversarial Multi-task Learning for Text Classification

热门文章

  1. 第一个程序01 - 零基础入门学习汇编语言20
  2. 农牧行业销售经理生存手册(二)
  3. 云+社区小程序知识周,等你来挑战!
  4. 友盟分享和cocos2dx符合重复duplicate symbol 解决方案
  5. ubuntu bless 16字节每行
  6. C++拷贝构造函数详解
  7. 服务器安装配置流水帐
  8. 测试整数(二进制)含1个数
  9. exchange server 2003 错误处理
  10. 【译】一行css代码搞定响应式布局