hud -5124-lines(线段树)

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

给的数据范围为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 }

时间: 2024-10-23 03:41:02

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

hdu 5124 lines (线段树+离散化)

lines Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 620    Accepted Submission(s): 288 Problem Description John has several lines. The lines are covered on the X axis. Let A is a point which

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

http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines are covered on the X axis. Let A is a point which is covered by the most lines. John wants to know how many lines cover A. Input The first line conta

hdu 1394 Minimum Inversion Number(这道题改日我要用线段树再做一次哟~)

Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of

Curious Robin Hood(树状数组+线段树)

1112 - Curious Robin Hood    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another tri

SPOJ--K-query (线段树离线) 离线操作解决一下问题

K-query Given a sequence of n numbers a1, a2, ..., an and a number of k- queries. A k-query is a triple (i, j, k) (1 ≤ i ≤ j ≤ n). For each k-query (i, j, k), you have to return the number of elements greater than k in the subsequence ai, ai+1, ...,

HDU1394 Minimum Inversion Number 线段树+数学

Problem Description The inversion number of a given number sequence a1, a2, -, an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, -, an, if we move the first m >= 0 numbers to the end of the

HDU 1689 Just a Hook 线段树区间更新求和

点击打开链接 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18894    Accepted Submission(s): 9483 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible

poj 3264 Balanced Lineup RMQ线段树实现

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36613   Accepted: 17141 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

hdu 1698 Just a Hook(线段树,成段更新,懒惰标记)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18384    Accepted Submission(s): 9217 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing