POJ 2352 stars (树状数组入门经典!!!)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 54352 | Accepted: 23386 |
Description

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.
Input
Output
Sample Input
5
1 1
5 1
7 1
3 3
5 5
Sample Output
1
2
1
1
0
Hint
给你n给点的star的坐标,star左下角star的个数是star的等级(包括左下边界,即x相同或者y相同
最好输出等级1到等级n的star个数
所以这题可以用树状数组写
所以统计某个等级的个数就是统计x前面比它小的星星的数量
但是有需要注意的地方
树状数组是不能统计0的
所以每个x都要++
这也也不会影响统计的结果
#include<queue>
#include<set>
#include<cstdio>
#include <iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define max_v 320010
int a[max_v];
int c[max_v];
//C数组C[i] = a[i – 2^k + 1] + … + a[i] ,k为二进制下某尾0的个数。
int lowbit(int x)//返回2的k次方的值
{
return x&(-x);
}
void update(int x,int d)//更新树状数组
{
while(x<max_v)
{
c[x]=c[x]+d;
x=x+lowbit(x);
}
}
int getsum(int x)//就是把C数组全部加起来
{
int res=;
while(x>)
{
res=res+c[x];
x=x-lowbit(x);
}
return res; }
int main()
{
int n,x,y;
while(~scanf("%d",&n))
{
memset(c,,sizeof(c));
memset(a,,sizeof(a));
for(int i=;i<n;i++)
{
scanf("%d %d",&x,&y);//下标可能从0开始,所以要x+1
a[getsum(x+)]++;//求出其左下角star个数
update(x+,);//更新树状数组
}
for(int i=;i<n;i++)
{
printf("%d\n",a[i]);
}
}
return ;
}
/*
题目意思:
给你n给点的star的坐标,star左下角star的个数是star的等级(包括左下边界,即x相同或者y相同
最好输出等级1到等级n的star个数 分析:树状树主要用来解决区间问题
所以这题可以用树状数组写 一开始给出的数据是已经按照y升序排序好了的,如果y相同则按照x升序排序
所以统计某个等级的个数就是统计x前面比它小的星星的数量
但是有需要注意的地方
树状数组是不能统计0的
所以每个x都要++
这也也不会影响统计的结果 */
POJ 2352 stars (树状数组入门经典!!!)的更多相关文章
- POJ 2352 【树状数组】
题意: 给了很多星星的坐标,星星的特征值是不比他自己本身高而且不在它右边的星星数. 给定的输入数据是按照y升序排序的,y相同的情况下按照x排列,x和y都是介于0和32000之间的整数.每个坐标最多有一 ...
- hdu 1541/poj 2352:Stars(树状数组,经典题)
Stars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- POJ 2352 && HDU 1541 Stars (树状数组)
一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...
- 树状数组入门 hdu1541 Stars
树状数组 树状数组(Binary Indexed Tree(B.I.T), Fenwick Tree)是一个查询和修改复杂度都为log(n)的数据结构.主要用于查询任意两位之间的所有元素之和,但是每次 ...
- POJ 2299 Ultra-QuickSort 求逆序数 (归并或者数状数组)此题为树状数组入门题!!!
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 70674 Accepted: 26538 ...
- POJ-2352 Stars 树状数组
Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39186 Accepted: 17027 Description A ...
- Stars(树状数组或线段树)
Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37323 Accepted: 16278 Description A ...
- poj 2229 Ultra-QuickSort(树状数组求逆序数)
题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...
- Stars(树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=1541 Stars Time Limit: 2000/1000 MS (Java/Others) Memor ...
随机推荐
- CentOS-Linux系统下安装MySQL
一.mysql的安装 Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够 ...
- [SCOI2016]背单词——trie树相关
题目描述 Lweb 面对如山的英语单词,陷入了深深的沉思,”我怎么样才能快点学完,然后去玩三国杀呢?“.这时候睿智的凤老师从远处飘来,他送给了 Lweb 一本计划册和一大缸泡椒,他的计划册是长这样的: ...
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
下面的代码我调试了将近一个星期,你能够看出什么地方出了问题吗?线程函数: DWORD WINAPI ThreadProc( while(!bTerminate) { // 从 ...
- Redirect local emails to a remote email account
My previous post is a guide for setting up exim4, an SMTP mail server, to use Gmail as a smarthost. ...
- javascript的时间描述图怎么写
在gis系统中往往需要在一个时间间隔内把图形动态播放出来,比如2000年到现在地震变化啊,海啸的变化,在flex中这种展现方式需要后台rest服务相结合,要建立有时间点的图层,arcgis发布要选ti ...
- The String class's judging function
字符串的判断功能: package com.itheima_03; /* * Object:是类层次结构中的根类,所有的类都直接或者间接的继承自该类. * 如果一个方法的形式参数是Object,那么这 ...
- .net core系列之《对AOP思想的理解及使用AspectCore实现自定义日志拦截》
对于AOP这个名词,相信对于搞过MVC开发的人来说,都很熟悉,里面各种各样的Filter简直是将AOP体现到了极致. 那么什么是AOP呢? AOP(Aspect Oriented Programmin ...
- sql 2000 无法连接远程数据库 sqlserver不存在或访问被拒绝、不能打开到主机的连接,在端口1433:连接失败等 解决方案
问题: sql 2000 无法连接远程数据库 sqlserver不存在或访问被拒绝 telnet 127.0.0.1 1433 提示:不能打开到主机的连接,在端口1433:连接失败 解决方案: ...
- 海量数据处理面试题(1) 找出两文件种包含的相同的url
问题:给定a.b两个文件,各存放50亿个url,每个url各占64字节,内存限制是4G,让你找出a.b文件共同的url? 分析:50亿个url,每个url64字节,就是320G,显然是无法一次读入内存 ...
- 在oracle电子商务套件中输出信息
一.用自定义用户HAND_SL登陆http://zd01.haasgz.hand-china.com:30000/ 添加可执行并发程序 执行文件名填写自己的包名称.入口函数/过程名 二.将可执行程序添 ...