Color the ball(线段树)
来源:互联网 发布:删除数据库sql 编辑:程序博客网 时间:2024/06/09 19:53
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24116 Accepted Submission(s): 11731
Problem Description
N个气球排成一排,从左到右依次编号为1,2,3….N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽”牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
Input
每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
当N = 0,输入结束。
Output
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
Sample Input
3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
Sample Output
1 1 1
3 2 1
//线段树裸题 一开始还担心超市 结果一提交 就AC 还是比较开心的
主要就是应用了lazy标记 真的是万能的lazy标记…..
lazy标记 顾名思义就是懒惰 就是我们用到这个区间时我们在根据lazy标记进行判断 如果lazy标记不为0 那说明之前对这个区间的内容进行修改了 而我们此时需要用这一个区间的值怎么办呢 呢我们就只需把lazy标记向下传递一层 (这里只传递一层 毕竟是懒惰标记嘛) 然后此时这个节点的子节点的值就被更新了 这样就减少了许多不必要的操作
#include<iostream>#include<stdio.h>#include<algorithm>using namespace std;#define maxn 100000struct Node{ int l,r; int sum ;}node[maxn*4];int add[maxn*4];long long cnt[maxn];void build(int l,int r,int rt){ node[rt].l = l; node[rt].r = r; add[rt] = 0; node[rt].sum =0; if(l == r) { //cnt[l] =0; return; } int mid = (l + r) /2; build(l,mid,rt*2); build(mid +1 ,r,rt*2+ 1); node[rt].sum = node[rt*2].sum + node[rt*2+1].sum;}void pushdown(int rt,int len){ if(add[rt]) { add[rt*2] += add[rt]; add[rt*2+1] += add[rt]; node[rt*2].sum += add[rt] * (len - len/2); node[rt*2+1].sum += add[rt] * (len/2); add[rt] = 0; }}void change(int l,int r,int rt){ if(l == node[rt].l && node[rt].r == r) { node[rt].sum += (r - l +1) ; add[rt] += 1; return; } pushdown(rt,node[rt].r - node[rt].l + 1); int mid = (node[rt].l + node[rt].r)/2; if(r <= mid) { change(l,r,rt*2); } else if(l > mid) { change(l,r,rt*2+1); } else { change(l,mid,rt*2); change(mid+1,r,rt*2+1); } node[rt].sum = node[rt*2].sum + node[rt*2+1].sum;}int query(int l,int rt){ if(node[rt].r == node[rt].l) { return node[rt].sum; } pushdown(rt,node[rt].r - node[rt].l +1); int mid = ( node[rt].l + node[rt].r )/2; if(l<=mid) return query(l,rt*2); else return query(l,rt*2+1);}int main(){ int n; while(~scanf("%d",&n)) { if(n==0) break; build(1,n,1); int m = n; while(m--) { int l,r; scanf("%d%d",&l,&r); change(l,r,1); } printf("%d",query(1,1)); for(int i =2;i<=n;i++) { printf(" %d",query(i,1)); } printf("\n"); }}
本题的另外一种做法就是采用树状数组来做 不过确实效率提高了许多 ……可是树状数组容易存在一个局限性 不支持区间更新 但这道题 我们可以看成权值线段树
一个思路就是 我们更新区间[a,b] 只需要让a点的位置 +1 让b+1 位置 减一 让a点位置 +1 很好理解 但让b+1的位置减一 这是为什嘛的呢
我们来看一下 加入更新区间[1,2]的值
呢嘛执行完a的位置加+1后c数组的值变化为(c表示区间和)
c[1] = c[2] = c[4] = c[8] .. = 1;
c[1] = c[2] = 1这是很正确的 但是c[4] = c[8] = 1这里就有点说不过去了 我们没有更新4 和8 的值 凭什么让他们的涂了一遍 所以我们需要平衡掉这个1 只需要在b+1 处 -1 呢么即 在3的位置减一 呢么c[4] = c[8] = 0 此时才是一个正确的结果
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#define maxn 100007using namespace std;int c[maxn],n;int low_bit(int k){ return k & (-k);}void update(int i,int val){ while(i<= n) { c[i] += val ; i += low_bit(i); }}int getSum(int indx){ int sum = 0; while(indx) { sum += c[indx]; indx -= low_bit(indx); } return sum;}int main(){ while(~scanf("%d",&n)) { if(n==0) break; memset(c,0,sizeof(c)); for(int i =1;i<=n;i++) { int a,b; scanf("%d%d",&a,&b); update(a,1); update(b+1,-1); } printf("%d",getSum(1)); for(int i =2;i<=n;i++) { printf(" %d",getSum(i)); } printf("\n"); } return 0;}
- Color the ball (线段树)
- Color the ball(线段树)
- Color the ball(线段树)
- hdu 1566 Color the ball---线段树
- HDU 1556 Color the ball 线段树
- HDU1556 Color the ball(线段树)
- HDU 1199 Color the Ball (线段树)
- HDU 1556 Color the ball (线段树)
- HDU 1556 Color the ball 线段树
- hdu 1556 Color the ball(线段树)
- hdu 1556 Color the ball 线段树
- hdoj 1556 Color the ball 【线段树】
- HDU 1556 Color the ball 线段树
- HDU 1556 Color the ball [线段树]
- HDU 1556 Color the ball (线段树)
- hdu - 1556 (Color the ball 线段树)
- hdu1556-color the ball 线段树
- hdu1556 Color the ball 线段树
- 使用canvas.drawRoundRect()时,解决四个圆角的线比较粗的问题
- [Leetcode] 520. Detect Capital 解题报告
- __sync_fetch_and_add的含义
- WordPress版微信小程序安装使用说明
- 使用vue开发的你是不是 真正需要Vuex
- Color the ball(线段树)
- 实现洗牌算法
- 学习2
- Android Assets工具类
- SVN系统实现多系统环境下的代码审核与版本同步
- 多线程断点下载
- SecureCrt 配置脚本查看日志
- style.left和offsetLeft和offsetWidth和scrollTop区别
- Austin 第二天 | 炫技!Google 语音操控 GKE 部署集群及扩容服务