Color the ball HDU1556
这题整整debug了两个小时
不同组居然要初始化 本以为built函数里面已经初始化好了!!!!!
其他无需注意
#include<cstdio>
#include<cstring>
using namespace std;
int n,p,a,b,m,x,y,ans;
struct node
{
int l,r,w,f;
}tree[];
inline void build(int k,int ll,int rr)//建树
{
tree[k].l=ll,tree[k].r=rr;
if(tree[k].l==tree[k].r)
{
tree[k].w=;
return;
}
int m=(ll+rr)/;
build(k*,ll,m);
build(k*+,m+,rr);
tree[k].w=tree[k*].w+tree[k*+].w;
}
inline void down(int k)//标记下传
{
tree[k*].f+=tree[k].f;
tree[k*+].f+=tree[k].f;
tree[k*].w+=tree[k].f*(tree[k*].r-tree[k*].l+);
tree[k*+].w+=tree[k].f*(tree[k*+].r-tree[k*+].l+);
tree[k].f=;
}
inline void ask_point(int k)//单点查询
{
if(tree[k].l==tree[k].r)
{
ans=tree[k].w;
return ;
}
if(tree[k].f) down(k);
int m=(tree[k].l+tree[k].r)/;
if(x<=m) ask_point(k*);
else ask_point(k*+);
} inline void change_interval(int k)//区间修改
{
if(tree[k].l>=a&&tree[k].r<=b)
{
tree[k].w+=(tree[k].r-tree[k].l+)*y;
tree[k].f+=y;
return;
}
if(tree[k].f) down(k);
int m=(tree[k].l+tree[k].r)/;
if(a<=m) change_interval(k*);
if(b>m) change_interval(k*+);
tree[k].w=tree[k*].w+tree[k*+].w;
}
int main()
{
while(scanf("%d",&n)==&&n)
{
memset(tree,,sizeof(tree));
build(,,n);
y=;
for(int i=;i<=n;i++)
{
scanf("%d%d",&a,&b);
change_interval();
}
for(x=;x<n;x++)
{
ans=;
ask_point();
printf("%d ",ans);
}
ans=;
ask_point();
printf("%d\n",ans);
}
return ;
}
可以用树状数组来做
普通的树状数组是改单点 求区间 或者改单点 求单点(做差)
但是树状数组也可以改区间 求单点 sum即为单点
每次更新为 参数~n 均+=val
#include <cstdio>
#include <cstring>
const int maxn = ;
int c[maxn],n;
int Lowbit(int x)
{
return x&(-x);
}
void update(int k,int x)
{
while(k <= n)
{
c[k] += x;
k += Lowbit(k);
}
}
int getsum(int x)
{
int sum = ;
while(x > )
{
sum += c[x];
x -= Lowbit(x);
}
return sum;
}
int main()
{
int a,b;
while(scanf("%d",&n)&&n!=)
{
memset(c,,sizeof(c));
for(int i=; i<n; i++)
{
scanf("%d%d",&a,&b);
update(a,);
update(b+,-);
}
for(int i=; i<n; i++)
printf("%d ",getsum(i));
printf("%d\n",getsum(n));
}
return ;
}
Color the ball HDU1556的更多相关文章
- (不用循环也可以记录数组里的数)Color the ball --hdu--1556
题目: N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次 ...
- Color the ball(HDU1556)树状数组
每次对区间内气球进行一次染色,求n次操作后后所有气球染色次数. 树状数组,上下区间更新都可以,差别不大. 1.对于[x,y]区间,对第x-1位减1,第y位加1,之后向上统计 #include<b ...
- HDU1556 Color the ball & 牛客 contest 135-I 区间 [差分标记]
一.差分标记介绍 差分标记用来解决针对区间(修改-查询)的问题,复杂度比线段树要更低.推荐这个博客. 例如,给数组中处于某个区间的数进行加减操作,然后查询某个位置上数的变化值. 二.HDU1556 C ...
- Color the ball(hdu1556)(hash)或(线段树,区间更新)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU1556 Color the ball(差分数组)题解
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU-1556:Color the ball(前缀和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- MyBatis参数传递
一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...
- VS2015 与 Git 的简单使用
前言 在白忙之中抽了点时间,记录了下 VS 与 Git 的简单使用. 在之前使用命令行的时候,提交或拉取代码时,总报错:(提取时遇到错误: Unsupported URL protocol),后来在网 ...
- linux4.10.8 内核移植(三)---裁剪内核
一.裁剪内核 1.1 第一次修改 现在的内核大小为2.8M左右,要裁剪到2.0M以下,毕竟给内核分区就只有2.0M. 这两个设备我们没有,裁剪掉. 进入make menuconfig中,搜索mouse ...
- Jetson tk1 hash sum mismatch
sudo apt-get update遭遇Hash Sum Mismatch 修改DNS服务器地址: sudo gedit /etc/resolv.conf 解决办法: 在装有goagent的情况下: ...
- 【网络编程4】网络编程基础-ARP响应(ARP欺骗之中间人攻击)
arp欺骗->arp响应 ARP 缓存中毒(ARP欺骗) arp传送原理在于主机发送信息时将包含目标IP地址的ARP请求广播到网络上的所有主机,并接收返回消息,以此确定目标的物理地址:收到返回消 ...
- 嵌入式linux系统中,lsusb出现unable to initialize libusb: -99 解决办法 【转】
转自:http://cpbest.blog.163.com/blog/static/41241519201111575726966/ libusb是linux系统中,提供给用户空间访问usb设备的AP ...
- windows环境用python修改环境变量的注意点(含代码)
1.部分环境变量字段需要保留原来的值,只是做添加,不可以替换 2.Path和PATH对于python来说是一样的,也就是说存在名为Path的环境变量时,添加PATH的环境变量,会覆盖原有的Path环境 ...
- centos中创建自动备份Mysql脚本任务并定期删除过期备份
背景: OA系统数据库是mysql,引擎为myisam,可以直接通过拷贝数据库文件的方式进行备份 创建只备份数据库的任务: 创建保存mysql数据库备份文件的目录mysqlbak mkdir /hom ...
- spring和hibernate集成事物管理配置方法
spring+hibernate,采用声明式事务 1.声明式事务配置 * 配置SessionFactory * 配置事务管理器 * 事务的传播特性 * 那些类那些方法使用事务 2.编写业务逻辑方法 * ...
- nginx1.8.1反向代理、负载均衡功能的实现
nginx1.8.1 proxy 服务器192.168.8.40 web1 centos6.5 httpd2.2.15 web2 centos7.2 httpd2.4.6 1.代理功能的简单实现 ng ...