2016暑假多校联合---Counting Intersections
The input data guarantee that no two segments share the same endpoint, no covered segments, and no segments with length 0.
The first line of each test case contains a number n(1<=n<=100000), the number of segments. Next n lines, each with for integers, x1, y1, x2, y2, means the two endpoints of a segment. The absolute value of the coordinate is no larger than 1e9.
- #include <iostream>
- #include <algorithm>
- #include <cstdio>
- #include <cstring>
- #include <map>
- using namespace std;
- typedef long long LL;
- LL ax[],ay[];
- LL c[];
- LL Lowbit(LL t)
- {
- return t&(t^(t-));
- }
- LL Sum(LL x)
- {
- LL sum = ;
- while(x > )
- {
- sum += c[x];
- x -= Lowbit(x);
- }
- return sum;
- }
- void add(LL li,LL d)
- {
- while(li<)
- {
- c[li]+=d;
- li=li+Lowbit(li);
- }
- }
- struct Nodex
- {
- LL x1,x2;
- LL y;
- }nodex[];
- struct Nodey
- {
- LL y1,y2;
- LL x;
- }nodey[];
- struct Node
- {
- LL x;
- LL y;
- }node[];
- bool cmp1(const Nodey s1,const Nodey s2)
- {
- return s1.x<s2.x;
- }
- bool cmp2(const Node s1,const Node s2)
- {
- return s1.x<s2.x;
- }
- map<LL,LL>q1;
- map<LL,LL>q2;
- int main()
- {
- LL T,n;
- scanf("%I64d",&T);
- while(T--)
- {
- q1.clear();
- q2.clear();
- scanf("%I64d",&n);
- LL tot1=,tot2=;
- for(LL i=;i<n;i++)
- {
- LL x1,x2,y1,y2;
- scanf("%I64d%I64d%I64d%I64d",&x1,&y1,&x2,&y2);
- if(x1==x2)
- {
- nodey[tot2].y1=min(y1,y2);
- nodey[tot2].y2=max(y1,y2);
- nodey[tot2++].x=x1;
- }
- else
- {
- nodex[tot1].x1=min(x1,x2);
- nodex[tot1].x2=max(x1,x2);
- nodex[tot1++].y=y1;
- }
- }
- LL num1,num2;
- for(LL i=;i<tot1;i++)
- {
- ax[i*]=nodex[i].x1;
- ax[i*+]=nodex[i].x2;
- }
- num1=*tot1;
- for(LL i=;i<tot2;i++)
- {
- ax[num1++]=nodey[i].x;
- }
- sort(ax,ax+num1);
- LL tot=,pre=-;
- for(LL i=;i<num1;i++)
- {
- if(ax[i]!=pre)
- {
- pre=ax[i];
- q1[pre]=++tot;
- }
- }
- for(LL i=;i<tot2;i++)
- {
- ay[i*]=nodey[i].y1;
- ay[i*+]=nodey[i].y2;
- }
- num2=*tot2;
- for(LL i=;i<tot1;i++)
- ay[num2++]=nodex[i].y;
- sort(ay,ay+num2);
- tot=,pre=-;
- for(LL i=;i<num2;i++)
- {
- if(ay[i]!=pre)
- {
- pre=ay[i];
- q2[pre]=++tot;
- }
- }
- for(LL i=;i<tot1;i++)
- {
- nodex[i].x1=q1[nodex[i].x1];
- nodex[i].x2=q1[nodex[i].x2];
- nodex[i].y=q2[nodex[i].y];
- }
- for(LL i=;i<tot2;i++)
- {
- nodey[i].y1=q2[nodey[i].y1];
- nodey[i].y2=q2[nodey[i].y2];
- nodey[i].x=q1[nodey[i].x];
- }
- //for(LL i=0;i<tot1;i++)
- //cout<<"x: "<<nodex[i].x1<<" "<<nodex[i].x2<<" "<<nodex[i].y<<endl;
- //for(LL i=0;i<tot1;i++)
- //cout<<"y: "<<nodey[i].y1<<" "<<nodey[i].y2<<" "<<nodey[i].x<<endl;
- sort(nodey,nodey+tot2,cmp1);
- tot=;
- for(LL i=;i<tot1;i++)
- {
- node[tot].x=nodex[i].x1;
- node[tot++].y=nodex[i].y;
- node[tot].x=nodex[i].x2+;
- node[tot++].y=-nodex[i].y;
- }
- sort(node,node+tot,cmp2);
- LL j=,k=,sum=;
- memset(c,,sizeof(c));
- for(LL i=;i<=;i++)
- {
- while(node[j].x==i&&j<tot)
- {
- if(node[j].y>)
- add(node[j].y,);
- else
- add(-node[j].y,-);
- j++;
- }
- while(nodey[k].x==i&&k<tot2)
- {
- sum+=Sum(nodey[k].y2)-Sum(nodey[k].y1-);
- k++;
- }
- if(j>=tot) break;
- if(k>=tot2) break;
- }
- printf("%I64d\n",sum);
- }
- return ;
- }
2016暑假多校联合---Counting Intersections的更多相关文章
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- 2016暑假多校联合---Windows 10
2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...
- 2016暑假多校联合---Substring(后缀数组)
2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a ...
- 2016暑假多校联合---To My Girlfriend
2016暑假多校联合---To My Girlfriend Problem Description Dear Guo I never forget the moment I met with you. ...
- 2016暑假多校联合---A Simple Chess
2016暑假多校联合---A Simple Chess Problem Description There is a n×m board, a chess want to go to the po ...
- 2016暑假多校联合---Another Meaning
2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two m ...
- 2016暑假多校联合---Death Sequence(递推、前向星)
原题链接 Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historia ...
- 2016暑假多校联合---Joint Stacks (STL)
HDU 5818 Problem Description A stack is a data structure in which all insertions and deletions of e ...
- 2016暑假多校联合---GCD
Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). ...
随机推荐
- paip.为什么使用多线程的原因.
paip.为什么使用多线程的原因. 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...
- iOS开发----优秀文章推荐
UI界面 iOS和Android 界面设计尺寸规范 http://www.alibuybuy.com/posts/85486.html iPhone app界面设计尺寸规范 http://www. ...
- ServletConfig接口默认是哪里实现的?
问题:Servlet接口默认是哪里实现的? 答:GenericServlet 1.结构 2.ServletConfig.GenericServlet.HttpServlet的关系如下: public ...
- 将Windows系统编译的.NET Core程序发布到Ubuntu系统
在可移植方面.NET Core应用程序分为两种,Portable application(便捷,需要目标机器安装.NET Core Runtime)和Self-contained applicatio ...
- andriod sdk模拟器安装过程中报错
andriod sdk模拟器安装过程中,出现下述错误: Failed to fetch URL http://dl-ssl.google.com/android/repository/reposito ...
- POJ3069 POJ2586 解题报告(异曲同工的贪心算法)
[POJ 3069](2586见下) 原题在此:http://poj.org/problem?id=3069 题目大意: 一个直线上有N个点.点i的距离是Xi.从这些点中选取若干个加上标记.要求:对于 ...
- JSON学习之一
1.JSON:JavaScript Object Notation(javaScript对象表示法),JSON是存储和交换文本信息的语法,类似XML:JSON比XML更小,更快,更易解析: { &qu ...
- Git使用命令
git init 初始化仓库 git init --bare 初始化一个裸仓库 git branch 查看本地分支 git branch -a 查看全部分支 git remote 远程仓库管理 add ...
- javascript类型系统——布尔Boolean类型
× 目录 [1]定义 [2]应用场景 [3]转为布尔[4]实例方法 前面的话 布尔值Boolean类型可能是三种包装对象Number.String和Boolean中最简单的一种.Number和Stri ...
- 后端码农谈前端(CSS篇)第四课:选择器补充(伪类与伪元素)
一.伪类: 属性 描述 :active 向被激活的元素添加样式. :focus 向拥有键盘输入焦点的元素添加样式. :hover 当鼠标悬浮在元素上方时,向元素添加样式. :link 向未被访问的链接 ...