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

给的数据范围为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. 21-Add Two Numbers-Leetcode

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  2. Spark的shuffle和MapReduce的shuffle对比

    目录 MapperReduce的shuffle Spark的shuffle 总结 MapperReduce的shuffle shuffle阶段划分 Map阶段和Reduce阶段 任务 MapTask和 ...

  3. Spring MVC入门(一)—— RestTemplate组件

    ClientHttpRequestFactory 它是个函数式接口,用于根据URI和HttpMethod创建出一个ClientHttpRequest来发送请求~ ClientHttpRequest它代 ...

  4. Vue重要知识

    Event Bus 总线 Vue中的EventBus是一种发布订阅模式的实践,适用于跨组件简单通信. Vuex也可以用来组件中进行通信,更适用于多组件高频率通信. 使用方式: 1.把Bus注入到Vue ...

  5. HTML DOM 对象 - 方法和属性

    一些常用的 HTML DOM 方法: getElementById(id) - 获取带有指定 id 的节点(元素) appendChild(node) - 插入新的子节点(元素) removeChil ...

  6. 用户信息查询系统_daoImpl

    package com.hopetesting.dao.impl;import com.hopetesting.dao.UserDao;import com.hopetesting.domain.Us ...

  7. 因Console.Read()导致Centos 后台运行.net core程序报错

    .net 控制台程序通常用 Console.Read(),或者Console.ReadKey()让进程阻塞保持,不退出. 但在.net core 需要将程序放在后台执行时 用Console.Read( ...

  8. uWSGI和WSGI之间的关系

    一.WSGI 协议 WSGI:是一种协议规范,起到规范参数的作用,就像告诉公路一样,规定超车靠右行,速度不低于90km/h,等.但这一切都是对双方进行沟通,比如,重庆到武汉这条高速路,这儿重庆和武汉就 ...

  9. 测试工具_http_load

    目录 一.简介 二.例子 三.参数 一.简介 http_load以并行复用的方式运行,用以测试Web服务器的吞吐量与负载.但是它不同于大多数压力测试工具,其可以以一个单一的进程运行,这样就不会把客户机 ...

  10. 01-gevent完成多任务

    gevent完成多任务 一.原理 gevent实现多任务并不是依靠多进程或是线程,执行的时候只有一个线程,在遇到堵塞的时候去寻找可以执行的代码.本质上是一种协程. 二.代码实现 import geve ...