您的位置:首页 > 其它

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

2013-10-22 11:58 405 查看
 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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐