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 ...
随机推荐
- hdu1455 dfs+剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- PyQt4自定义事件
listview控件与updateText 相关联 self.listview.updateText.connect(self.viewlist) updateText = QtCore.pyqt ...
- VQuery选择器
VQUery elements属性,储存选中的元素 参数 typeof的作用 字符串 class ID tagName 函数 事件绑定 对象 直接插入 $函数 绑定事件 click方法 显示隐藏,- ...
- Best Time to Buy and Sell Stock with Cooldown
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【leetcode】Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 第十天 多进程、协程(multiprocessing、greenlet、gevent、gevent.monkey、select、selector)
1.多进程实现方式(类似于多线程) import multiprocessing import time,threading def thread_run():#定义一个线程函数 print(&quo ...
- Docker的安装配置及使用详解
基本概念 Docker 包括三个基本概念 镜像(Image) 容器(Container) 仓库(Repository) 先理解了这三个概念,就理解了 Docker 的整个生命周期. 1.docker安 ...
- strcpy vs memcpy
[本文连接] http://www.cnblogs.com/hellogiser/p/strcpy_vs_memcpy.html [分析] strcpy和memcpy都是标准C库函数,它们有下面的特点 ...
- NGUI研究院之在Unity中使用贝塞尔曲线(六)[转]
鼎鼎大名的贝塞尔曲线相信大家都耳熟能详.这两天因为工作的原因需要将贝塞尔曲线加在工程中,那么MOMO迅速的研究了一下成果就分享给大家了哦.贝塞尔曲线的原理是由两个点构成的任意角度的曲线,这两个点一个是 ...
- 配置ubuntu虚拟机备忘
#1配置minicom sudo minicom -s sudo minicom -w #1.配置网络,嵌入机的ip地址 ifconfig eth0 10.5.52.202 #2.挂载文件,把虚拟主机 ...