题目的意思是求重合层数最多的段(把点也看成段)。

给的数据范围为N<1e5;

ai<1e9;

有于N只有1e5;那么离散化一下可以将ai的范围映射到1e5,而不改变原端点的相对大小。

接下来用线段树来做就行了,要用lazy操作,到最后再把所有的值放下,然后比较每个点的大小,最大的就为重合最多的。

线段树复杂度为N*log(N);

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<math.h>
7 #include<map>
8 void local(int l,int r,int k);
9 void add(int l,int r,int k,int uu,int ww);
10 typedef long long ll;
11 typedef struct pp
12 {
13 int x;
14 int y;
15 } ss;
16 int tree[4*100005];//线段树数组
17 int su[100005*2];
18 ss dd[100005];//结构体存段
19 int N;
20 using namespace std;
21 int main(void)
22 {
23 int i,j,k,num;
24 scanf("%d",&k);
25 while(k--)
26 {
27 N=0;
28 fill(tree, tree+4*100005,0);
29 scanf("%d",&num);
30 int z=0;
31 map<int,int>my;//映射
32 for(i=0; i<num; i++)
33 {
34 scanf("%d %d",&dd[i].x,&dd[i].y);
35 su[z]=dd[i].x;
36 z++;
37 su[z]=dd[i].y;
38 z++;
39 }
40 sort(su,su+z);
41 j=1;
42 for(i=0; i<z; i++)
43 {
44 if(my[su[i]]==0)
45 {
46 my[su[i]]=j;
47 j++;
48 }
49
50 }//离散化
51 for(i=0; i<num; i++)
52 {
53 int x=dd[i].x=my[dd[i].x];
54 int y=dd[i].y=my[dd[i].y];
55 add(x,y,0,1,j-1);//线段树加段更新
56 }
57 local(1,j-1,0);//最后下放查询
58 printf("%d\n",N);
59 }
60 return 0;
61
62 }
63 void add(int l,int r,int k,int uu,int ww)//建树更新
64 {
65 if(l>ww||r<uu)
66 {
67 return ;
68 }
69 else if(l<=uu&&r>=ww)
70 {
71 tree[k]+=1;
72 }
73 else
74 {
75 add(l,r,2*k+1,uu,(uu+ww)/2);
76 add(l,r,2*k+2,(uu+ww)/2+1,ww);
77 }
78 }
79 void local(int l,int r,int k)//下放查询
80 {
81 if(l==r)
82 {
83 if(N<tree[k])
84 {
85 N=tree[k];
86 }
87 return ;
88 }
89 else
90 {
91 tree[2*k+1]+=tree[k];
92 tree[2*k+2]+=tree[k];
93 local(l,(l+r)/2,2*k+1);
94 local((l+r)/2+1,r,2*k+2);
95
96 }
97 }

[title]1002 lines[/title] 我们可以将一条线段[x_i,y_i][x​i​​,y​i​​]分为两个端点x_ix​i​​和(yi)+1(yi)+1,在x_ix​i​​时该点会新加入一条线段,同样的,在(y_i)+1(y​i​​)+1时该点会减少一条线段,因此对于2n个端点进行排序,

令x_ix​i​​为价值1,y_iy​i​​为价值-1,问题转化成了最大区间和,因为1一定在-1之前,因此问题变成最大前缀和,我们寻找最大值就是答案

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<string.h>
6 #include<math.h>
7 #include<map>
8 int f(const void*p,const void*q);
9 typedef long long ll;
10 typedef struct pp
11 {
12 int x;
13 int y;
14 } ss;
15 using namespace std;
16 ss a[100005*2];
17 int main(void)
18 {
19 int i,j,k,p,q,s,l;
20 scanf("%d",&k);
21 while(k--)
22 {
23 scanf("%d",&p);
24 int uu=0;
25 for(i=0; i<p; i++)
26 {
27 scanf("%d",&s);
28 a[uu].x=s;
29 a[uu].y=1;
30 uu++;
31 scanf("%d",&l);
32 a[uu].x=l;
33 a[uu].y=0;
34 uu++;
35 }
36 qsort(a,uu,sizeof(ss),f);
37 ll sum=0;
38 ll dd=0;
39 for(i=0; i<uu; i++)
40 {
41 if(a[i].y==1)
42 {
43 sum++;
44 if(sum>dd)
45 {
46 dd=sum;
47 }
48 }
49 else sum--;
50 }
51 printf("%lld\n",dd);
52
53 }
54 return 0;
55 }
56 int f(const void*p,const void*q)
57 {
58 ss*w=(ss*)p;
59 ss*r=(ss*)q;
60 if(w->x==r->x)
61 {
62 return r->y-w->y;
63 }
64 return w->x-r->x;
65 }

下面map映射改为二分查找

  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<math.h>
7 #include<map>
8 void local(int l,int r,int k);
9 int er(int n,int m,int k);
10 void add(int l,int r,int k,int uu,int ww);
11 typedef long long ll;
12 typedef struct pp
13 {
14 int x;
15 int y;
16 } ss;
17 int tree[4*100005];
18 int su[100005*2];
19 int mapp[100005*2];
20 ss dd[100005];
21 int N=0;
22 using namespace std;
23 int main(void)
24 {
25 int i,j,k,p,q,num;
26 scanf("%d",&k);
27 while(k--)
28 {
29 N=0;
30 fill(tree, tree+4*100005,0);
31 scanf("%d",&num);
32 int z=0;
33 map<int,int>my;
34 for(i=0; i<num; i++)
35 {
36 scanf("%d %d",&dd[i].x,&dd[i].y);
37 su[z]=dd[i].x;
38 z++;
39 su[z]=dd[i].y;
40 z++;
41 }
42 sort(su,su+z);
43 j=1;
44 mapp[0]=1;
45 for(i=1; i<z; i++)
46 {
47 if(su[i]!=su[i-1])
48 {
49 j++;
50 mapp[i]=j;
51 }
52 else mapp[i]=j;
53
54 }
55 for(i=0; i<num; i++)
56 {
57 int x=dd[i].x=er(0,z-1,dd[i].x);
58 int y=dd[i].y=er(0,z-1,dd[i].y);
59 add(x,y,0,1,j);
60 }
61 local(1,j,0);
62 printf("%d\n",N);
63 }
64 return 0;
65
66 }
67
68
69 void add(int l,int r,int k,int uu,int ww)
70 {
71 if(l>ww||r<uu)
72 {
73 return ;
74 }
75 else if(l<=uu&&r>=ww)
76 {
77 tree[k]+=1;
78 }
79 else
80 {
81 add(l,r,2*k+1,uu,(uu+ww)/2);
82 add(l,r,2*k+2,(uu+ww)/2+1,ww);
83 }
84 }
85 void local(int l,int r,int k)
86 {
87 if(l==r)
88 {
89 if(N<tree[k])
90 {
91 N=tree[k];
92 }
93 return ;
94 }
95 else
96 {
97 tree[2*k+1]+=tree[k];
98 tree[2*k+2]+=tree[k];
99 local(l,(l+r)/2,2*k+1);
100 local((l+r)/2+1,r,2*k+2);
101
102 }
103 }
104 int er(int n,int m,int k)
105 {
106 int zz=(n+m)/2;
107 if(m<n)
108 {
109 return -1;
110 }
111 if(su[zz]==k)
112 {
113 return mapp[zz];
114 }
115 else if(su[zz]>k)
116 {
117 return er(n,zz-1,k);
118 }
119 else return er(zz+1,m,k);
120 }

hud -5124-lines(线段树)的更多相关文章

  1. HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧)

    HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧) 题意分析 题目大意:一个h*w的公告牌,要在其上贴公告. 输入的是1*wi的w值,这些是公告的尺寸. 贴公告 ...

  2. hud 5124 lines(思维 + 离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 lines   Problem Description: John has several lines. ...

  3. UVa 1471 Defense Lines - 线段树 - 离散化

    题意是说给一个序列,删掉其中一段连续的子序列(貌似可以为空),使得新的序列中最长的连续递增子序列最长. 网上似乎最多的做法是二分查找优化,然而不会,只会值域线段树和离散化... 先预处理出所有的点所能 ...

  4. hud 3308 LCIS 线段树 区间合并

    题意: Q a b 查询[a, b]区间的最长连续递增子序列的长度 U a b 将下表为a的元素更新为b 区间合并一般都有3个数组:区间最值,左区间最值和右区间最值 具体详见代码 #include & ...

  5. HDU5124:lines(线段树+离散化)或(离散化思想)

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...

  6. HUD 1166:敌兵布阵(线段树 or 树状数组)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Des ...

  7. ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】

     FZU 2105  Digits Count Time Limit:10000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  8. HDU 5475(2015 ICPC上海站网络赛)--- An easy problem(线段树点修改)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5475 Problem Description One day, a useless calculato ...

  9. CF380C. Sereja and Brackets[线段树 区间合并]

    C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...

随机推荐

  1. 取gridview中textbox的值【C#】

    <asp:GridView ID="gridView" runat="server" OnRowCommand="gridView_RowCom ...

  2. C++中union相关

    前两天做阿里笔试遇到一个选择题题目大概是 #include <iostream> #include <stdlib.h> using namespace std; union ...

  3. Spring Batch Event Listeners

    Learn to create and configure Spring batch's JobExecutionListener (before and after job), StepExecut ...

  4. C++STL标准库学习笔记(四)multiset续

    自定义排序规则的multiset用法 前言: 在这个笔记中,我把大多数代码都加了注释,我的一些想法和注解用蓝色字体标记了出来,重点和需要关注的地方用红色字体标记了出来,只不过这一次的笔记主要是我的补充 ...

  5. Redis集群断电恢复

    再集群整体断点或关闭后,默认启动集群后,会成为孤立的单点,需要删除每个节点的pid文件,node.conf.并将RDB和AOF文件移动出来,再挨个启动每个节点,并用create创建集群脚本,重新创建集 ...

  6. 代码仓库gogs的基本配置使用

    目录 一.基本功能介绍 主板说明 页面说明 用户设置 二.仓库 新建仓库 迁移仓库 仓库介绍 三.组织和团队 创建新组织 创建团队 一.基本功能介绍 主板说明 图中1表示自己个人账户下的仓库(所有权属 ...

  7. 跨平台调用之WebService

    一.简介 web service是一种跨编程语言和跨操作系统平台的远程调用技术,是基于网络的.分布式的模块化组件. 跨编程语言就是说服务器端程序采用 Java 编写,客户端程序则可以采用其他编程语言编 ...

  8. pipeline option指令

    目录 一.简介 二.参数 buildDiscarder checkoutToSubdirectory disableConcurrentBuilds newContainerPerStage retr ...

  9. Docker通过阿里云镜像仓库使用Gitlab_CI部署SpringBoot项目

    Docker.Gitlab.阿里云镜像仓库.SpringBoot的相关安装.搭建这里就不讲了. Linux 安装 Docker :https://www.cnblogs.com/linnuo/p/15 ...

  10. [BUUCTF]PWN——[BJDCTF 2nd]test

    [BJDCTF 2nd]test 步骤 根据题目,ssh连接一下靶机 登录成功后,ls看一下当前目录下的文件,根据提示可知,我们没法直接获取flag字符串,但是我们可以读取test的源码, test. ...