HDU 1556 线段树/树状数组/区间更新姿势 三种方法处理
Color the ball
Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15145    Accepted Submission(s): 7540
当N = 0,输入结束。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<queue>
#include<stack>
using namespace std;
struct node
{
int l,r,value;
}tree[];
int ans[];
int n;
int aa,bb;
int jishu=;
void buildtree(int root,int left,int right)
{
tree[root].l=left;
tree[root].r=right;
tree[root].value=;
if(left==right)
return ;
int mid=(left+right)>>;
buildtree(root<<,left,mid);
buildtree(root<<|,mid+,right);
}
void updata(int c,int left,int right,int root)
{
if(tree[root].l==left&&tree[root].r==right)
{
tree[root].value+=c;
return ;
}
int mid=(tree[root].l+tree[root].r)>>;
if(right<=mid)
updata(c,left,right,root<<);
else
{
if(left>mid)
updata(c,left,right,root<<|);
else
{
updata(c,left,mid,root<<);
updata(c,mid+,right,root<<|);
}
}
}
void sum(int root)
{
if(tree[root].l==tree[root].r)
{
ans[jishu]=tree[root].value;
jishu++;
return ;
}
tree[root<<].value+=tree[root].value;
tree[root<<|].value+=tree[root].value;
sum(root<<);
sum(root<<|);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==)
break;
// memset(ans,0,sizeof(ans));
buildtree(,,n);
for(int i=;i<=n;i++)
{
scanf("%d %d",&aa,&bb);
updata(,aa,bb,);
}
jishu=;
sum();
printf("%d",ans[]);
for(int i=;i<=n;i++)
printf(" %d",ans[i]);
printf("\n");
}
return ;
2.树状数组 处理
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int tree[];
int lowbit (int x)
{
return x&(-x);
}
void hsd (int a,int b,int c)
{
while(a<=c)
{
tree[a]+=b;
a+=lowbit(a);
}
}
int get(int x)
{ int sum=;
while(x>)
{
sum+=tree[x];
x-=lowbit(x);
}
return sum; }
int main()
{
int n,i,a,b,x;
while(scanf("%d",&n)!=EOF)
{ if(n==)
break;
memset(tree,,sizeof(tree));
for(i=; i<=n; i++)
{
scanf("%d%d",&a,&b);
hsd(a,,n);
hsd(b+,-,n);
}
printf("%d",tree[]);
for(i=;i<=n;i++)
{
x=get(i);
printf(" %d",x);
}
cout<<endl; }
return ;
}
3. 区间更新姿势 处理
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<queue>
#include<stack>
using namespace std;
int a[];
int b[];
int aa,bb;
int n;
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==)
break;
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(int i=;i<=n;i++)
{
scanf("%d %d",&aa,&bb);
b[aa]++;
b[bb+]--;
}
int q=;
for(int i=;i<=n;i++)
{
q+=b[i];
a[i]=q;
}
printf("%d",a[]);
for(int i=;i<=n;i++)
printf(" %d",a[i]);
printf("\n");
}
return ;
}
HDU 1556 线段树/树状数组/区间更新姿势 三种方法处理的更多相关文章
- php数组合并有哪三种方法
		
php数组合并有哪三种方法 一.总结 一句话总结:array_merge():array_merge_recursive():‘+'号 $a = array('color'=>'red',5,6 ...
 - hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
		
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
 - 【poj2155】Matrix(二维树状数组区间更新+单点查询)
		
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
 - 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?
		
开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...
 - HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)
		
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Sub ...
 - HDU 1556 Color the ball (树状数组区间更新)
		
水题,练习一下树状数组实现区间更新. 对于每个区间,区间左端点+1,右端点的后一位-1,查询每个位置的覆盖次数 #include <cstdio> #include <cstring ...
 - NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)
		
Problem 1050: Just Go Time Limits: 3000 MS Memory Limits: 65536 KB 64-bit interger IO format: % ...
 - hdu3966 树链剖分点权模板+线段树区间更新/树状数组区间更新单点查询
		
点权树的模板题,另外发现树状数组也是可以区间更新的.. 注意在对链进行操作时方向不要搞错 线段树版本 #include<bits/stdc++.h> using namespace std ...
 - HDU 4267  A Simple Problem with Integers(树状数组区间更新)
		
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
 
随机推荐
- python3爬虫之开篇
			
写在前面的话: 折腾爬虫也有一段时间了,从一开始的懵懵懂懂,到现在的有一定基础,对于这一路的跌跌撞撞,个人觉得应该留下一些文字性的东西,毕竟好记性不如烂笔头,而且毕竟这是吃饭的家伙,必须用心对待才可以 ...
 - ABAP CDS ON HANA-(7)CDSビューでの集約
			
Aggregate expression in CDS View An aggregate expression calculates a single value from an operand o ...
 - python2.7练习小例子(七)
			
7):题目:将一个列表的数据复制到另一个列表中. 程序分析:使用列表[:]. 程序源代码: #!/usr/bin/python # -*- coding: UTF-8 -*- ...
 - 20145202马超 2006-2007-2 《Java程序设计》第2周学习总结
			
20145202马超 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 第三章主要讲了各种变量的设置以及流程控制,基本上都和c是一样的.print是不太一样的, ...
 - 20145202马超《网络对抗》Exp3免杀 进阶
			
木马化正常软件,如通过改变机器指令.实现可免杀免防火墙提示的后门. 继上次实验3所做的代码在主函数里面加上一行调用就可以 改各种属性,这里我参考了郝浩同学的博客 最后我还是遇到了问题 后来发现虽然有那 ...
 - Moodle的安装和登陆(使用Https)
			
之前使用默认的配置和安装,到中间检测组件是,总是提示 site no https.所以重新安装,用:https://localhost来登陆,结果不再提示,所以建议大家在使用时,还是用https来登 ...
 - 云计算之路-阿里云上:“黑色1秒”问题与2009年Xen一个补丁的故事
			
在之前对“黑色1秒”问题的分析博文中,我们将最大嫌疑对象锁定在了Xen,在这篇博文我们将从Xen的角度进行分析.也许有人会问,为什么不知道天多高地多厚地去研究不属于自己范围的问题?只因我们对一个问题的 ...
 - 让webapi支持CORS,可以跨域访问
			
1.在NuGet里搜索webapi找到下面的扩展,添加进项目里. 2.在Global.asax中添加一行代码 protected void Application_Start() { //添加CORS ...
 - Sublime text3最全快捷键清单
			
[转]https://blog.csdn.net/mrchengzp/article/details/78508509,感谢作者的分享,收录方便查阅 Sublime Text 支持多种编程语言的语 ...
 - CCF-NOIP-2018 提高组(复赛) 模拟试题(四)
			
T1 贪吃蛇 [问题描述] 贪吃蛇是一个好玩的游戏.在本题中,你需要对这个游戏进行模拟. 这个游戏在一个 \(n\) 行 \(m\) 列的二维棋盘上进行. 我们用 \((x, y)\) 来表示第 \( ...