hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7941 Accepted Submission(s): 4070
当N = 0,输入结束。
#include <iostream>
#include <stdio.h>
using namespace std;
#define MAXN 100010
struct Node{
int L,R;
int val; //被涂过的次数
};
Node a[MAXN*+];
void Init(int d,int l,int r) //初始化线段树
{
if(l==r){ //递归出口
a[d].L = l;
a[d].R = r;
a[d].val = ;
return ;
} //初始化当前节点
a[d].L = l;
a[d].R = r;
a[d].val = ; //递归初始化孩子节点
int mid = (l+r)/;
Init(d*,l,mid);
Init(d*+,mid+,r);
}
void Update(int d,int l,int r) //更新某一区间的值
{
if(a[d].L==l && a[d].R==r){ //递归出口。找到区间
a[d].val++;
return ;
}
if(a[d].L==a[d].R) //递归出口。没有找到
return ;
//没找到
int mid = (a[d].L+a[d].R)/;
if(mid>=r){ //去左孩子找
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(a[d].L==l && a[d].R==r) //找到区间
return a[d].val;
if(a[d].L==a[d].R)
return ; int mid = (a[d].L+a[d].R)/;
if(mid>=r){ //去左孩子找
return a[d].val + Query(d*,l,r);
}
else if(mid<l){ //去右孩子找
return a[d].val + Query(d*+,l,r);
}
else { //中点在要查询区间的中间,两边都要找
return a[d].val + Query(d*,l,mid) + Query(d*+,mid+,r);
}
}
int main()
{
int N,A,B,i,sum;
while(scanf("%d",&N)!=EOF && N){
Init(,,N);
for(i=;i<=N;i++){ //输入并更新线段树
scanf("%d%d",&A,&B);
Update(,A,B);
}
for(i=;i<=N;i++){ //输出每一个气球被涂过的次数
sum = Query(,i,i);
printf("%d",sum);
if(i!=N) printf(" ");
}
printf("\n");
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hdu 1556:Color the ball(线段树,区间更新,经典题)的更多相关文章
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hdu 1556 Color the ball 线段树 区间更新
水一下 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 ...
- hdu 1556 Color the ball(线段树区间维护+单点求值)
传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/3276 ...
- hdu 1556 Color the ball (线段树+代码详解)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 1556 Color the ball 线段树
题目链接:HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气 ...
- HDU 1556 Color the Ball 线段树 题解
本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...
- Color the ball 线段树 区间更新但点查询
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...
- hdu1556Color the ball线段树区间更新
题目链接 线段树区间更新更新一段区间,在更新区间的过程中,区间被分成几段,每一段的左右界限刚好是一个节点的tree[node].left和tree[node].right(如果不是继续分,直到是为止) ...
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
随机推荐
- linux下使用ffmpeg将amr转成mp3
说明:AMR格式是智能手机上的常用音频文件格式,比MP3格式的压缩比大.同样时长的AMR文件大概是MP3的十分之一,所以在移动互联项目中应用比较广泛.但目前AMR格式在个人电脑上应用较少,所以目前大部 ...
- Linux ffmpeg命令的介绍与使用
ffmpeg使用语法 ffmpeg [[options][`-i' input_file]]... {[options] output_file}... 如果没有输入文件,那么视音频捕捉(只在Linu ...
- 4.6---找二叉树中序后继(CC150)
因为,没有重复值,所以只需要做一个标记就OK了. public class Successor { static boolean flag = false; static int result = 0 ...
- 【leetcode】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- poj 1797(并查集)
http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...
- Core Data 多线程时多个context使用
原文地址:http://blog.csdn.net/willmomo/article/details/19759413 写的非常好!我这里截取一点重要的,备份,请去原地址阅读! 本人去年10月份才接触 ...
- Qt 改变图片大小
void Setting_TabProduct::changeImageSize(int width,int height,QString imgFile) { QPixmap pixmap(imgF ...
- ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)
两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...
- Python~第三方模块
第三方库还有MySQL的驱动:MySQL-python,用于科学计算的NumPy库:numpy,用于生成文本的模板工具Jinja2 模块搜索路径 Windows下: 双\\ sys.path.ap ...
- C字符串和指针问题汇总
空指针和传参问题 1) 段错误.形参改为二级指针即可 void GetMemory( char *p ){ p = ( ); } void Test( void ){ char *str = NULL ...