hdu 1541/poj 2352:Stars(树状数组,经典题)
Stars
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4052 Accepted Submission(s): 1592

For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.
You are to write a program that will count the amounts of the stars of each level on a given map.
int lowbit(int x)
{
return x & -x;
}
int sum(int a[],int x) //求出第x个元素之前的和
{
int ans = ;
while(x>){
ans+=a[x];
x -= lowbit(x); //向左上爬
}
return ans;
}
void add(int a[],int x,int d,int n) //将编号为x的数加d
{
while(x<=n){
a[x]+=d;
x+=lowbit(x);
}
}
代码:
#include <stdio.h>
#include <iostream>
using namespace std;
int lowbit(int x)
{
return x & -x;
}
int sum(int a[],int x) //求出第x个元素之前的和
{
int ans = ;
while(x>){
ans+=a[x];
x -= lowbit(x); //向左上爬
}
return ans;
}
void add(int a[],int x,int d,int n) //将编号为x的数加d
{
while(x<=n){
a[x]+=d;
x+=lowbit(x);
}
}
int main()
{
int i,n;
while(scanf("%d",&n)!=EOF){
int level[]={};
int x[]={};
for(i=;i<=n;i++){ //输入,计算当前星星前面的总星数(level),对应等级值+1,更新x[]数组
int a,b,num=;
scanf("%d%d",&a,&b);
level[sum(x,a+)]++;
add(x,a+,,); //这个位置的星星数+1
}
for(i=;i<n;i++) //输出
printf("%d\n",level[i]);
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hdu 1541/poj 2352:Stars(树状数组,经典题)的更多相关文章
- POJ 2352 【树状数组】
题意: 给了很多星星的坐标,星星的特征值是不比他自己本身高而且不在它右边的星星数. 给定的输入数据是按照y升序排序的,y相同的情况下按照x排列,x和y都是介于0和32000之间的整数.每个坐标最多有一 ...
- POJ 2481Cows(树状数组 + 好题)
Cows Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 15222 Accepted: 5070 Description ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- HDU 1166 敌兵布阵(线段树/树状数组模板题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 1166 敌兵布阵 树状数组小结(更新)
树状数组(Binary Indexed Tree(BIT), Fenwick Tree) 是一个查询和修改复杂度都为log(n)的数据结构.主要用于查询任意两位之间的所有 元素之和,但是每次只能修改一 ...
- st表树状数组入门题单
预备知识 st表(Sparse Table) 主要用来解决区间最值问题(RMQ)以及维护区间的各种性质(比如维护一段区间的最大公约数). 树状数组 单点更新 数组前缀和的查询 拓展:原数组是差分数组时 ...
- POJ 2352 && HDU 1541 Stars (树状数组)
一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...
- Stars(树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=1541 Stars Time Limit: 2000/1000 MS (Java/Others) Memor ...
随机推荐
- Initialization of deep networks
Initialization of deep networks 24 Feb 2015Gustav Larsson As we all know, the solution to a non-conv ...
- dedecms升级后报错
DedeCMS Error: (PHP 5.3 and above) Please set 'request_order' ini value to include C,G and P (recomm ...
- Integer Inquiry
Integer Inquiry Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Sub ...
- 磁盘 I/O 性能监控指标和调优方法
在介绍磁盘 I/O 监控命令前,我们需要了解磁盘 I/O 性能监控的指标,以及每个指标的所揭示的磁盘某方面的性能.磁盘 I/O 性能监控的指标主要包括:指标 1:每秒 I/O 数(IOPS 或 tps ...
- 教你安装漂亮的Arc GTK主题
导读 近日,我们又发现了一款深受 Linux 用户喜爱的桌面主题 — Arc GTK,Arc GTK 主题已被很多 GNU/Linux 操作系统支持和采用,其中就包括即将到来的 Linux Mint ...
- Unity3d 经验小结
Unity3d 经验小结 文本教程 你是第2541个围观者 0条评论 供稿者:Jamesgary 标签:unity3d教程 Fbx.贴图导入Unity时的注意事项: 在导出Fbx之前,Maya中已 ...
- 添加删除一个controller
(文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) rails generate controller Users rails destroy cont ...
- [codeforces 549]G. Happy Line
[codeforces 549]G. Happy Line 试题描述 Do you like summer? Residents of Berland do. They especially love ...
- OpenGL Vertex Array
转载 http://blog.csdn.net/dreamcs/article/details/7699603
- C++模板常用使用方法介绍
转载:http://developer.51cto.com/art/201002/182202.htm C++编程语言中的模板应用在一定程度上大大提高了程序开发的效率.我们在这篇文章中为大家详细讲解一 ...