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;
}
全部评论

相关推荐

牛舌:如果我不想去,不管对方给了多少,我一般都会说你们给得太低了。这样他们就会给下一个offer的人更高的薪资了。
点赞 评论 收藏
分享
三年之期已到我的offer快到碗里来:9硕都比不上9本
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务