HDU 1556 Color the ball(线段树)
来源:互联网 发布:知乎帐号异常 编辑:程序博客网 时间:2024/06/06 12:43
思路:
法一:直接标记两个端点最后一直用dp[i]+=dp[i+1],向后推即可。
法二:线段树区间更新。
#include<map>#include<queue>#include<cmath>#include<iostream>#include<cstdio>#include<stack>#include<cstring>#include<algorithm>#define LL __int64#define inf 0x3f3f3f3f#define ls l,mid,rt<<1#define rs mid+1,r,rt<<1|1const double PI=acos(-1.0);using namespace std;#define mod 1000000007int sum[200010*5],lazy[5*200010];void Pushdown(int rt,int len){ if(lazy[rt]){ sum[rt<<1]+=(len-(len>>1))*lazy[rt]; sum[rt<<1|1]+=lazy[rt]*(len>>1); lazy[rt<<1|1]+=lazy[rt]; lazy[rt<<1]+=lazy[rt]; lazy[rt]=0; }}void Pushup(int rt){ sum[rt]=sum[rt<<1|1]+sum[rt<<1];}void Updata(int a,int b,int l,int r,int rt){ if(a<=l&&b>=r){ sum[rt]+=(r-l+1); lazy[rt]+=1; return ; } Pushdown(rt,r-l+1); int mid=(l+r)>>1; if(a<=mid) Updata(a,b,ls); if(b>mid) Updata(a,b,rs); Pushup(rt);}int so(int x,int l,int r,int rt){ if(l==r){ return sum[rt]; } Pushdown(rt,r-l+1); int mid=(l+r)>>1; int ans=0; if(x<=mid){ return ans+=so(x,ls); } else return ans+=so(x,rs);}int main(){ int n,m,i,j,k,a,b; while(~scanf("%d",&n)&&n){ memset(sum,0,sizeof(sum)); memset(lazy,0,sizeof(lazy)); //ini(1,n,1); for(i=0;i<n;i++){ scanf("%d%d",&a,&b); Updata(a,b,1,n,1); } for(i=1;i<=n;++i){ printf(i==n?"%d\n":"%d ",so(i,1,n,1)); } } return 0;}
0 0
- HDU 1556 Color the ball 线段树
- HDU 1556 Color the ball (线段树)
- HDU 1556 Color the ball 线段树
- hdu 1556 Color the ball(线段树)
- hdu 1556 Color the ball 线段树
- HDU 1556 Color the ball 线段树
- HDU 1556 Color the ball [线段树]
- HDU 1556 Color the ball (线段树)
- hdu - 1556 (Color the ball 线段树)
- hdu 1556 Color the ball(线段树+懒惰标记)
- hdu 1556 Color the ball(线段树)
- hdu 题目1556 Color the ball(线段树)
- HDU 1556 Color the ball(线段树)
- HDU 1556 Color the ball(线段树)
- HDU 1556 Color the ball(线段树)
- hdu 1556 Color the ball(线段树)
- HDU 1556 Color the ball(线段树区间更新)
- HDU 1556 Color the ball(线段树)
- String、StringBuffer与StringBuilder之间区别
- 字符串,字符数组,字符指针的区别与联系
- SQL Server 常用 几个设置(set)选项 [经典实用]
- 蓝桥杯历届-奇怪的比赛
- 数据结构----单链表分析及实现
- HDU 1556 Color the ball(线段树)
- 值得推荐的C/C++框架和库
- [GDOI模拟2016.03.05]魔道研究
- POJ1321 棋盘问题
- python交叉编译过程记录
- VS编译错误:syntax error : missing ';' before 'type'
- 2016年目标
- Ubuntu14.04下,GO语言的安装!
- VMware 虚拟机硬盘镜像共享