zoukankan      html  css  js  c++  java
  • HDU 4417-Super Mario-线段树+离线

    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的数的个数。

    核心思想:

    线段树+离线。

    代码如下:

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N=1e5+20;
    struct node{
    	int x,id;
    }a[N];
    struct Node{
    	int l,r,h,id,chu;
    }que[N];
    struct tnode{
    	int l,r,sum;
    }tr[N<<2];
    bool cmp0(node p,node q)
    {
    	return p.x<q.x;
    }
    bool cmp1(Node p,Node q)
    {
    	return p.h<q.h;
    }
    bool cmp2(Node p,Node q)
    {
    	return p.id<q.id;
    }
    void pushup(int m)
    {
    	tr[m].sum=tr[m<<1].sum+tr[m<<1|1].sum;
    	return;
    }
    void build(int m,int l,int r)
    {
    	tr[m].l=l;
    	tr[m].r=r;
    	if(l==r)
    	{
    		tr[m].sum=0;
    		return;
    	}
    	int mid=(l+r)>>1;
    	build(m<<1,l,mid);
    	build(m<<1|1,mid+1,r);
    	pushup(m);
    	return;
    }
    void update(int m,int x)
    {
    	if(tr[m].l==x&&tr[m].r==x)
    	{
    		tr[m].sum=1;
    		return;
    	}
    	int mid=(tr[m].l+tr[m].r)>>1;
    	if(x<=mid)
    		update(m<<1,x);
    	else
    		update(m<<1|1,x);
    	pushup(m);
    	return;
    }
    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);
    	if(l>mid)
    		return query(m<<1|1,l,r);
    	return query(m<<1,l,mid)+query(m<<1|1,mid+1,r);
    }
    int main()
    {
    	int T,ca=0;
    	cin>>T;
    	while(T--)
    	{
    		int n,m;
    		//输入并建树 
    		scanf("%d%d",&n,&m);
    		build(1,1,n);
    		for(int i=1;i<=n;i++)
    		{
    			scanf("%d",&a[i].x);
    			a[i].id=i;
    		}
    		//排序,小的先进树 
    		sort(a+1,a+1+n,cmp0);
    		//离线处理 
    		for(int i=1;i<=m;i++)
    		{
    			scanf("%d%d%d",&que[i].l,&que[i].r,&que[i].h);
    			que[i].l++;
    			que[i].r++;
    			que[i].id=i;
    		}
    		sort(que+1,que+1+m,cmp1);
    		//边更新边离线查询 
    		int k=1;
    		for(int i=1;i<=m;i++)
    		{
    			while(k<=n&&a[k].x<=que[i].h)
    			{
    				update(1,a[k].id);
    				k++;
    			}
    			que[i].chu=query(1,que[i].l,que[i].r);
    		}
    		//把顺序sort回来,输出 
    		sort(que+1,que+1+m,cmp2);
    		printf("Case %d:
    ",++ca);
    		for(int i=1;i<=m;i++)
    			printf("%d
    ",que[i].chu);
    	}
    	return 0;
    }
    
  • 相关阅读:
    UVA 10617 Again Palindrome
    UVA 10154 Weights and Measures
    UVA 10201 Adventures in Moving Part IV
    UVA 10313 Pay the Price
    UVA 10271 Chopsticks
    Restore DB後設置指引 for maximo
    每行SQL語句加go換行
    种服务器角色所拥有的权限
    Framework X support IPV6?
    模擬DeadLock
  • 原文地址:https://www.cnblogs.com/NothingbutFight/p/11307383.html
Copyright © 2011-2022 走看看