Color the ball

Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9502 Accepted Submission(s):
4872

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
 
由于数据比较多所以简单的可以用hash,当然也可以用线段树区间更新!
 
 
 
 
hash
#include<stdio.h>
#include<string.h>
int hash[];
int main()
{
int n,i,x,y,sum;
while(scanf("%d",&n),n)
{
memset(hash,,sizeof(hash));
for(i=;i<=n;i++)
{
scanf("%d%d",&x,&y);
hash[x]++;
hash[y+]--;
}
printf("%d",hash[]);
sum=hash[];
for(i=;i<=n;i++)
{
sum+=hash[i];
printf(" %d",sum);
}
printf("\n");
}
return ;
}

线段树区间跟新。

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm> #define Lson left,mid,n<<1
#define Rson mid+1,right,n<<1|1
const int MAX=;
const int Max=<<;
int s[Max];
int m;
using namespace std;
typedef struct Node
{
int left;
int right;
int value;
};
Node node[Max]; void build_tree(int left,int right,int n)
{
node[n].left=left;
node[n].right=right;
node[n].value=;
if(node[n].left==node[n].right)
return ;
int mid=(node[n].left+node[n].right)>>;//位运算相当于除以2
build_tree(Lson);
build_tree(Rson);
}
void query(int left,int right,int n)//查询
{
if(node[n].left>=left&&node[n].right<=right)//找到要涂颜色区间,这里很重要,表示用区间后再慢慢回归到子节点上
{
node[n].value+=;
return;
}
int mid=(node[n].left+node[n].right)>>;
if(right<=mid)
query(left,right,n<<);
else if(left>mid)
query(left,right,n<<|);//相当于2*n+1
else
{
query(Lson);
query(Rson);
}
} void sum(int n)
{
if(node[n].left==node[n].right)
{
s[m]=node[n].value;
m+=;
return;
}
node[n<<].value+=node[n].value;
node[n<<|].value+=node[n].value;
sum(n<<);
sum(n<<|);
} int main()
{
int n,i,j,a,b;
while(scanf("%d",&n)&&n)
{
build_tree(,n,);
for(i=;i<n;i++)
{
scanf("%d%d",&a,&b);
query(a,b,);
}
m=;
sum();
for(i=;i<m;i++)
{
if(i==m-)
printf("%d\n",s[i]);
else
printf("%d ",s[i]);
}
}
return ;
}
 

Color the ball(hdu1556)(hash)或(线段树,区间更新)的更多相关文章

  1. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  2. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  3. ZOJ 1610 Count the Color(线段树区间更新)

    描述Painting some colored segments on a line, some previously painted segments may be covered by some ...

  4. hdu1556Color the ball线段树区间更新

    题目链接 线段树区间更新更新一段区间,在更新区间的过程中,区间被分成几段,每一段的左右界限刚好是一个节点的tree[node].left和tree[node].right(如果不是继续分,直到是为止) ...

  5. 【POJ 2777】 Count Color(线段树区间更新与查询)

    [POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4094 ...

  6. HDU_1556_线段树区间更新

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

  7. ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)

    1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...

  8. POJ-2528 Mayor's posters(线段树区间更新+离散化)

    http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...

  9. hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新

    #1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...

  10. HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...

随机推荐

  1. Angular build Error:In this configuration Angular requires Zone.js

    Angular cli 运行 build后打开生成的index.html报错:In this configuration Angular requires Zone.js 生成代码如下: ng bui ...

  2. 【转】JS中的call()和apply()方法

    原文:http://uule.iteye.com/blog/1158829 1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[,   [,.argN]]]]]) ...

  3. 《jQuery基础教程(第四版)》学习笔记

    本书代码参考:Learning jQuery Code Listing Browser 原书: jQuery基础教程 目录: 第2章 选择元素 1. 使用$()函数 2. 选择符 3. DOM遍历方法 ...

  4. [BeiJing wc2012]连连看(建模,最小费用最大流)

    前言 突然发现自己在图论①被dalao吊着打... Solution 看到数据范围1000,感觉可以直接枚举连边,然后新建两个点就好了. 注意要拆点,不然可能会死循环(过来人) 代码实现 #inclu ...

  5. 【bzoj3218】a+b Problem 最小割+主席树

    数据范围:$n≤5000$,$a,l,r≤10^9$,$b,w,p≤2\times 10^5$. 我们考虑一种暴力的最小割做法: 首先令$sum=\sum\limits_{i=1}^{n} b_i+w ...

  6. QQ gtk,bkn算法

    public long GetGTK(string sKey) { ; , len = sKey.Length; i < len; ++i) { hash += (hash << ) ...

  7. android(java) 开发过程中经验及总结记录

    android(java) 开发过程中经验及总结记录

  8. python中文画图显示乱码解决办法

    最近使用notebook Python中的matplotlib作图,发现中文设置的坐标标签和title都显示为乱码,用了网上的许多教程都不管用,嘴后解决的方式是设置下述的两行即可搞定: plt.rcP ...

  9. nc命令简介

    nc介绍 ncat/nc 既是一个端口扫描工具,也是一款安全工具,还能是一款监测工具,甚至可以做为一个简单的 TCP 代理. 在大多数 Debian 发行版中,nc 是默认可用的,它会在安装系统的过程 ...

  10. Android中常见的对话框

    1. 普通对话框 public void click01(View view){ AlertDialog.Builder builder = new AlertDialog.Builder(this) ...