HDU-4417:Super Mario(树状数组+离线操作)

本文详细讲解了如何使用树状数组和离线操作技巧解决HackerEarth题目SuperMario中的问题,涉及砖块高度查询、离散化处理和二分查找。博主分享了代码实现以及关键步骤,包括构建树状数组、离散化查询范围和计算答案区间。

题目描述

HDU-4417:Super Mario(树状数组+离线操作)
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
Case 1:
4
0
0
3
1
2
0
1
5
1

题意

每次询问[L,R]中,小于等于H的数字的个数

我的想法

1.输入每个砖块的高度,num[i].number , 并且记录index = i.排序,并且离散化(离散化的含义就是将num[i]的number,变成与原来具有相同位置,但是更小的一个数,并用ma[i]表示)。
2.输入每次的询问,到query[]中,每次输入,都要二分寻找他询问的高度在离散化后的num中大概排在哪里(就是代表他的那个数字,num[i].number <= query[j].number)。
3.根据每个query的L , number(高度),计算当输入了前L个,的getSum(number)。
4.同3计算r的。
5.4 的R - 3的L,就是答案。
tips:ma表示离散化,maa表示query[i].number在ma中为几?
MD因为排序query的时候用了n而不是m,debug了一年

代码实现(比较长,更为简介的代码看下面)

#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>

using namespace std;
const int N = 1e5 + 1e3;
typedef long long int LL;
struct Node{
	LL number , index;
}num[N];
struct Node1{
	LL l , r , number;
	LL ln , rn;
	LL index;
}query[N];

LL n , m , cnum[N] , ans[N];
map<LL,LL> ma;
map<LL,LL> maa;

LL lowbit(LL x){
	return x&(-x);
}
bool cmp(Node a , Node b){
	return a.number<b.number;
}
bool cmp_query1(Node1 a ,Node1 b){
	return a.l<b.l;
}
bool cmp_query2(Node1 a , Node1 b){
	return a.r<b.r;
}
void add(LL index , LL num){
	for(LL i = index ; i <= n ; i += lowbit(i)){
		cnum[i] += num;
	}
}
LL getSum(LL index){
	LL res = 0;
	for(LL i = index ; i > 0 ; i -= lowbit(i)){
		res += cnum[i];
	}
	return res;
}
bool check(LL mid , LL numm){
	if(num[mid].number<=numm) return true;
	else return false;
}
int main(){
	ios::sync_with_stdio(false);
	LL i , j , t;
	cin>>t;
	for(i = 1 ; i <= t ; i ++ ){
		cin>>n>>m;
		memset(cnum , 0 , sizeof(cnum));
		ma.clear();
		maa.clear();
		
		for(j = 1 ; j <= n ; j ++ ){
			cin>>num[j].number;
			num[j].index = j;
		}
		sort(num+1,num+1+n,cmp);
		LL cnt = 1;
		ma[num[1].index] = cnt;
		for(j = 2 ; j <= n ; j ++ ){
			if(num[j].number == num[j-1].number) ma[num[j].index]=cnt;
			else ma[num[j].index] = ++cnt;
		}
//		for(j = 1 ; j <= n ; j ++ ) cout<<ma[j]<<" ";
//		cout<<endl;
		for(j = 1 ; j <= m ; j ++ ){
			cin>>query[j].l>>query[j].r>>query[j].number;
			query[j].index = j;
			query[j].l++;query[j].r++;
			int l = 1 , r = n;
			while(l<r){
				int mid = (l+r+1)>>1;
				if(check(mid , query[j].number)) l = mid;
				else r = mid-1;
			}
			if(num[l].number<=query[j].number) maa[query[j].number] = ma[num[l].index];
			else maa[query[j].number] = 0;
//			cout<<" "<<maa[query[j].number]<<endl;;		
		}//二分 
		sort(query+1,query+1+m,cmp_query1);
		cnt = 1;
		for(j = 1 ; j <= n ; j ++ ){
			while(cnt<=m && query[cnt].l==j){
				query[cnt].ln = getSum(maa[query[cnt].number]);
				cnt++; 
			}
			add(ma[j],1);
		}//两遍,一边l , 一边r 
		cnt = 1;
		sort(query+1,query+1+m,cmp_query2);
		memset(cnum , 0 , sizeof(cnum));
		for(j = 1 ; j <= n ; j ++ ){
			add(ma[j] , 1);
			while(cnt<=m && query[cnt].r == j){
				query[cnt].rn = getSum(maa[query[cnt].number]);
				cnt++;
			}
		}
		for(j = 1 ; j <= m ; j ++ ){
//			cout<<query[j].l<<" "<<query[j].r<<" "<<query[j].number<<" "<<query[j].ln<<" "<<query[j].rn<<endl;
			ans[query[j].index] = query[j].rn-query[j].ln;
		}
		cout<<"Case "<<i<<":"<<endl;
		for(j = 1 ; j <= m ; j ++ ){
			cout<<ans[j]<<endl;
		}
	}
	
	return 0;
}

大佬代码的思路

树状数组记录下表
简要:枚举询问,对于询问的高度 大于等于 砖块(的高度)的下标放入树状数组中,然后,计算getSum(r)-getSum(l-1);

大佬代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
#include <functional>
#define rank ra
#define lson rt<<1
#define rson rt<<1|1
#define pb push_back
using namespace std;
typedef long long ll;
int n,m;
int cnt[100010];
int ans[100010];    //保存答案
struct no1
{
    int idx;
    int x;
}a[100010];
struct node
{
    int l,r;
    int h;
    int idx;
}qu[100010];
bool cmp1(no1 p,no1 q)
{
    return p.x<q.x;
}
bool cmp2(node p,node q)
{
    return p.h<q.h;
}
void add(int x)     //正常的树状数组
{
    while(x<=n)
    {
        cnt[x]++;
        x += x&(-x);
    }
}
int sum(int i)
{
    int s=0;
    while(i>0)
    {
        s+=cnt[i];
        i -= i&(-i);
    }
    return s;
}
int main()
{
    int QAQ;
    scanf("%d",&QAQ);
    int kase=0;
    while(QAQ--)
    {
        memset(cnt,0,sizeof(cnt));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i].x);
            a[i].idx=i;
        }
        int l,r,h;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&l,&r,&h);
            qu[i].l=l+1;       //记得加1
            qu[i].r=r+1;
            qu[i].idx=i;
            qu[i].h=h;
        }
        sort(a+1,a+1+n,cmp1);
        sort(qu+1,qu+1+m,cmp2);
        printf("Case %d:\n",++kase);
        for(int i=1,j=1;j<=m;j++)   //按照查询的h依次加入
        {
            while(i<=n&&qu[j].h>=a[i].x)
            {
                add(a[i].idx);
                i++;
            }
            ans[qu[j].idx]=sum(qu[j].r)-sum(qu[j].l-1);
        }
        for(int i=1;i<=m;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}

试着写了一下,但是wa了。。呜呜呜

大佬的博客
我用#CSDN#这个app发现了有技术含量的博客,小伙伴们求同去《HDU-4417:Super Mario(树状数组+离线操作)》, 一起来围观吧 https://blog.csdn.net/f2935552941/article/details/77180467?utm_source=app&app_version=4.11.0&code=app_1562916241&uLinkId=usr1mkqgl919blen

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值