每次对区间内气球进行一次染色,求n次操作后后所有气球染色次数。

树状数组,上下区间更新都可以,差别不大。

1.对于[x,y]区间,对第x-1位减1,第y位加1,之后向上统计

#include<bits/stdc++.h>
using namespace std;
const int maxn=+;
int ans[maxn],Tree[maxn],n;
inline int lowbit(int x) //计算2^k
{
return (x&-x);
}
void add(int x,int value)
{
for(int i=x; i<maxn; i+=lowbit(i))
Tree[i]+=value;
}
void up(int x,int val)
{
while(x>)
{
Tree[x]+=val;
x-=lowbit(x);
}
}
int get(int x)
{
int sum=;
while(x<=n)
{
sum+=Tree[x];
x+=lowbit(x);
}
return sum;
}
int main()
{
int x,y;
while(~scanf("%d",&n)&&n)
{
memset(Tree,,sizeof(Tree));
for(int i=;i<n;i++)
{
scanf("%d%d",&x,&y);
up(y,);
up(x-,-);
}
for(int i=;i<=n;i++)
printf("%d%c",get(i),i==n?'\n':' ');
}
return ;
}

2.对于[x,y]区间,对第y+1位减1,第x位加1,之后向下统计

#include<bits/stdc++.h>
using namespace std;
const int maxn=+;
int ans[maxn],Tree[maxn];
inline int lowbit(int x) //计算2^k
{
return (x&-x);
}
void add(int x,int value)
{
for(int i=x; i<maxn; i+=lowbit(i))
Tree[i]+=value;
}
int get(int x)
{
int sum=;
for(int i=x; i>; i-=lowbit(i))
sum+=Tree[i];
return sum;
}
int main()
{
int n,x,y;
while(~scanf("%d",&n)&&n)
{
memset(Tree,,sizeof(Tree));
for(int i=;i<n;i++)
{
scanf("%d%d",&x,&y);
add(x,);
add(y+,-);
}
for(int i=;i<=n;i++)
printf("%d%c",get(i),i==n?'\n':' ');
}
return ;
}

Color the ball(HDU1556)树状数组的更多相关文章

  1. hdu 1556 Color the ball(树状数组)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意:N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数[a,b]之间的气球 ...

  2. hdu 1556 Color the ball (树状数组)

    Color the ballTime Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  3. HDU 1556 Color the ball (树状数组 区间更新+单点查询)

    题目链接 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽&quo ...

  4. HDU 1556 Color the ball【树状数组】

    题意:给出n个区间,每次给这个区间里面的数加1,询问单点的值 一维的区间更新,单点查询,还是那篇论文里面讲了的 #include<iostream> #include<cstdio& ...

  5. HD1556Color the ball(树状数组)

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

  6. HDU1556(树状数组)

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

  7. hdoj--1556--Color the ball(模拟&&树状数组)

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

  8. hdu4605 magic ball game 树状数组+离线处理

    题意:给你一棵二叉树,每个节点有一个w值,现在有一颗小球,值为x,从根节点往下掉,如果w==x,那么它就会停止:如果w>x,那么它往左.右儿子的概率都是1.2:如果w<x,那么它往左儿子的 ...

  9. HDU 4605 Magic Ball Game 树状数组

    题目大意很简单. 有一颗树(10^5结点),所有结点要么没有子结点,要么有两个子结点.然后每个结点都有一个重量值,根结点是1 然后有一个球,从结点1开始往子孙结点走. 每碰到一个结点,有三种情况 如果 ...

随机推荐

  1. HYSBZ - 3676

    模板题.问你一个串里最大的值(回文子串*出现次数) /* gyt Live up to every day */ #include<cstdio> #include<cmath> ...

  2. canvas 实现飞碟射击游戏

    var canvas = document.getElementById('canvas'); var cxt = canvas.getContext('2d'); // 射击角度 var ang = ...

  3. 协程的NullReferenceException 错误

    public void loadPic(string url) { WWW www = new WWW(url); StartCoroutine(WaitForRequest(www)); } IEn ...

  4. 将excel的数据导入到数据库后都乱码了是怎么回事

    将excel内容首先保存成csv格式,然后在MySQL数据库中导入,结果汉字出现了乱码. 解决过程: 1.csv文件以txt形式打开,另存为,选择utf-8编码. 2.数据库,设置,collation ...

  5. 补全爬取的url

    有时爬取到的href不全,如href=‘/11031/’解决方法:from urllib import parseurl=parse.urljoin(response.url,get_url)resp ...

  6. IntelliJ IDEA 2017版 spring-boot2.0.2 搭建 JPA springboot DataSource JPA 实体类浅谈

    一.实体类分析 一般用到的实体类的类型有 String类型.Long类型.Integer类型.Double类型.Date类型.DateTime类型.Text类型.Boolean型等 1.String类 ...

  7. UVa 10163 Storage Keepers (二分 + DP)

    题意:有n个仓库,m个管理员,每个管理员有一个能力值P,每个仓库只能由一个管理员看管,但是每个管理员可以看管k个仓库(但是这个仓库分配到的安全值只有p/k,k=0,1,...),雇用的管理员的工资即为 ...

  8. rhel5.4+oracle 10g rac

    各种报错各种愁啊 ... 1> 不知道什么原因,在节点2执行root.sh 报错 .无解 . 还原虚拟机,重新安装 .唯一与以前不同的是,执行orainroot.sh后 接着在节点2执行.再去分 ...

  9. 第一次Java实验

      模仿JavaAppArguments.java实例,编写一个程序,此程序从命令行接受多个数字,求和之后输出. 1.设计思路:命令行参数都是字符串,必须将其转化成数字才能相加,定义一个数组接收字符串 ...

  10. POJ3111 K Best 2017-05-11 18:12 31人阅读 评论(0) 收藏

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 10261   Accepted: 2644 Case Time ...