HDU-4417(可持续化线段树or莫队)

Problem Description
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的值的个数。注意,这里的l和r是从0开始的。
解题思路:这个题我们可以用主席树和莫队来做,先讲主席树的做法叭,我们可以维护一个权值线段树,然后题目就转换为在区间[l,r]中权值为1-h的个数,所以用主席树求区间和就可以了。
代码:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <cmath>
#include <set>
using namespace std;
#define int1 long long
#define ls root<<1
#define rs root<<1|1
#define QL ls,l,r
#define QR rs,l,r
const int maxn=1e5+7;
int pos[maxn];
struct tree
{
    int l,r,date;
}t[maxn<<5];
int tot,head[maxn],a[maxn];
inline void build(int &root,int l,int r)
{
    root=++tot;
    t[root]=tree{0,0,0};
    if(l==r)return;
    int mid=l+r>>1;
    build(t[root].l,l,mid);
    build(t[root].r,mid+1,r);
}
inline void update(int pre,int &now,int l,int r,int c)
{
    now=++tot;
    t[now]=t[pre];
    t[now].date++;
    if(l==r)return;
    int mid=l+r>>1;
    if(c<=mid)update(t[pre].l,t[now].l,l,mid,c);
    else update(t[pre].r,t[now].r,mid+1,r,c);
}
inline int query(int pre,int now,int l,int r,int c)
{
    if(l==r)return t[now].date-t[pre].date;
    int mid=l+r>>1,ans=0;
    if(r<=c)return t[now].date-t[pre].date;
    if(l<=c){
        ans+=query(t[pre].l,t[now].l,l,mid,c);
        if(mid<c) ans+=query(t[pre].r,t[now].r,mid+1,r,c);
    }
    return ans;
}
signed main() {
    int T,cas=1;
    scanf("%d",&T);
    while(T--){
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            scanf("%d",pos+i);
            a[i]=pos[i];
        }
        printf("Case %d:\n",cas++);
        tot=0;
        sort(pos+1,pos+1+n);
        int len=unique(pos+1,pos+1+n)-pos-1;
//        for(int i=1;i<=len;i++){
//        	cout<<pos[i]<<' ';
//		}
//		cout<<endl;
        build(head[0],1,len);
        for(int i=1;i<=n;i++){
            int x=lower_bound(pos+1,pos+len+1,a[i])-pos;
            update(head[i-1],head[i],1,n,x);
        }
        while(m--){
            int l,r,h;
            scanf("%d%d%d",&l,&r,&h);
            //cout<<h<<endl;
            l++,r++;
            int x=lower_bound(pos+1,pos+1+len,h)-pos;
			//cout<<h<<endl;
			if(pos[x]>h)x--;
            printf("%d\n",query(head[l-1],head[r],1,n,x));
        }
    }
}

然后我们来讲一下莫队的做法叭,莫队就是分块,然后暴力扫过去,我们维护一个树状数组,然后我们在加入这个数的时候就将这个位置+1,离开就-1,最后求出满足区间的小于等于h所在位置的和就可以了。
代码:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <cmath>
#include <set>
using namespace std;
#define int1 long long
#define ls root<<1
#define rs root<<1|1
#define QL ls,l,r
#define QR rs,l,r
const int maxn=1e5+7;
struct node
{
	int l,r,x,pos,id;
	bool operator <(node a)const{
		 return a.pos==pos?r<a.r:pos<a.pos;
	}
}e[maxn];
int len;
int sum[maxn];
inline void add(int x,int d)
{
	while(x<=len){
		sum[x]+=d;
		x+=(x&-x);
	}
}
inline int ask(int x)
{
	int ans=0;
	while(x){
		ans+=sum[x];
		x-=(x&-x);
	}
	return ans;
}
int a[maxn],ans[maxn],b[maxn];
int main()
{
	int t,cas=1;
	scanf("%d",&t);
	while(t--){
		int n,m;
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++){
			scanf("%d",a+i);
			b[i]=a[i];
		}
		sort(a+1,a+1+n);
		len=unique(a+1,a+1+n)-a;
		for(int i=1;i<=n;i++){
			b[i]=lower_bound(a+1,a+len,b[i])-a;
		}
		int le=sqrt(n);
		for(int i=1;i<=m;i++){
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			e[i]=node{x+1,y+1,z,x/le,i};
		}
		sort(e+1,e+1+m);
//		for(int i=1;i<=m;i++){
//			cout<<e[i].l<<' '<<e[i].r<<' '<<e[i].x<<' '<<e[i].id<<endl;
//		}
//		cout<<endl;
		int l=1,r=0;
		memset(sum,0,sizeof sum);
		for(int i=1;i<=m;i++){
			while(l<e[i].l){
				add(b[l++],-1);
			}
			while(l>e[i].l){
				add(b[--l],1);
			}
			while(r<e[i].r){
				//int x=lower_bound(a+1,a+len,++r)-a;
				add(b[++r],1);
			}
			while(r>e[i].r){
				//int x=lower_bound(a+1,a+len,r--)-a;
				add(b[r--],-1);
			}
			int x=lower_bound(a+1,a+len,e[i].x)-a;
			if(a[x]>e[i].x)x--;
			ans[e[i].id]=ask(x);
		}
		printf("Case %d:\n",cas++);
		for(int i=1;i<=m;i++){
			printf("%d\n",ans[i]);
		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值