Super Mario HDU 4417(线段树+离线)

这篇博客探讨了一种使用离线方法和线段树数据结构解决马里奥跳跃问题的算法。文章首先介绍了问题背景,即马里奥需要在不同高度的砖块间跳跃救公主。接着,详细解释了如何存储和排序询问,以及如何构建和更新线段树。在算法实现部分,展示了C++代码,包括线段树的构建、更新和查询操作。最后,给出了样例输入和输出,展示算法如何工作。
摘要由CSDN通过智能技术生成

传送门

题面

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
把每条询问以结构体储存起来,按照H的值从小到大排列
同时记录每条询问的序号;
把数列也从小到大排列,记录序列号;

然后从“最小”的询问开始遍历,如果node[j].cnt<=op[i].sum,将这个数更新到线段树中,然后依次将每个询问
范围内的数用query()求出来储存再res数组中

代码

//离线+线段树 
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;

const int N = 1e5+10;

struct nod{
	int l,r,sum;
}tree[N<<2];

struct np{
	int id,cnt;
}node[N];

struct nd{
	int l,r,sum,id;
}op[N];

int a[N];

void pushup(int k)
{
	tree[k].sum = tree[k<<1].sum + tree[k<<1|1].sum;
}

void build(int k, int l, int r)
{
	tree[k] = {l,r,0};
	if(l==r)
	{
		return ;
	}
	int mid = (l+r)>>1;
	build(k<<1,l,mid);
	build(k<<1|1,mid+1,r);
	pushup(k);
}
void update(int k, int pos)
{
	if(tree[k].r==tree[k].l)
	{
		tree[k].sum = 1;
		return ;
	}
	int mid = (tree[k].l+tree[k].r)>>1;
	if(pos<=mid)
		update(k<<1,pos);
	else
		update(k<<1|1,pos);
	pushup(k);
}

int query(int k, int l, int r)
{
	if(tree[k].l>r||tree[k].r<l)
		return 0;
	if(tree[k].l>=l&&tree[k].r<=r)
		return tree[k].sum;
	return query(k<<1,l,r)+query(k<<1|1,l,r);
}
bool cmp(np a, np b)
{
	return a.cnt <b.cnt ;
}
bool cmp1(nd a, nd b)
{
	return a.sum < b.sum ;
}
int main()
{
//	freopen("E:\\000.txt","r",stdin);
	int t; cin >> t;
	for(int k=1;k<=t;k++)
	{
		int n,m,x;
		cin >> n >> m;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&x);
			node[i].id = i;
			node[i].cnt = x;
		}
		sort(node+1,node+1+n,cmp);
		for(int i=1;i<=m;i++)
		{
		//	scanf("%d%d%d",op[i].l ,op[i].r ,op[i].sum );
			cin >>op[i].l >> op[i].r >>op[i].sum;
			op[i].l++;
			op[i].r++;
			op[i].id = i;
		}
		sort(op+1,op+1+m,cmp1);
		build(1,1,n);
		int j=1;
		int res[N];
		for(int i=1;i<=m;i++)
		{
			while(op[i].sum>=node[j].cnt&&j<=n)
			{
				update(1,node[j].id);
				j++;
			}
			res[op[i].id] = query(1,op[i].l ,op[i].r );
		}
		printf("Case %d:\n",k);
		
		for(int i=1;i<=m;i++)
		printf("%d\n",res[i]);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值