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 ...
随机推荐
- django-cms安装
ubuntu:12.04 (32bit) djangocms 0.5.1 =========================== 首先,跟着这个做: https://github.com/divio/ ...
- JavaScript——同源策略
概念:同源策略是客户端脚本(尤其是Javascript)的重要的安全度量标准.它最早出自Netscape Navigator2.0,其目的是防止某个文档或脚本从多个不同源装载. 这里的同源指的是: ...
- Unity3d 扩展自定义类Inspector
public class MyClass : MonoBehaviour { public int A; // Use this for initialization void Start () { ...
- 解读Unity中的CG编写Shader系列七(不透明度与混合)
转自http://www.itnose.net/detail/6098539.html 1.不透明度 当我们要将两个半透的纹理贴图到一个材质球上的时候就遇到混合的问题,由于前面的知识我们已经知道了片段 ...
- (转)C++0x语言新特性一览
转自:http://blog.csdn.net/zwvista/article/details/2429781 原文请见http://en.wikipedia.org/wiki/C%2B%2B0x. ...
- 8.js模式-状态模式
1. 状态模式 var offLightState = function(light){ this.light = light; } offLightState.prototype.buttonWas ...
- 11. javacript高级程序设计-DOM扩展
1. DOM扩展 1.1 选择符API l querySelector() 接收一个css选择符,返回与该模式匹配的第一个元素 l querySelectorAll() 接收一个css选择符,返回所有 ...
- Java for LeetCode 237 Delete Node in a Linked List
Java实现如下: public class Solution { public void deleteNode(ListNode node) { if(node==null||node.next== ...
- Excel保护工作表
最近见到一个Excel文件,其中的表不能选中,不能编辑,今天研究了一下 明白了其设置方法 excel中开始--格式--保护工作表--去掉所有勾选,输入保护密码 即可
- VB兼容问题
window7 64位无法显示打印窗问题 在Windows7 64位和VS2008环境下,PrintDialog.ShowDialog不能显示打印对话框 在VS2008中编写?如下代码: PrintD ...