第九届湖南省大学生程序设计竞赛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树状数组。(我是尝试过暴力不过没有过。。刘汝佳的题数据果然还是卡的死)
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; } }
相关文章推荐
- CSU 1335: 高桥和低桥(树状数组啊 二分查找啊 湖南省第九届大学生计算机程序设计竞赛)
- 湖南省第九届大学生计算机程序设计竞赛 高桥和低桥
- 湖南省第九届大学生计算机程序设计竞赛 高桥和低桥
- 湖南省第九届大学生程序设计竞赛
- 湖南省第九届大学生计算机程序设计竞赛 Interesting Calculator
- 湖南省第九届大学生计算机程序设计竞赛 Interesting Calculator
- 湖南省第九届大学生计算机程序设计竞赛 G - 一行盒子
- 湖南省第九届大学生程序设计竞赛
- 湖南省第九届大学生计算机程序设计竞赛B 搞笑版费马大定理
- Interesting Calculator( 湖南省第九届大学生计算机程序设计竞赛)
- 湖南省第九届大学生计算机程序设计竞赛1337: 搞笑版费马大定理
- CSU 1330: 字符识别?(字符串模拟啊 湖南省第九届大学生计算机程序设计竞赛)
- 搞笑版费马大定理 (湖南省第九届大学生计算机程序设计竞赛)
- 第九届湖南省大学生程序设计竞赛部分题解
- CSU 1336: Interesting Calculator(BFS啊 湖南省第九届大学生计算机程序设计竞赛)
- CSU 1334: 好老师(数学啊 湖南省第九届大学生计算机程序设计竞赛)
- CSU 1337: 搞笑版费马大定理(数学啊 湖南省第九届大学生计算机程序设计竞赛)
- 湖南省第九届大学生计算机程序设计竞赛 字符识别?
- 湖南省第九届大学生计算机程序设计竞赛1334: 好老师 (模拟)
- 湖南省第九届大学生计算机程序设计竞赛 字符识别?