hdu 1556 Color the ball (技巧 || 线段树)
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 28415 Accepted Submission(s): 13851
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
当N = 0,输入结束。
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
1 1 1
3 2 1
C/C++(线段树):
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 5e5 + ; int n, a, b; struct nod
{
int L, R, val;
}node[MAX]; void build(int d, int l, int r)
{
node[d].L = l, node[d].R = r, node[d].val = ;
if (l == r) return;
int mid = (l + r) >> ;
build(d << , l, mid);
build((d << ) + , mid + , r);
} void update(int d, int l, int r)
{
if (node[d].L == l && node[d].R == r)
{
node[d].val ++;
return;
}
if (node[d].L == node[d].R) return;
int mid = (node[d].L + node[d].R) >> ;
if (r <= mid)
update(d << , l, r);
else if (mid < l)
update((d << ) + , l, r);
else
{
update(d << , l, mid);
update((d << ) + , mid + , r);
}
} int query(int d, int l, int r)
{
if (node[d].L == l && node[d].R == r) return node[d].val;
if (node[d].L == node[d].R) return ;
int mid = (node[d].L + node[d].R) >> ;
if (r <= mid)
return node[d].val + query(d << , l, r);
else if (mid < l)
return node[d].val + query((d << ) + , l, r);
else
return node[d].val + query(d << , l, mid) + query((d << ) + , mid + , r);
} int main()
{
while (scanf("%d", &n), n)
{
build(, , n);
for (int i = ; i < n; ++ i)
{
scanf("%d%d", &a, &b);
update(, a, b);
}
for (int i = ; i < n; ++ i)
printf("%d ", query(, i, i));
printf("%d\n", query(, n, n));
}
return ;
}
C/C++(技巧):
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 1e5 + ; int n, a, b; int main()
{
while (scanf("%d", &n), n)
{
int num[MAX] = {}, m = ;
for (int i = ; i <= n; ++ i)
{
scanf("%d%d", &a, &b);
num[a] ++, num[b + ] --;
}
for (int i = ; i < n; ++ i)
{
m += num[i];
printf("%d ", m);
}
printf("%d\n", m + num[n]);
}
return ;
}
hdu 1556 Color the ball (技巧 || 线段树)的更多相关文章
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 1556 Color the ball(线段树:区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意: N个气球,每次[a,b]之间的气球涂一次色,统计每个气球涂色的次数. 思路: 这道题目用树状数组和 ...
- hdu 1556 Color the ball (线段树做法)
Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a ...
- hdu 1556 Color the ball(非线段树做法)
#include<stdio.h> #include<string.h> ]; int main() { int n,i; int a,b; while(scanf(" ...
- hdoj 1556 Color the ball【线段树区间更新】
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1199 Color the Ball(离散化线段树)
Color the Ball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和
题意:给你n个球排成一行,初始都为黑色,现在给一些操作(L,R,color),给[L,R]区间内的求染上颜色color,'w'为白,'b'为黑.问最后最长的白色区间的起点和终点的位置. 解法:先离散化 ...
- hdu 1199 Color the Ball 离散线段树
C - Color the Ball Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU 1556 Color the ball (一维树状数组,区间更新,单点查询)
中文题,题意就不说了 一开始接触树状数组时,只知道“单点更新,区间求和”的功能,没想到还有“区间更新,单点查询”的作用. 树状数组有两种用途(以一维树状数组举例): 1.单点更新,区间查询(即求和) ...
随机推荐
- PMBOK(第六版) PMP笔记——《十三》第十三章(项目干系人管理)
PMBOK(第六版) PMP笔记——<十三>第十三章(项目干系人管理) 第十三章 项目干系人管理: 了解干系人的需要和期望.解决实际发生的问题.管理利益冲突.促进干系人合理参与 项目决策和 ...
- PMP 德尔菲技术
1.德尔菲技术,必须遵守以下几个规则: 每个专家只与主持人单线联系. 专家之间完全背靠背,更不能进行讨论.为保证专家提出独立见解,甚至需要把专家分散在不同的物理地点. 专家以匿名的书面形式提出意见. ...
- 如何利用Fiddler4进行Android APP / IOS APP抓包
Fiddler抓包 1.Fiddler介绍 Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据. F ...
- django-URL默认参数传递
主要用在分页中. book/views.py def page(request,pn=): return HttpResponse("<h1>{}</h1>" ...
- vue事件
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- fenby C语言 P6
printf=格式输出函数; printf=("两个相加的数字是:%d,%d,他们的和是:%d\n",a,b,c); %d整数方式输出; \n=Enter; int a=1; fl ...
- SSM简历模板1.0
张三 xxx-xxxx-xxxx| xxxxxxx@qq.com| 南京 x岁 | 籍贯:江苏 已离职 | 求职意向:java开发工程师 | 期望薪资:面议 专业技能 1.熟悉MVC体系结构模式.B/ ...
- egg-mongoose --- nodejs
项目 egg + mongoose 项目结构 配置 egg 安装模块 npm i egg-mongoose --save config/pulgin.js exports.mongoose = { e ...
- springboot---发送邮件
1.pom.xml配置 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...
- 解决移动端touch事件与click冲突的问题
最简单的办法,就只绑定一个事件不就行了: 第二种,我觉得和第一种也没啥区别.. const Button = document.getElementById("targetButton&qu ...