您的位置:首页 > 其它

hdoj 5124 lines 【离散化 + 线段树】

2015-11-08 12:40 387 查看

lines

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1354    Accepted Submission(s): 560


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

 

题意:给定n条平行于x轴的线段,设被这些线段覆盖次数最多的点为A,让你求出A被覆盖的次数。

先把坐标离散化,然后就是线段树区间更新维护区间最大值了。

AC代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (100000+10)
#define MAXM (50000000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
struct Tree
{
int l, r;
int Max;
int lazy;
};
Tree tree[MAXN<<2];
int x[MAXN], y[MAXN];
void PushUp(int o){
tree[o].Max = max(tree[ll].Max, tree[rr].Max);
}
void PushDown(int o)
{
if(tree[o].lazy)
{
tree[ll].lazy += tree[o].lazy;
tree[rr].lazy += tree[o].lazy;
tree[ll].Max += tree[o].lazy;
tree[rr].Max += tree[o].lazy;
tree[o].lazy = 0;
}
}
void build(int o, int l, int r)
{
tree[o].Max = tree[o].lazy = 0;
tree[o].l = l, tree[o].r = r;
if(l == r)
return ;
int mid = (l + r) >> 1;
build(lson);
build(rson);
}
int rec[MAXN*2];
int Find(int val, int l, int r)
{
while(r >= l)
{
int mid = (l + r) >> 1;
if(rec[mid] == val)
return mid;
else if(rec[mid] > val)
r = mid-1;
else
l = mid+1;
}
}
void update(int o, int L, int R, int v)
{
if(L <= tree[o].l && R >= tree[o].r)
{
tree[o].lazy += v;
tree[o].Max += v;
return ;
}
PushDown(o);
int mid = (tree[o].l + tree[o].r) >> 1;
if(R <= mid)
update(ll, L, R, v);
else if(L > mid)
update(rr, L, R, v);
else
{
update(ll, L, mid, v);
update(rr, mid+1, R, v);
}
PushUp(o);
}
int main()
{
int t; Ri(t);
W(t)
{
int n; Ri(n);
int len = 1;
for(int i = 0; i < n; i++)
{
Ri(x[i]); Ri(y[i]);
rec[len++] = x[i];
rec[len++] = y[i];
}
sort(rec+1, rec+len);
int R = 2;
for(int i = 2; i < len; i++)
if(rec[i] != rec[i-1])
rec[R++] = rec[i];
sort(rec+1, rec+R);
build(1, 1, R-1);
for(int i = 0; i < n; i++)
{
if(x[i] > y[i])
swap(x[i], y[i]);
int l = Find(x[i], 1, R-1);
int r = Find(y[i], 1, R-1);
update(1, l, r, 1);
}
printf("%d\n", tree[1].Max);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: