第九届湖南省大学生程序设计竞赛H题 高桥,低桥(UVA 12663) BIT 树状数组

 There are one high bridge and one low bridge across the river. The river has flooded twice, why the high bridge is flooded twice but the low bridge is flooded only once?

A: Because the lower bridge is so low that it's still under water after the first flood is over.

If you're confused, here's how it happens:

  • Suppose high bridge and low bridge's heights are 2 and 5, respectively, and river's initial water level is 1.
  • First flood: the water level is raised to 6(Both bridges are flooded), and then back to 2(high bridge is not flooded anymore, but low bridge is still flooded).
  • Second flood: the water level is raised to 8(The high bridge is flooded again), and then back to 3.

Just a word game, right? The key is that if a bridge is still under water (i.e. the water level is no less than the bridge height) after a flood, then next time it will not be considered flooded again.

Suppose the i-th flood raises the water level to ai and then back to bi. Given n bridges' heights, how many bridges are flooded at least k times? The initial water level is 1.

题解:有m次询问,每次都有涨潮和退潮点,首先应该想到的应该是利用效率高的数据结构去询问解决。可以利用BIT树状数组。(我是尝试过暴力不过没有过。。刘汝佳的题数据果然还是卡的死)

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
int a[100005],b[100005];
int n,m,k,l,r;
void BIT(int i,int d)
{
    while(i<=n)
    {
        b[i]+=d;
        i+=i&-i;
    }
}
int  Sum(int i)
{
    int sum=0;
    while(i)
    {
        sum+=b[i];
        i-=i&-i;
    }
    return sum;
}
int main()
{
    int tmp,count=1;
    while(cin>>n>>m>>k)
    {
        tmp=0;
        memset(b,0,sizeof(b));
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        sort(a+1,a+n+1);
        int pre=0;
        while(m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            r=upper_bound(a+1,a+n+1,x)-a;
            l=upper_bound(a+1,a+n+1,pre)-a;
            BIT(l,1);
            BIT(r,-1);
            pre=y;
        }
        for(int i=1;i<=n;i++)
        {
            if(Sum(i)>=k)
                tmp++;
        }
        cout<<"Case "<<count++<<": ";
        cout<<tmp<<endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值