lines(最大区间和)

lines

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1350    Accepted Submission(s): 558

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 contains a single integer T(1≤T≤100)(the data for N>100 less than 11 cases),indicating the number of test cases.
Each test case begins with an integer N(1≤N≤105),indicating the number of lines.
Next N lines contains two integers Xi and Yi(1≤Xi≤Yi≤109),describing a line.

Output

For each case, output an integer means how many lines cover A.

Sample Input

2
5
1 2
2 2
2 4
3 4
5 1000
5
1 1
2 2
3 3
4 4
5 5

Sample Output

3
1

官方题解:
我们可以将一条线段[xi,yi]分为两个端点xi和(yi)+1,在xi时该点会新加入一条线段,同样的,在(yi)+1时该点会减少一条线段,因此对于2n个端点进行排序,令xi为价值1,yi为价值-1,问题转化成了最大区间和,因为1一定在-1之前,因此问题变成最大前缀和,我们寻找最大值就是答案,另外的,这题可以用离散化后线段树来做。复杂度为排序的复杂度即nlgn,另外如果用第一种做法数组应是2n,而不是n,由于各种非确定性因素我在小数据就已经设了n=10W的点。

题解:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #define mem(x,y) memset(x,y,sizeof(x))
 7 using namespace std;
 8 typedef long long LL;
 9 const int INF=0x3f3f3f3f;
10 const int MAXN=1e5+100;
11 pair<int,int>pa[MAXN<<1];
12 int main(){
13     int T,N;
14     scanf("%d",&T);
15     while(T--){int a,b;
16         scanf("%d",&N);
17         for(int i=0;i<N;i++){
18             scanf("%d%d",&a,&b);
19             pa[i<<1]=make_pair(a,1);
20             pa[i<<1|1]=make_pair(b+1,-1);
21         }
22         sort(pa,pa+N*2);
23         int ans=0,cnt=0;
24         for(int i=0;i<2*N;i++)
25             cnt+=pa[i].second,ans=max(ans,cnt);
26         printf("%d\n",ans);
27     }
28     return 0;
29 }
时间: 2024-08-25 00:58:46

lines(最大区间和)的相关文章

Guava中RangeMap自定义range区间

***********************************************声明****************************************************** 原创作品,出自 "晓风残月xj" 博客,欢迎转载,转载时请务必注明出处(http://blog.csdn.net/xiaofengcanyuexj). 由于各种原因,可能存在诸多不足,欢迎斧正! *******************************************

HDU4622:Reincarnation(后缀数组,求区间内不同子串的个数)

Problem Description Now you are back,and have a task to do: Given you a string s consist of lower-case English letters only,denote f(s) as the number of distinct sub-string of s. And you have some query,each time you should calculate f(s[l...r]), s[l

UVA 11992(Fast Matrix Operations-线段树区间加&amp;改)[Template:SegmentTree]

Fast Matrix Operations There is a matrix containing at most 106 elements divided into r rows and c columns. Each element has a location (x,y) where 1<=x<=r,1<=y<=c. Initially, all the elements are zero. You need to handle four kinds of operati

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

HDU 3397 线段树区间修改

Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8308    Accepted Submission(s): 2507 Problem Description lxhgww got a sequence contains n characters which are all '0's or '1'

poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 83959   Accepted: 25989 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. Yo

hdu 1698(线段树区间更新)

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

【线段树区间合并】HDU1540-Tunnel Warfare

一.题目 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village

hdu--1540 Tunnel Warfare(线段树+区间合并)

Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was