Color the ball(hdu1556)(hash)或(线段树,区间更新)
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
b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
<= a <= b <= N)。
当N = 0,输入结束。
#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)或(线段树,区间更新)的更多相关文章
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- ZOJ 1610 Count the Color(线段树区间更新)
描述Painting some colored segments on a line, some previously painted segments may be covered by some ...
- hdu1556Color the ball线段树区间更新
题目链接 线段树区间更新更新一段区间,在更新区间的过程中,区间被分成几段,每一段的左右界限刚好是一个节点的tree[node].left和tree[node].right(如果不是继续分,直到是为止) ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
- HDU_1556_线段树区间更新
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)
1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...
- POJ-2528 Mayor's posters(线段树区间更新+离散化)
http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...
- hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新
#1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...
- HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...
随机推荐
- Spring AOP 源码分析 - 筛选合适的通知器
1.简介 从本篇文章开始,我将会对 Spring AOP 部分的源码进行分析.本文是 Spring AOP 源码分析系列文章的第二篇,本文主要分析 Spring AOP 是如何为目标 bean 筛选出 ...
- 【计算机网络】数据交换技术和多路复用技术的正(nao)确(can)打开方式
交换的作用 数据交换是计算机网络中两个终端进行数据传输的方式,它又可以分成两种类型:电路交换和分组交换.很显然,问题的核心在于“交换”,那么我们首先要思考的是:交换的作用是什么? “交换”的作 ...
- <转>php中heredoc与nowdoc的使用方法
http://www.361way.com/php-heredoc-nowdoc/3008.html 一.heredoc结构及用法 Heredoc 结构就象是没有使用双引号的双引号字符串,这就是说在 ...
- 设计模式《JAVA与模式》之调停者模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述调停者(Mediator)模式的: 调停者模式是对象的行为模式.调停者模式包装了一系列对象相互作用的方式,使得这些对象不必相互明显引用.从 ...
- Windows平台下搭建自己的Git服务器
该文章转自:http://www.codeceo.com/article/windows-git-server.html Gitblit 是一个纯 Java 库用来管理.查看和处理 Git 资料库,相 ...
- [原创]K8 MSF Bind Shell TCP 连接工具
工具: K8_MSFBindShellClient_20170524[K.8]编译: 自己查壳组织: K8搞基大队[K8team]作者: K8拉登哥哥博客: http://qqhack8.blog.1 ...
- 详解使用flask_paginate进行分页
分页技术好处: 1.分页技术是把数据全部查询出来,然后再进行分页 2.分页技术可以,降低带宽使用,提高访问速度 使用flask_paginate进行分页 1.要使用flask_paginate,首先安 ...
- C#实现程序的版本升级更新
我们做了程序,不免会有版本升级,这就需要程序有自动版本升级的功能.那么看看我是如何实现程序自动更新的. 直接上代码: using System; using System.Collections.Ge ...
- getFields和getDeclaredFields
getFields()获得某个类的所有的公共(public)的字段,包括父类. getDeclaredFields()获得某个类的所有申明的字段,即包括public.private和proteced, ...
- CentOS7运行Tomcat8时启动慢,访问总是转圈,但是过一会又好了
我一开始遇到这个问题的时候也是懵逼的. 这叫什么问题... 描述一下,当输入命令启动tomcat之后,访问网站总是不能访问,但是5.6分钟之后又好了,有时候好,有时候不行. 遇到这样的问题运用以下的方 ...