Super Mario--线段树或树状数组+离散化+离线处理

题目链接:https://cn.vjudge.net/problem/HDU-4417

题目大意

给定一个长度为n的序列,m个询问,每次询问时,马里奥都有一个跳跃的最大值,问在此区间内马里奥能越过的障碍有多少个(不考虑中间被阻挡)。

分析

其实就是求某个区间内,障碍高度小于等于h的有多少个。先把障碍的高度离散化,同时记录下标,再把区间存下,同时记录下标,将区间按h排序,然后遍历每个区间,将低于该区间h的数放置,最后求区间和。

线段树写法

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
const int N = 100010;
int n,m,ans[N];
struct node{
	int l,r,sum;
}tr[N<<2];
struct Ques{
	int l,r,id,h;
}q[N];
struct A{
	int id,val;
}a[N];
bool cmp1(const Ques x,const Ques y)
{
	return x.h < y.h;
}
bool cmp2(const A x,const A y)
{
	return x.val < y.val;
}
void pushup(int m)
{
	tr[m].sum = tr[m<<1].sum + tr[m<<1|1].sum;
}
void build(int m,int l,int r)
{
	tr[m].l = l;
	tr[m].r = r;
	tr[m].sum = 0;
	if(l == r) return ;
	int mid = (l + r) >> 1;
	build(m<<1,l,mid);
	build(m<<1|1,mid+1,r);
}
void updata(int m,int id)
{
	if(tr[m].l == id && tr[m].r == id)
	{
		tr[m].sum = 1;
		return ;
	}
	int mid = (tr[m].l + tr[m].r) >> 1;
	if(id <= mid)
		updata(m<<1,id);
	else
		updata(m<<1|1,id);
	pushup(m);
}
int query(int m,int l,int r)
{
	if(tr[m].l == l && tr[m].r == r)
		return tr[m].sum;
	int mid = (tr[m].l + tr[m].r) >> 1;
	if(r <= mid)
		return query(m<<1,l,r);
	else if(l > mid)
		return query(m<<1|1,l,r);
	else
		return query(m<<1,l,mid) + query(m<<1|1,mid+1,r);
}
int main()
{
	int t,cas = 1;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i].val);
			a[i].id = i;
		}
		sort(a,a+n,cmp2);
		build(1,0,n-1);
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].h);
			q[i].id = i;
		}
		int j = 0;
		sort(q+1,q+m+1,cmp1);
		for(int i=1;i<=m;i++)
		{
			while(a[j].val <= q[i].h && j < n)
			{
				updata(1,a[j].id);
				j++;
			}
			ans[q[i].id] = query(1,q[i].l,q[i].r);
		}
		printf("Case %d:\n",cas++);
		for(int i=1;i<=m;i++)
			printf("%d\n",ans[i]);
	}
	return 0;
}

树状数组写法:要将整个区间右移一个单位

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 1e5+5;
int n,m,c[2*N],ans[N];
struct A{
	int id,val;
}a[N];
struct Ques{
	int id,l,r,h;
}q[N];
bool cmp1(const Ques x,const Ques y)
{
	return x.h < y.h;
}
bool cmp2(const A x,const A y)
{
	return x.val < y.val;
}
int lowbit(int x)
{
	return x & (-x);
}
int getsum(int x)
{
	int ans = 0;
	while(x)
	{
		ans += c[x];
		x -= lowbit(x);
	}
	return ans;
}
void add(int x,int val)
{
	while(x < 2*N)
	{
		c[x] += val;
		x += lowbit(x);
	}
}
int main()
{
	int t,cas = 1;
	scanf("%d",&t);
	while(t--)
	{
		memset(c,0,sizeof c);
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i].val);
			a[i].id = i;
		}
		sort(a+1,a+n+1,cmp2);
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].h);
			q[i].id = i;
		}
		int j = 1;
		sort(q+1,q+m+1,cmp1);
		for(int i=1;i<=m;i++)
		{
			while(a[j].val <= q[i].h && j <= n)
			{
				add(a[j].id,1);
				j++;
			}
			ans[q[i].id] = getsum(q[i].r + 1) - getsum(q[i].l);
		}
		printf("Case %d:\n",cas++);
		for(int i=1;i<=m;i++)
			printf("%d\n",ans[i]);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值