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.单点更新,区间查询(即求和) ...
随机推荐
- [USACO10NOV]购买饲料Buying Feed 单调队列优化DP
题目描述 约翰开车来到镇上,他要带 KKK 吨饲料回家.运送饲料是需要花钱的,如果他的车上有 XXX 吨饲料,每公里就要花费 X2X^2X2 元,开车D公里就需要 D×X2D\times X^2D×X ...
- 记因git规范导致的提测和发布延迟
号外 最近因为换工作的原因,我的博客和Github没有像之前那样频繁的更新了.一方面原因是投递简历和准备面试,由于之前的基础没有很扎实,需要把平时的知识点都整理一遍.这个时间段持续了20多天的样子,因 ...
- 详解 Redis 内存管理机制和实现
Redis是一个基于内存的键值数据库,其内存管理是非常重要的.本文内存管理的内容包括:过期键的懒性删除和过期删除以及内存溢出控制策略. 最大内存限制 Redis使用 maxmemory 参数限制最大可 ...
- Spring Boot2 系列教程(十七)SpringBoot 整合 Swagger2
前后端分离后,维护接口文档基本上是必不可少的工作. 一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理想的状态,实际开发中却 ...
- Ribbon - Customizing the Ribbon Client
自定义Ribbon算法 自定义Ribbon 官网文档链接 Ribbon github源码地址 <!--ribbon配置--> <dependency> <group ...
- vue 组件样式如何不影响全局
可以在 "style" 标签中添加 "scoped" 属性. <style scoped> .red { color: #f00; } </s ...
- KMP算法关键
Knuth-Morris-Pratt Algorithm 当初写这个博客之后一年多,再次看发现当初并不是完全弄明白了.这里为了“避免重复制造轮子”,引用大神博客. http://blog.csdn.n ...
- Spring Cloud Feign初接触
最近想使用下Feign,然后简单了解了一下,简单的搭了个demo. 首先简单介绍一下Feign,它是一个Http请求客户端,类似HttpClient,具体里面实现还没去看,知道它是一个请求客户端就行, ...
- SteamVR Plugin
使用HTC vive基于unity做虚拟现实,需要用到steamVR插件,最近查找了很多资料,稍微做一下总结. 做虚拟现实无非是头显在场景中的camera功能以及手柄的操作功能. (一)camera以 ...
- docker入门级详解
Docker 1 docker安装 yum install docker [root@topcheer ~]# systemctl start docker [root@topcheer ~]# mk ...