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 ...
随机推荐
- 2015安徽省赛 H.数7
http://xcacm.hfut.edu.cn/problem.php?id=1212 模拟大发 #include<iostream> #include<cstdio> #i ...
- Python自动化装饰器问题解疑
问题一 到底是怎么执行的? import time def timer(timeout=0): def decorator(func): def wrapper(*args, **kwargs): # ...
- Python自动化之面向对象进阶
1 静态方法 静态方法是不可以访问实例变量或类变量的,一个不能访问实例变量和类变量的方法,其实相当于跟类本身已经没什么关系了,它与类唯一的关联就是需要通过类名来调用这个方法. class Dog(ob ...
- 2.6---找有环链表的开头结点(CC150)
public ListNode detectCycle(ListNode head) { ListNode fast = head; ListNode slow = head; int flag = ...
- JSONP跨域的原理解析
JavaScript是一种在Web开发中经常使用的前端动态脚本技术.在JavaScript中,有一个很重要的安全性限制,被称为“Same- Origin Policy”(同源策略).这一策略对于Jav ...
- IPC----消息队列
消息队列可以认为是一个消息链表,System V 消息队列使用消息队列标识符标识.具有足够特权的任何进程都可以往一个队列放置一个消息,具有足够特权的任何进程都可以从一个给定队列读出一个消息.在某个进程 ...
- Light OJ 1032
数位dp,许多数位dp需要统计某种模式(子串)出现的数量,这种题通常需要在递归参数中加入高位已经出现过的模式的数量. #include <cstdio> #include <cstr ...
- ios 中的autoresizingMask
以前对这个知识理解的不太对,看了下面这个地址的文章后,感觉说的对,也没检验,今天实验后,发现是错的...在这里对以前读过此文的朋友表示抱歉. 原文地址如下: http://www.cnblogs.co ...
- markdown 基本语法
代码块: ```console.log(1);```--- 标题: # h1## h2### h3 --- 粗斜体: *斜体***粗体*****粗斜体*** --- 强调:`强调` --- 链接:[百 ...
- FFmpeg-20160413-snapshot-bin
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 F ...