hdu-4417 Super Mario

Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8377 Accepted Submission(s): 3546

Problem Description
Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss’s castle as a line (the length is n), on every integer point i there is a brick on height hi. Now the question is how many bricks in [L, R] Mario can hit if the maximal height he can jump is H.

Input
The first line follows an integer T, the number of test data.
For each test data:
The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries.
Next line contains n integers, the height of each brick, the range is [0, 1000000000].
Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)

Output
For each case, output “Case X: ” (X is the case number starting from 1) followed by m lines, each line contains an integer. The ith integer is the number of bricks Mario can hit for the ith query.

Sample Input
1
10 10
0 5 2 7 5 4 3 8 7 7
2 8 6
3 5 0
1 3 1
1 9 4
0 1 0
3 5 5
5 5 1
4 6 3
1 5 7
5 7 3

Sample Output
4
0
0
3
1
2
0
1
5
1

Source
2012 ACM/ICPC Asia Regional Hangzhou Online

题目大意
有 n 个元素和 m 次询问,每次询问第 L 个到第 R 个元素中不大于 k 的元素个数。

题解
比较裸的主席树。

代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e5+5,maxm=maxn*30;//主席树的内存不能开太小,我第一次开20就炸了
int T,n,m,w,tot,rot[maxn],a[maxn*2],b[maxn],le[maxn],ri[maxn],v[maxn];
//w表示去重后的个数 
struct nod{
    int L,R,s;//L和R表示左右儿子,s表示个数 
}t[maxm];
int read()
{
    int ret=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
    return ret*f;
}
void quchong()
{
    int n=w;
    sort(a+1,a+n+1);
    a[w=1]=a[1];
    for (int i=2;i<=n;i++)
      if (a[i]!=a[i-1]) a[++w]=a[i];
}
int fin(int x)
{
    for (int L=1,R=w,mid=R+L>>1;L<=R;mid=R+L>>1)
      if (x<a[mid]) R=mid-1;else
      if (x>a[mid]) L=mid+1;else
      return mid;
}
int build(int L,int R)
{
    int now=++tot;
    t[now].s=0;
    if (L==R) return now;
    int mid=R+L>>1;
    t[now].L=build(L,mid);
    t[now].R=build(mid+1,R);
    return now;
}
int addrot(int lst,int x,int dat)
{
    int newrot=++tot,ret=newrot;
    t[newrot].s=t[lst].s+dat;
    for (int L=1,R=w,mid=R+L>>1;L<R;mid=R+L>>1)
    {
        if (x<=mid)
        {
            t[newrot].L=++tot;
            t[newrot].R=t[lst].R;
            newrot=tot;
            lst=t[lst].L;
            R=mid;
        }else
        {
            t[newrot].L=t[lst].L;
            t[newrot].R=++tot;
            newrot=tot;
            lst=t[lst].R;
            L=mid+1;
        }
        t[newrot].s=t[lst].s+dat;
    }
    return ret;
}
int query(int le,int ri,int x)
{
    int ans=0,L=1,R=w,mid;
    while (L<R)
    {
        mid=R+L>>1;
        if (x<=mid)
        {
            le=t[le].L;
            ri=t[ri].L;
            R=mid;
        }else
        {
            ans+=t[t[ri].L].s-t[t[le].L].s;
            le=t[le].R;
            ri=t[ri].R;
            L=mid+1;
        }
    }
    return ans+t[ri].s-t[le].s;
}
int main()
{
    T=read();
    while (T--)
    {
        n=read();m=read();
        for (int i=1;i<=n;i++) a[i]=b[i]=read();
        w=n;
        for (int i=1;i<=m;i++) {le[i]=read();ri[i]=read();a[++w]=v[i]=read();}
        quchong();
        rot[0]=build(1,w);
        for (int i=1;i<=n;i++) rot[i]=addrot(rot[i-1],fin(b[i]),1);
        for (int i=1;i<=m;i++)
        {
            //题目把第 1 次当成第 0 次,所以是 ri[i]+1 而不是 le[i]-1 
            printf("%d\n",query(rot[le[i]],rot[ri[i]+1],fin(v[i])));
        }
    }
}

猜你喜欢

转载自blog.csdn.net/xu0_zy/article/details/80056802