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 ...
随机推荐
- 关于nodejs DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
const mongoose = require('mongoose') mongoose.connect("mongodb://localhost:27017/study", { ...
- Sqoop的安装配置及使用
一.Sqoop基础:连接关系型数据库与Hadoop的桥梁 1.1 Sqoop的基本概念 Hadoop正成为企业用于大数据分析的最热门选择,但想将你的数据移植过去并不容易.Apache Sqoop正在加 ...
- struts2官方 中文教程 系列六:表单验证
先贴个本帖的地址,以免被爬:struts2教程 官方系列六:表单验证 即 http://www.cnblogs.com/linghaoxinpian/p/6906720.html 下载本章节代码 介 ...
- 两个完整的jquery slide多方面滑动效果实例
实例1,需要引用jquery-ui.js <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...
- Entity Framework + WCF 远程调用出错
在使用Entity Framework中使用WCF,在程序中调用服务一直报错,我一直以为是WCF的哪个地方的配置有问题,找来找去,一直没有解决. 最后在网上找到一篇文章 ...
- 30分钟 带你浅入requirejs源码
因为最近项目想现实一个单页功能,用的是react ,然后看了一下react route,挖槽 gzip后16k? 然后我简单写了一个纯单页(不支持多页的单页,所有入口都经过rewrite跑到index ...
- java网络编程框架
虽然写过一些网络编程方面的东西,但还没有深入研究过这方面的内容,直接摘录一些文章,后续整理 原文地址:http://blog.csdn.net/lwuit/article/details/730613 ...
- jmeter添加自定义扩展函数之图片base64编码
打开eclipse,新建maven工程,在pom中引入jmeter核心jar包: <!-- https://mvnrepository.com/artifact/org.apache.jmete ...
- 《python核心编程第二版》第7章习题
7–1. 字典方法.哪个字典方法可以用来把两个字典合并到一起? 答:dict1.update(dict2) 7–2. 字典的键.我们知道字典的值可以是任意的Python 对象,那字典的键又如何呢?请试 ...
- Mybatis 异常记录(1): Invalid bound statement (not found)
错误信息: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.pingan.cr ...