HDU 4471 Super Mario(主席树)

Problem Description

\quad 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 ] [L, R] [L,R] Mario can hit if the maximal height he can jump is H H H.

Input

\quad The first line follows an integer T T T, the number of test data.
For each test data:
The first line contains two integers n , m ( 1 ≤ n ≤ 1 0 5 , 1 ≤ m ≤ 1 0 5 ) n,m\left(1≤n≤10^{5},1≤m≤10^{5} \right) nm(1n1051m105), n n n is the length of the road, m is the number of queries.
Next line contains n n n integers, the height of each brick, the range is [ 0 , 1000000000 ] [0, 1000000000] [0,1000000000].
Next m lines, each line contains three integers L , R , H ( 0 ≤ L ≤ R ≤ n , 0 ≤ H ≤ 1000000000 ) L,R,H\left(0≤L≤R≤n,0≤H≤1000000000 \right) L,R,H(0LRn0H1000000000)

Output

\quad For each case, output " C a s e X : " "Case X: " "CaseX:" ( X X X is the case number starting from 1 1 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

Case 1:

4
0
0
3
1
2
0
1
5
1
\quad 题意就是 n n n个数,给你 m m m个询问,然后每个询问给 l , r , k l,r,k l,r,k意思就是查询 l l l r r r中有多少数比 k k k小的。
题意很简单这道题就是标准的主席树题目。建树1部分和模板一样。查询过程判断每一层是否满足当前版本的线段树的值大于k,满足的话返回 r r r版本的节点个数减去 l − 1 l-1 l1版本的节点个数。

AC代码:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
    int ret = 0, sgn = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')
            sgn = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        ret = ret*10 + ch - '0';
        ch = getchar();
    }
    return ret*sgn;
}
inline void Out(int a)    //Êä³öÍâ¹Ò
{
    if(a>9)
        Out(a/10);
    putchar(a%10+'0');
}

int qpow(int m, int k, int mod)
{
    int res = 1, t = m;
    while (k)
    {
        if (k&1)
            res = res * t % mod;
        t = t * t % mod;
        k >>= 1;
    }
    return res;
}

ll gcd(ll a,ll b)
{
    return b==0?a : gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
    return a*b/gcd(a,b);
}

const int maxn = 2e5+5;
vector<int> v;
int a[maxn];
int n,m;
struct ndoe
{
    int l,r,sum;
} hjt[maxn*40];
int cnt,root[maxn];

inline int getid(int x)
{
    return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}///离散化二分查找

void inset(int l,int r,int pre,int &now,int p)
{
    hjt[++cnt]=hjt[pre];///当前版本的线段树继承上一个版本的线段树
    now=cnt;
    hjt[now].sum++;///节点数量++
    if(l==r)
        return ;
    int mid = (l+r)>>1;
    if(p<=mid)
    {
        inset(l,mid,hjt[pre].l,hjt[now].l,p);
    }
    else
    {
        inset(mid+1,r,hjt[pre].r,hjt[now].r,p);
    }
}

int querry(int l,int r,int x,int y,int k)///r版本的线段树减去l版本的线段树
{
    if(r<=k)
        return hjt[y].sum-hjt[x].sum;
    int mid=(l+r)>>1;
    if(k<=mid)
        return querry(l,mid,hjt[x].l,hjt[y].l,k);
    else
        return hjt[hjt[y].l].sum-hjt[hjt[x].l].sum+querry(mid+1,r,hjt[x].r,hjt[y].r,k);
    ///把左子树的加起来使得递归少一层。
}

int main()
{
    int t;
    sd(t);
    int cas=1;
    while(t--)
    {
        printf("Case %d:\n",cas++);
        v.clear();
        sd(n);
        sd(m);
        rep(i,1,n)
        {
            sd(a[i]);
            v.pb(a[i]);
        }
        cnt=0;
        sort(v.begin(),v.end());
        v.erase(unique(v.begin(),v.end()),v.end());
        ///unique去重后的数组元素,把重复的删掉,去重便于二分查找
        rep(i,1,n)
        {
            inset(1,n,root[i-1],root[i],getid(a[i]));
        }
        while(m--)
        {
            int l,r,k;
            sddd(l,r,k);
            l++;
            r++;
            int pos=upper_bound(v.begin(),v.end(),k)-v.begin();
            ///找到大于等于k的第一个元素。
            if(pos)
            {
                int ans=querry(1,n,root[l-1],root[r],pos);
                printf("%d\n",ans);
            }
            else
                printf("0\n");
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值