HDU 1556 Color the ball【差分数组裸题/模板】
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
【题意】:
给出一个区间n, n个数初始值都是0, 给出n个区间修改,修改是在[l,r]上加1, 最后输出n个数。
【分析】:
属于离线查询, 可以维护差分数组来求出最后的数字。
【代码】:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2e5 + 10;
int n;
int a,b;
int x[maxn],p[maxn];
int main()
{
while(~scanf("%d",&n),n)
{
memset(x,0,sizeof(x));
memset(p,0,sizeof(p));
for(int i=1; i<=n; i++)
{
scanf("%d%d",&a,&b);
p[a]++;
p[b+1]--;
}
for(int i=1;i<=n;i++)
{
x[i] = x[i-1] + p[i];
}
for(int i=1; i<=n; i++)
{
if(i==n)
printf("%d\n",x[i]);
else printf("%d ",x[i]);
}
}
}
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(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- 解题报告:hdu 1556 Color the ball(区间修改,单点查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N ...
- 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 (树状数组)
Color the ballTime Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 1556.Color the ball-差分数组-备忘
备忘. 差分数组: 区间更新查询有很多方法,线段树.树状数组等都可以.如果为离线查询,就可以考虑使用差分数组. 假设对于区间[l,r]的每个数都加1,我们用一个数组a来记录,a[l]+=1;a[r+1 ...
- hdu 1556 Color the ball 线段树
题目链接:HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气 ...
- 线段树(求单结点) hdu 1556 Color the ball
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- JVM GC Q&A(补充ing)
1.如果一个对象没有与其相连的GC ROOT,一定会被回收吗? 这个对象并非是非死不可的,这时他只是处于死缓阶段,要真正宣告一个对象的死亡,至少要经历两次标记过程:如果对象在进行可达性分析后发现并没有 ...
- 附录A培训实习生-面向对象基础方法重载(3)
就上一篇代码而言,你如果写Cat cat = new Cat();会直接报错错误 : 1 “Cat”方法没有采用“0”个参数的重载 E:\大话设计模式学习\BigDesignPattern ...
- [Leetcode] remove element 删除元素
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- [poj 3436]最大流+输出结果每条边流量
题目链接:http://poj.org/problem?id=3436 大力套kuangbin板过了orz #include<cstdio> #include<cstring> ...
- POJ1511:Invitation Cards(最短路)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 34743 Accepted: 114 ...
- 固定width但是有间隔
<!DOCTYPE > <html> <head> <title></title> <meta name="name&quo ...
- webkit在vs2008中编译
转载自:http://xjchilli.blog.163.com/blog/static/4534773920091016115533158/ webkit的官方网站写的webkit需要在vs2005 ...
- ZOJ3872 Beauty of Array---规律 | DP| 数学能力
传送门ZOJ 3872 Beauty of Array Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has an array A ...
- 动态规划:LCS
先上状态转移方程,还是很容易看明白的 例题是Codevs的1862,这个题不是实现了方程就可以了的,还要完成一个事情那就是计数,数一数到底有多少个最长公共子序列 #include<cstdio& ...
- 【Codeforces】849D. Rooter's Song
[算法]模拟 [题意]http://codeforces.com/contest/849/problem/D 给定n个点从x轴或y轴的位置p时间t出发,相遇后按对方路径走,问每个数字撞到墙的位置.(还 ...