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

刚刚写了两个离线,感觉就是先保存下来再排序来达到本来无法达到的操作

#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
struct node1{
   
	int l,r;
	long long sum;
}a[400005];
struct node2{
   
	int l,r,id,h;
	long long sum;
}b[100005];
struct node3{
   
	int id,h;
}c[100005];
int n,m;
void build(int l,int r,int k){
   
	a[k].l=l;
	a[k].r=r;
	a[k].sum=0;
	if(l==r)	return;
	int mid=(l+r)/2;
	build(l,mid,k*2);
	build(mid+1,r,k*2+1);
}
bool cmp(node2 x,node2 y){
   
	return x.h<y.h;
}
bool cmp1(node2 x,node2 y){
   
	return x.id<y.id;
}
bool cmp2(node3 x,node3 y){
   
	return x.h<y.h;
}
void update(int dian,int k){
   
	if(a[k].l==a[k].r){
   
		a[k].sum=1;
		return;
	}
	int mid=(a[k].l+a[k].r)/2;
	if(dian<=mid)	update(dian,k*2);
	else	update(dian,2*k+1);
	a[k].sum=a[k*2].sum+a[k*2+1].sum;
}
long long query(int l,int r,int k){
   
	if(l<=a[k].l&&r>=a[k].r){
   
		return a[k].sum;
	}
	int mid=(a[k].l+a[k].r)/2;
	long long res=0;
	if(l<=mid)	res+=query(l,r,k*2);
	if(r>mid)	res+=query(l,r,k*2+1);
	return res;
}
int main(){
   
	int t;
	scanf("%d",&t);
	for(int ttt=1;ttt<=t;ttt++){
   
		printf("Case %d:\n",ttt);
		scanf("%d%d",&n,&m);
		build(0,n-1,1);
		for(int i=0;i<n;i++){
   
			scanf("%d",&c[i].h);
			c[i].id=i;
		}
		for(int i=0;i<m;i++){
   
			scanf("%d%d%d",&b[i].l,&b[i].r,&b[i].h);
			b[i].id=i;
			b[i].sum=0;
		}
		sort(b,b+m,cmp);
		sort(c,c+n,cmp2);
		int j=0;
		for(int i=0;i<m;i++){
   
			while(b[i].h>=c[j].h&&j<n){
   
				update(c[j].id,1);
				j++;
			}
			b[i].sum=query(b[i].l,b[i].r,1);
		}
		sort(b,b+m,cmp1);
		for(int i=0;i<m;i++)
			printf("%d\n",b[i].sum);
	}
	return 0;
}
全部评论

相关推荐

xiaolihuam...:当然还有一种情况是你多次一面挂,并且挂的原因都比较类似,例如每次都是算法题写不出来。面试官给你的评价大概率是算法能力有待加强,算法能力有待提高,基础知识掌握的不错,项目过关,但是coding要加强。短期内高强度面试并且每次都是因为同样的原因挂(这个你自己肯定很清楚),会形成刻板印象,因为你偶尔一次算法写不出来,面试官自己也能理解,因为他清楚的知道自己出去面试也不一定每一次面试算法都能写出来。但是连续几次他发现你的面屏里面都是算法有问题,他就认为这不是运气问题,而是能力问题,这种就是很客观的评价形成了刻白印象,所以你要保证自己。至少不能连续几次面试犯同样的错。算法这个东西比较难保证,但是有些东西是可以的,例如某一轮你挂的时候是因为数据库的索引,这个知识点答的不好,那你就要把数据库整体系统性的复习,下一轮面试你可以,项目打的不好,可以消息队列答的不好,但是绝对不可以数据库再答的不好了。当然事实上对于任何面试都应该这样查漏补缺,只是对于字节来说这个格外重要,有些面试官真的会问之前面试官问过的问题
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务