hdu 4417 Super Mario 夜

http://acm.hdu.edu.cn/showproblem.php?pid=4417

线段树 or 树状数组 都可以

先把问题 和 和 砖块 分别按高度排序

在求解区间答案时 根据要求的高度 将小于这个高度的砖块全部插入 再求解即可

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>

#define LL long long
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int N=100005;
int c[N];
struct q
{
    int l,r,h,I;
    int ans;
}qt[N];
struct inh
{
    int h,I;
}in[N];
int n;
int lowbit(int x)
{
    return x&(-x);
}
void Add(int x)
{
    while(x<=n)
    {
        ++c[x];
        x+=lowbit(x);
    }
}
int Sum(int x)
{
    int temp=0;
    while(x>=1)
    {
        temp+=c[x];
        x-=lowbit(x);
    }
    return temp;
}
bool cmpqt(q x,q y)
{
    return x.h<y.h;
}
bool cmpin(inh x,inh y)
{
    return x.h<y.h;
}
bool cmpI(q x,q y)
{
    return x.I<y.I;
}
int main()
{
    //freopen("data.txt","r",stdin);
    int T;
    scanf("%d",&T);
    for(int ca=1;ca<=T;++ca)
    {
        int m;
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&in[i].h);
            in[i].I=i;
        }
        for(int i=0;i<m;++i)
        {
            scanf("%d %d %d",&qt[i].l,&qt[i].r,&qt[i].h);
            ++qt[i].l;++qt[i].r;
            qt[i].I=i;
        }
        sort(qt,qt+m,cmpqt);
        sort(in+1,in+n+1,cmpin);
        memset(c,0,sizeof(c));
        int l=1;
        for(int i=0;i<m;++i)
        {
            while(in[l].h<=qt[i].h&&l<=n)
            {
                Add(in[l].I);
                ++l;
            }
            qt[i].ans=Sum(qt[i].r)-Sum(qt[i].l-1);
        }
        sort(qt,qt+m,cmpI);
        printf("Case %d:\n",ca);
        for(int i=0;i<m;++i)
        printf("%d\n",qt[i].ans);

    }
    return 0;
}

原文地址:https://www.cnblogs.com/liulangye/p/2703489.html