lines
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1413 Accepted Submission(s): 576
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
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
1
//树状数组加离散化
//暑假学的树状数组和数据离散化又忘得差不多了。。。重新学了一遍。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 100100;
int bit[N*2], a[N*2];
int n;
struct node
{
int v, u;
}s[N];
int sum(int i)
{
int s = 0;
while(i > 0)
{
s += bit[i];
i -= i & - i;
}
return s;
}
void add(int i, int x)
{
while(i <= 2 * n) //离散化后最多n*2个点
{
bit[i] += x;
i += i & - i;
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
memset(bit, 0, sizeof bit);
int k = 0;
for(int i = 0; i < n; i++)
{
scanf("%d%d", &s[i].v, &s[i].u);
a[k++] = s[i].v;
a[k++] = s[i].u;
}
sort(a, a + k);
for(int i = 0; i < n; i++) //离散化
{
s[i].v = lower_bound(a, a + k, s[i].v) - a + 1;
s[i].u = lower_bound(a, a + k, s[i].u) - a + 1;
}
for(int i = 0; i < n; i++)
{
add(s[i].v, 1);
add(s[i].u + 1, -1);
}
int res = -1;
for(int i = 1; i <= 2 * n; i++) //离散化后最多n*2个点
res = max(res, sum(i));
printf("%d\n", res);
}
return 0;
}
线段树的做法,用cnt记录被覆盖的次数,不过效率较慢,所用时间接近限制,有一半的概率TLE,看人品。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N = 100100;
int a[N], b[N], tmp[N*2];
struct node
{
int l, r, cnt, mark;
}s[N*8];
void PushUp(int k)
{
s[k].cnt = max(s[k<<1].cnt, s[k<<1|1].cnt);
}
void PushDown(int k)
{
if(s[k].mark)
{
s[k<<1].cnt += s[k].mark;
s[k<<1|1].cnt += s[k].mark;
s[k<<1].mark += s[k].mark;
s[k<<1|1].mark += s[k].mark;
s[k].mark = 0;
}
}
void init(int l, int r, int k)
{
s[k].l = l, s[k].r = r, s[k].cnt = s[k].mark = 0;
if(l == r)
return;
int mid = (l + r) >> 1;
init(l, mid, k << 1);
init(mid + 1, r, k << 1|1);
}
void update(int l, int r, int k)
{
if(l <= s[k].l && s[k].r <= r)
{
s[k].cnt++;
s[k].mark++;
return;
}
PushDown(k);
int mid = (s[k].l + s[k].r) >> 1;
if(l <= mid) update(l, r, k << 1);
if(r > mid) update(l, r, k << 1|1);
PushUp(k);
}
int main()
{
int t, n;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
int k = 0;
for(int i = 0; i < n; i++)
{
scanf("%d%d", a + i, b + i);
tmp[k++] = a[i];
tmp[k++] = b[i];
}
sort(tmp, tmp + k);
int max1 = -1;
for(int i = 0; i < n; i++)
{
a[i] = lower_bound(tmp, tmp + k, a[i]) - tmp + 1;
b[i] = lower_bound(tmp, tmp + k, b[i]) - tmp + 1;
max1 = max(max1, max(a[i], b[i]));
}
init(1, max1, 1);
for(int i = 0; i < n; i++)
update(a[i], b[i], 1);
printf("%d\n", s[1].cnt);
}
return 0;
}