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种颜色 ...
随机推荐
- WPF Adorner
之前做项目时,为了实现类似微信消息数目的效果 image.png ,我之前是修改的ControlTemplate.类似于将一个带数字的控件,放在另一个控件的右上角,来实现的这个效果. 原来WPF有 ...
- MVC中通过ajax判断输入的内容是否重复(新手笔记,请各位多多指教)
控制器代码: public string ValidateCarID(string carid)//这里接收ajax传递过来的值 { string result; Car car = db.Car.F ...
- [leetcode.com]算法题目 - Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【BZOJ2882】 工艺(SAM)
传送门 BZOJCH 洛谷 Solution 这个东西要求的不就是最小表示法吗? 把原串复制一遍然后都加到后缀自动机里面去. 用个map跑一下,这样子可以保证每一次选的是最小字典序的. 然后跑\(n\ ...
- awk的匹配
关系运算符 含义 用法示例 < 小于 x < y > 大于 x > y
- Python 模块之wxpython 的应用
第一个应用程序:“Hello World” 作为传统,我们首先将要写一个小的“Hello World”程序,下面是他的代码: #!/usr/bin/env python import wx app = ...
- 拿pyg 的fastdfs分布式文件系统 存储的位置
http://192.168.25.133/group1/M00/00/00/wKgZhVtW1vqAUpkZAABcxtZsmb0631.png StorageClient1 upload_fil ...
- D16——C语言基础学PYTHON
C语言基础学习PYTHON——基础学习D16 20180927内容纲要: 1.JavaScript介绍 2.JavaScript功能介绍 3.JavaScript变量 4.Dom操作 a.获取标签 b ...
- Python staticmethod classmethod 普通方法 类变量 实例变量 cls self 概念与区别
类变量 1.需要在一个类的各个对象间交互,即需要一个数据对象为整个类而非某个对象服务. 2.同时又力求不破坏类的封装性,即要求此成员隐藏在类的内部,对外不可见. 3.有独立的存储区,属于整个类. ...
- 【rocketMQ】1、搭建MQ服务器,生产一个订单与消费一个订单
1. 先解压 2. maven编译安装.(注意虚拟机采用nat网络模式,需要联网) mvn -Prelease-all -DskipTests clean install -U 启动nameser节点 ...