UVA 12663 High bridge, low bridge
2016-03-25 20:44
239 查看
题意:刚开始给你n个桥和他们分别对应的高度,接下来有m次涨潮,每次涨潮水的高度会上升到xi,接下来又会下降到yi,
每次涨潮时水重新淹没这座桥算作一次淹没,如果桥一直比水的位置低,则只算做一次淹没。
(1<=n,m,k<=1e5)
思路:先对桥的高度从小到大进行排序,
每次我们在线段树上对上一次被水退潮的高度+1到这次水淹没的高度的区间+1,最后统计有多少座桥淹没的次数大于等于k便可以了。
每次涨潮时水重新淹没这座桥算作一次淹没,如果桥一直比水的位置低,则只算做一次淹没。
(1<=n,m,k<=1e5)
思路:先对桥的高度从小到大进行排序,
每次我们在线段树上对上一次被水退潮的高度+1到这次水淹没的高度的区间+1,最后统计有多少座桥淹没的次数大于等于k便可以了。
#include <map> #include <set> #include <stack> #include <queue> #include <cmath> #include <ctime> #include <vector> #include <cstdio> #include <cctype> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #include<bitset> using namespace std; #define INF 0x3f3f3f3f #define inf -0x3f3f3f3f #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define mem0(a) memset(a,0,sizeof(a)) #define mem1(a) memset(a,-1,sizeof(a)) #define mem(a, b) memset(a, b, sizeof(a)) const int maxn=100101; int a[maxn],sumv[4*maxn],addv[4*maxn],L,R,v; int k; void build(int l,int r,int rt){ addv[rt]=0; sumv[rt]=0; if(l==r) return ; int m=(l+r)>>1; build(lson); build(rson); } void pushdown(int rt,int l){ if(addv[rt]){ addv[rt<<1]+=addv[rt]; addv[rt<<1|1]+=addv[rt]; sumv[rt<<1]+=addv[rt]; sumv[rt<<1|1]+=addv[rt]; addv[rt]=0; } } void update(int l,int r,int rt){ if(L<=l&&R>=r){ addv[rt]+=v; sumv[rt]+=v; return ; } pushdown(rt,r-l+1); int m=(l+r)>>1; if(L<=m) update(lson); if(R>m) update(rson); } int query(int l,int r,int rt){ int ret=0; if(l==r) return sumv[rt]>=k; pushdown(rt,r-l+1); int m=(l+r)>>1; ret+=query(lson); ret+=query(rson); return ret; } int c[maxn],d[maxn]; int main(){ int n,m,case1=1; while(scanf("%d%d%d",&n,&m,&k)!=EOF){ for(int i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+n+1); build(1,n,1); d[0]=0; for(int i=1;i<=m;i++){ scanf("%d%d",&c[i],&d[i]); L=lower_bound(a+1,a+n+1,d[i-1]+1)-a; R=upper_bound(a+1,a+n+1,c[i])-a; if(a[R]>c[i]) R--; v=1; update(1,n,1); } printf("Case %d: %d\n",case1++,query(1,n,1)); } return 0; }
相关文章推荐
- PHP如何判断一个元素是否在已知数组中
- 闭包的循环引用问题
- 设置错误日志
- linux 源码安装mysql 5.5
- R语言中的常用函数
- 关于oracle误删数据的恢复
- WebService学习之旅(三)JAX-WS与Spring整合发布WebService
- WebService学习之旅(三)JAX-WS与Spring整合发布WebService
- 如何为Swift进行宏定义
- WebService学习之旅(三)JAX-WS与Spring整合发布WebService
- Swift --- 面向对象中类和对象的属性
- html之大白
- Android学习之ItemTouchHelper实现RecylerView的拖拽以及滑动删除功能
- java中的stack
- jquery插件——检测DOM元素是否在浏览器可视范围之内
- 基础算法-插入排序
- Linux crontab定时执行任务
- [LeetCode]Binary Tree Paths
- 如何根据树的两个遍历顺序确定另外一个遍历顺序
- 递归方法生成N位的格雷码