HDU 5124 lines (线段树+离散化)
题面
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
思路
可以说是两个板子结合。
代码实现
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
#define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i)
#define per(i,n,a) for (int i=n;i>=a;i--)
#define MT(x,i) memset(x,i,sizeof(x) )
#define rev(i,start,end) for (int i=start;i<end;i++)
#define inf 0x3f3f3f3f
#define mp(x,y) make_pair(x,y)
#define lowbit(x) (x&-x)
#define MOD 1000000007
#define exp 1e-8
#define N 1000005
#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int ,int> PII;
typedef pair<int ,PII> PIII;
ll gcd (ll a,ll b) {return b?gcd (b,a%b):a; }
const int maxn=1e5+10;
int t,n;
int le[maxn],ri[maxn];
int rec[maxn];
struct node {
int l,r;
int data,add;
}tree[maxn<<2];
int b[maxn],c[maxn];
int temp[maxn<<1];
inline void buildtree (int k,int l,int r) {
tree[k].l=l,tree[k].r=r;
tree[k].add=0,tree[k].data=0;
if (l==r) return ;
int mid=(l+r)>>1;
buildtree (k*2,l,mid);
buildtree (k*2+1,mid+1,r);
}
inline void lazydown (int k) {
if (tree[k].add) {
tree[k*2].add+=tree[k].add;
tree[k*2+1].add+=tree[k].add;
tree[k*2].data+=tree[k].add;
tree[k*2+1].data+=tree[k].add;
tree[k].add=0;
}
}
inline void update (int k,int l,int r) {
if (l==tree[k].l&&r==tree[k].r) {
tree[k].data++;
tree[k].add++;
return ;
}
lazydown (k);
int mid= (tree[k].l+tree[k].r)>>1;
// if (l<=mid) update (k*2,l,r);
// if (r>mid) update (k*2+1,l,r);
if (l>mid) update (k*2+1,l,r);
else if (r<=mid) update (k*2,l,r);
else {
update (k*2,l,mid);
update (k*2+1,mid+1,r);
}
tree[k].data=max (tree[k*2].data,tree[k*2+1].data);
}
int main () {
// freopen ("data.in","r",stdin);
cin>>t;
while (t--) {
cin>>n;
MT (b,0);
MT (c,0);
MT (temp,0);
int cnt=0;
rev (i,0,n) {
scanf ("%d %d",&b[i],&c[i]);
temp[cnt++]=b[i];
temp[cnt++]=c[i];
}
sort (temp,temp+cnt);
int m=unique (temp,temp+cnt)-temp;
buildtree (1,1,m);
rev (i,0,n) {
int tl=lower_bound (temp,temp+m,b[i])-temp+1;
int tr=lower_bound (temp,temp+m,c[i])-temp+1;
update (1,tl,tr);
}
cout<<tree[1].data<<endl;
}
// fclose (stdin);
return 0;
}