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

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
 
Author
8600
 
Source
题意: 区间更新 单点查询
题解:
1.线段树 处理:
 #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 线段树/树状数组/区间更新姿势 三种方法处理的更多相关文章

  1. php数组合并有哪三种方法

    php数组合并有哪三种方法 一.总结 一句话总结:array_merge():array_merge_recursive():‘+'号 $a = array('color'=>'red',5,6 ...

  2. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. 【poj2155】Matrix(二维树状数组区间更新+单点查询)

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  4. 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?

    开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...

  5. HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Sub ...

  6. HDU 1556 Color the ball (树状数组区间更新)

    水题,练习一下树状数组实现区间更新. 对于每个区间,区间左端点+1,右端点的后一位-1,查询每个位置的覆盖次数 #include <cstdio> #include <cstring ...

  7. NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)

    Problem 1050: Just Go Time Limits:  3000 MS   Memory Limits:  65536 KB 64-bit interger IO format:  % ...

  8. hdu3966 树链剖分点权模板+线段树区间更新/树状数组区间更新单点查询

    点权树的模板题,另外发现树状数组也是可以区间更新的.. 注意在对链进行操作时方向不要搞错 线段树版本 #include<bits/stdc++.h> using namespace std ...

  9. HDU 4267 A Simple Problem with Integers(树状数组区间更新)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

随机推荐

  1. windows和Ubuntu下安装mongodb

    windows 下载 mongodb官网下载压缩版安装包:下载地址:https://www.mongodb.com/download-center/community 注意选择版本(目前windows ...

  2. python基础,导入模块,if语句,while语句

    python基础 python代码 变为字节码 变为机器码 最后执行执行‘文件名.py’文件时出现的‘文件名.pyc’文件为字节码 缓存机制 使用pycharm的时候在文件最开始添加下面这两行代码,中 ...

  3. dot安装和使用

    1.安装 apt-get install graphviz 如果报错说缺少依赖文件,则使用apt自动安装依赖项 apt-get -f install 我在安装中报错: dpkg: unrecovera ...

  4. 蓝牙技术(BlueTooth)——(一)

    一,概述 蓝牙是一种短距离的无线通信技术标准. 蓝牙协议分为4层,即核心协议层,电缆替代协议层,电话控制协议层,和      采纳的其它协议层. 这4中协议中最重要的是核心协议.蓝牙的核心协议包括基带 ...

  5. MyEclipse - 问题集 - 创建Maven项目,JDK版本默认是1.5

    修改Maven的配置文件settings.xml,增加profile节点,如下所示: <profile> <id>jdk-1.8</id> <activati ...

  6. 一个关于sql更新的小笔记

    一直在sqlserver下写东西,突然用mysql有些语法发生了改变,有点折腾 (MS SQL Server)语句:update A set  a.Name =   b.Name   from   A ...

  7. Android Studio Gradle编译时『No resource found that matches the given name』解决方法(windows系统的坑)

    * 最近帮团队同事配置gradle时,发现一个非常奇怪的问题:> * 同样的gradle配置的项目,只是修改了一个项目的名称,竟然会出现以下奇怪问题: ## 现象1. 一个编译完全OK,另外一个 ...

  8. IDEA 中.properties文件中文自动转Unicode编码及乱码问题

    问题描述: 在使用IDEA开发工具编辑属性文件(.properties)的时候出现中文自动转成了Unicode编码,或在读取属性文件的时候中文出现乱码. 问题解决: 进入 File -> Set ...

  9. 剑指offer-反转链表15

    题目描述 输入一个链表,反转链表后,输出新链表的表头. class Solution: # 返回ListNode def ReverseList(self, pHead): # write code ...

  10. Visual Studio 2010安装包

    点击下载