LightOJ1089

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26806

题目大意:略

题目思路:前缀和与离散化

可用线段树做,但是前缀和更简单

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <climits>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define Min(x,y) (x<y?x:y)
#define Max(x,y) (x>y?x:y)
using namespace std;
#define gamma 0.5772156649015328606065120 //欧拉常数
#define MOD 100000007
#define inf 0x3f3f3f3f
#define N 50010
#define maxn 10001000
typedef long long LL;
typedef pair<int,int> PII;

int n,m,a[N<<1],res[N<<1];
PII p[N];

int main()
{
    int i,x,y,v,group,Case=0;
    //freopen("in.txt","r",stdin);
    scanf("%d",&group);
    while(group--)
    {
        mst(res,0);
        int cnt=0;
        scanf("%d%d",&n,&m);
        for(i=0; i<n; ++i)
        {
            scanf("%d%d",&p[i].fi,&p[i].se);
            a[cnt++]=p[i].fi;
            a[cnt++]=++p[i].se;
        }
        stable_sort(a,a+cnt);
        cnt=unique(a,a+cnt)-a;
        for(i=0; i<n; ++i)
        {
            int l=lower_bound(a,a+cnt,p[i].fi)-a;
            int r=lower_bound(a,a+cnt,p[i].se)-a;
            ++res[l];--res[r];
        }
        for(i=1; i<cnt; ++i)
            res[i]+=res[i-1];
        printf("Case %d:\n",++Case);
        for(i=0; i<m; ++i)
        {
            scanf("%d",&x);
            int pos=upper_bound(a,a+cnt,x)-a;
            printf("%d\n",res[pos-1]);
        }
    }
    return 0;
}
时间: 2024-08-04 04:10:43

LightOJ1089的相关文章

LightOj1089(求点包含几个线段 + 线段树)

题目链接 题意:n( n <= 50000 ) 个线段,q ( q <= 50000) 个点,问每个点在几个线段上 线段端点的和询问的点的值都很大,所以必须离散化 第一种解法:先把所有的线段端点和询问点,离散处理,然后对于每条选段处理,c[x]++, c[y + 1]--,然后令c[x] = c[x] + c[x - 1],所以c[x]就保存了被几个线段覆盖,然后对对于每个询问点,查找他在离散后的位置,然后直接读取c[],这种方法很巧妙,佩服佩服 1 #include <iostream