HDU1556(树状数组)
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17044 Accepted Submission(s): 8513
Problem Description
Input
当N = 0,输入结束。
Output
Sample Input
Sample Output
树状数组入门,区间更新,单点查询
//2016.8.10
//树状数组,区间更新,单点查询
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; int arr[], n;//arr[i]表示i管辖的范围内数字之和 int lowbit(int x){return x&(-x);} int add(int pos, int tt)
{
for(int i = pos; i <= n; i+=lowbit(i))
arr[i] += tt;
return ;
} int query(int pos)
{
int sum = ;
for(int i = pos; i > ; i-=lowbit(i))
sum+=arr[i];
return sum;
} int main()
{
int a, b;
while(cin>>n)
{
memset(arr, , sizeof(arr));
for(int i = ; i < n; i++)
{
scanf("%d%d", &a, &b);
add(a, );
add(b+, -);
}
for(int i = ; i <= n; i++)
if(i == n)
cout<<query(i)<<endl;
else cout<<query(i)<<" ";
} return ;
}
// 2018-03-28
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n;
while(cin.hasNext()) {
n = cin.nextInt();
if(n == 0)break;
BinaryIndexedTrees bit = new BinaryIndexedTrees(n);
int l, r;
for(int i = 0; i < n; i++) {
l = cin.nextInt();
r = cin.nextInt();
bit.updata(l, r, 1);
}
for(int i = 1; i <= n; i++) {
if(i == n)System.out.printf("%d \n", bit.query(i));
else System.out.printf("%d ", bit.query(i));
}
}
}
}
class BinaryIndexedTrees {
/*
* 区间更新,单点查询
*/
int n;
int [] e;
BinaryIndexedTrees(int _n) {
this.n = _n;
e = new int[n+1];
}
// x+lowbit(x)表示到达x的父节点
// x-lowbit(x)表示到达x点管辖区间的下个区间的管辖点
int lowbit(int x) {
return x&(-x);
}
void add(int pos, int val) {
for(int i = pos; i <= n; i+=lowbit(i))
e[i] += val;
}
int query(int pos) {
int sum = 0;
for(int i = pos; i > 0; i-=lowbit(i))
sum += e[i];
return sum;
}
// 给[l, r]区间内的每个数加上val
void updata(int l, int r, int val) {
add(l, val);
add(r+1, -val);
}
}
HDU1556(树状数组)的更多相关文章
- hdu1556 树状数组区间更新单点查询板子
就是裸的区间更新: 相对于直观的线段树的区间更新,树状数组的区间更新原理不太相同:由于数组中的一个结点控制的是一块区间,当遇到更新[l,r]时,先将所有能控制到 l 的结点给更新了,这样一来就是一下子 ...
- Color the ball(HDU1556)树状数组
每次对区间内气球进行一次染色,求n次操作后后所有气球染色次数. 树状数组,上下区间更新都可以,差别不大. 1.对于[x,y]区间,对第x-1位减1,第y位加1,之后向上统计 #include<b ...
- hdu1556树状数组的区间更新单点查询
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- ZZNU 2098 Drink coffee(差分+树状数组)
题目链接:http://acm.hi-54.com/problem.php?pid=2098 2098 : Drink coffee 时间限制:1 Sec 内存限制:256 MiB 提交:32 答案正 ...
- bryce1010专题训练——树状数组
Bryce1010模板 1.一维树状数组 https://vjudge.net/contest/239647#problem/A[HDU1556] #include<bits/stdc++.h& ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- bzoj1878--离线+树状数组
这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
随机推荐
- linux command ---1
查看Linux的内核版本 当前系统的发行版信息(distribution):lsb_release -a , lsb(linux standard Base) and distribution inf ...
- NGUI具有流光效果的UISprite
之前做过一个流光效果(http://www.cnblogs.com/jietian331/p/4748644.html). 现将其改进一下,与NGUI结合起来,提供一个具有流光效果的组件:UIWalk ...
- (简单) POJ 2253 Frogger,Dijkstra。
Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Fro ...
- 3505: [Cqoi2014]数三角形
3505: [Cqoi2014]数三角形 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1324 Solved: 807[Submit][Statu ...
- Mac下配置node.js环境(Mac 10.12)
有安装就有卸载,卸载教程参考:http://www.cnblogs.com/EasonJim/p/6287141.html 一.官方下载pkg安装包 1.安装 到官网https://nodejs.or ...
- 利用STM32F唯一96bit序列号实现反拷贝加密的源代码公开(转)
源:利用STM32F唯一96bit序列号实现反拷贝加密的源代码公开 //---------------------------------------------------------------- ...
- tomcat 插件
1. http://www.eclipsetotale.com/tomcatPlugin.html
- jenkins 构建时,取消构建测试类
如图 点击配置,添加clean install -Dmaven.test.skip=true 保存即可
- Unity3D ——强大的跨平台3D游戏开发工具(四)
第六章 Unity3D中的C#Script编程的注意事项 也许您在学习Unity3D之前,已经是一位C#的编程高手了.但在Unity3D中的C#并不像真正的C#那般强大,在Unity3D的C#中必须全 ...
- RabbitMQ消息队列(二):”Hello, World“
本文将使用Python(pika 0.9.8)实现从Producer到Consumer传递数据”Hello, World“. 首先复习一下上篇所学:RabbitMQ实现了AMQP定义的消息队列.它实现 ...