【C++竞赛 G】Lines

Time Limit: 3s Memory Limit: 64MB
问题描述
Ljr has several lines. The lines are covered on the X axis. Let A is a point which is covered by the most lines. Ljr wants to know how many lines cover A.
输入描述
The first line contains a single integer T(1≤T≤100) (the data for N>100 less than 10 cases), indicating the number of test cases. Each test case begins with an integerN(1≤N≤〖10〗^5), indicating the number of lines. Next N lines contains two integers X_i and Y_i (-〖10〗^9≤X_i,Y_i≤〖10〗^9), describing a line.
输出描述
For each case, output an integer means how many lines cover A.
输入样例
2
5
1 2
2 3
2 4
3 4
5 1000
5
1 2
3 4
5 6
7 8
9 10
输出样例
3
1
?
【题目链接】:

【题解】

把区间端点离散化一下,然后就转换成区间最大值的问题了;
写个线段树就好;
x<=y不一定成立;

【完整代码】

#include <bits/stdc++.h>
#define rep1(i,a,b) for (int i = a;i <= b;i++)
using namespace std;
#define pb push_back;

const int MAXN = 1e5+10;

int ma[MAXN*2*4],tag[MAXN*2*4];
struct abc
{
    int l,r;
};

abc aa[MAXN];
vector <int> a;
map <int,int> dic;

void push_down(int rt)
{
    tag[rt<<1]+=tag[rt];
    tag[rt<<1|1]+=tag[rt];
    ma[rt<<1]+=tag[rt];
    ma[rt<<1|1]+=tag[rt];
    tag[rt] = 0;
}

void up_data(int L,int R,int l,int r,int rt)
{
    //printf("%d %d\n",l,r);
    if (L<=l && r <= R)
    {
        ma[rt]++;
        tag[rt]++;
        return;
    }
    if (tag[rt]!=0)
        push_down(rt);
    int m = (l+r)>>1;
    if (L<=m)
        up_data(L,R,l,m,rt<<1);
    if (m<R)
        up_data(L,R,m+1,r,rt<<1|1);
    ma[rt] = max(ma[rt<<1],ma[rt<<1|1]);
}

int main()
{
    //freopen("D:\\rush.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while (T--)
    {
        memset(ma,0,sizeof(ma));
        memset(tag,0,sizeof(tag));
        dic.clear();
        a.clear();
        int n;
        scanf("%d",&n);
        rep1(i,1,n)
        {
            scanf("%d%d",&aa[i].l,&aa[i].r);
            if (aa[i].l>aa[i].r)
                swap(aa[i].l,aa[i].r);
            if (!dic[aa[i].l])
            {
                a.push_back(aa[i].l);
                dic[aa[i].l] = 1;
            }
            if (!dic[aa[i].r])
            {
                a.push_back(aa[i].r);
                dic[aa[i].r] = 1;
            }
        }
        sort(a.begin(),a.end());
        rep1(i,1,n)
        {
            int l,r;
            l = lower_bound(a.begin(),a.end(),aa[i].l)-a.begin()+1;
            r = lower_bound(a.begin(),a.end(),aa[i].r)-a.begin()+1;
            up_data(l,r,1,MAXN<<1,1);
        }

        printf("%d\n",ma[1]);
    }
    return 0;
}
posted @ 2017-10-04 18:45  AWCXV  阅读(330)  评论(0编辑  收藏  举报