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. HyperLedger Fabric 1.4 交易流程(6.3)

    区块链最主要的特性之一是去中心化,没有了中心机构的集中处理,为了达成数据的一致性,就需要网络中全民参与管理,并以某种方法达成共识,所以区块链的交易流程也就是共识的过程.       在Fabric中, ...

  2. Linux 之vi与vim

    vi 三种模式: 『一般模式』: 光标 『编辑模式』:i,o,a,r 『指令列命令模式』「:/ ?」 例子: 1. 请在/tmp 这个目录下建立一个名为vitest 的目录: 2. 将/etc/man ...

  3. elasticsearch 拼音+ik分词,spring data elasticsearch 拼音分词

    elasticsearch 自定义分词器 安装拼音分词器.ik分词器 拼音分词器: https://github.com/medcl/elasticsearch-analysis-pinyin/rel ...

  4. 【PHP】进一法取整、四舍五入取整、忽略小数等的取整数方法大全

    PHP取整数函数常用的四种方法,下面收集了四个函数:经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已--主要是:ceil,floor,round,intval PHP取整数函数常用 ...

  5. 【WPF】 前言

    [WPF] 前言 前段时间项目中用到了WPF,就边学边做项目,一个项目做下来有点感触,以此记录. 以前也开发过多个C/S项目, 一直都是用的Winform,Winform 做些简单的界面很方便,基本只 ...

  6. 玩转VIM-札记(三)

    玩转VIM-札记(三) 眨眼之间,5月就要从指间溜走,不给人一点点遐想的时间,我要赶紧抓着五月的尾巴,在博客中在添一笔.那么就还接着Vim来说吧.以Vim来为五月画上一个句号. 返璞归真 相信经过玩转 ...

  7. 【APUE】Chapter13 Daemon Processes

    这章节内容比较紧凑,主要有5部分: 1. 守护进程的特点 2. 守护进程的构造步骤及原理. 3. 守护进程示例:系统日志守护进程服务syslogd的相关函数. 4. Singe-Instance 守护 ...

  8. 官方文档 恢复备份指南八 RMAN Backup Concepts

    本章内容 Consistent and Inconsistent RMAN Backups Online Backups and Backup Mode Backup Sets Image Copie ...

  9. 并查集——hdu1232(入门)

    传送门:畅通工程 实质是求连通分支的数量 #include <iostream> #include <cstdio> #include <algorithm> us ...

  10. eclipse mylyn.tasks.ui

    sudo rm workspace/.metadata/.lock ./Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse -clean - ...