UVA12663 线段树+二分
来源:互联网 发布:unity3d卡牌游戏教程 编辑:程序博客网 时间:2024/06/05 05:31
分析:线段树模板+二分查找
读入每座桥的高度,然后排序。
m次洪水,每次二分查找在高度在区间【Bi-1,A】的桥的个数,然后区间更新。
代码如下:
#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100000;int n,m,k;int a,b;int h[maxn+10];struct node{ int lc,rc; int add; int sum;}tree[maxn*4+10];int ans;void pushdown(int root, int x){ if (tree[root].add){ tree[root<<1].sum += tree[root].add*(x-(x>>1)); tree[root<<1|1].sum += tree[root].add*(x>>1); tree[root<<1].add += tree[root].add; tree[root<<1|1].add += tree[root].add; tree[root].add = 0; }}void build(int root,int L, int R){ tree[root].lc = L; tree[root].rc = R; tree[root].add = 0; tree[root].sum = 0; if (L==R) return; int mid = (L+R)>>1; build(root<<1,L,mid); build(root<<1|1,mid+1,R);}void update(int root , int L, int R, int x){ if (tree[root].lc == L && tree[root].rc == R){ tree[root].sum += x*(R-L+1); tree[root].add += x; return; } pushdown(root,tree[root].rc-tree[root].lc+1); int mid = (tree[root].lc+tree[root].rc)>>1; if (R<=mid) update(root<<1,L,R,x); else if (L>mid) update(root<<1|1,L,R,x); else { update(root<<1,L,mid,x); update(root<<1|1,mid+1,R,x); }}void query(int root, int L, int R){ if (L==tree[root].lc && R==tree[root].rc) { ans += tree[root].sum; return; } pushdown(root,tree[root].rc-tree[root].lc+1); int mid = (tree[root].lc+tree[root].rc)>>1; if (R<=mid) query(root<<1,L,R); else if (L>mid) query(root<<1|1,L,R); else { query(root<<1,L,mid); query(root<<1|1,mid+1,R); }}void init(){ for (int i=1; i<=n; i++) scanf("%d",&h[i]); sort(h+1,h+n+1); build(1,1,n);}int kase = 0;int main(){ int s1,s2; while (scanf("%d %d %d",&n,&m,&k)!=EOF){ init(); int high = 1; while (m--){ scanf("%d %d",&a,&b); s1 = lower_bound(h+1,h+n+1,high)-h; s2 = lower_bound(h+1,h+n+1,a)-h; if (s2>n) s2 = n; while (h[s1]<=high) s1++; while (a<h[s2]) s2--; if (s1<=s2) update(1,s1,s2,1); high = b; } int total = 0; for (int i=1; i<=n; i++) { ans = 0; query(1,i,i); if (ans>=k) total++; } printf("Case %d: %d\n",++kase,total); } return 0;}
阅读全文
0 0
- UVA12663 线段树+二分
- hdu3564(二分+线段树)
- hdu4614 二分+线段树
- hdu5217Brackets【线段树,二分】
- 【HDU4614】【线段树】【二分】
- HDU4791(线段树+二分)
- HDU5592(线段树+二分)
- Codeforces460C【二分+线段树】
- hdu6070 二分+线段树
- HDU6070 二分 线段树
- hdu6070 线段树+二分
- poj2182 线段树/线段数组+二分搜索
- hdu 4339 线段树+二分
- hdu 4614 线段树+二分~
- HDU 4614 线段树+二分
- HDU 4791二分+线段树
- HDU - 4973(线段树+二分)
- hdu 4339 线段树+二分
- WebSocket刨根问底(转)
- 解析json数组
- Gson
- Android TabLayout使用以及自定义TabLayout
- instanceof与class的等价性
- UVA12663 线段树+二分
- bzoj1260 [CQOI2007]涂色paint 区间DP
- Swift 算法练习
- JS中的同步和异步过程。
- tcp/ip<3>
- OpenStack源码分析 Neutron源码分析(一)-----------Restful API篇
- android 系统自带 回声消除
- 阿里巴巴Java开发手册学习笔记-编程规约
- HAAR特征+adaboost的实现原理