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 ...
随机推荐
- JavaWeb中监听器
一.事件源:三大域! ServletContext 生命周期监听:ServletContextListener,它有两个方法,一个在创建时调用,一个在销毁时调用: void contextIniti ...
- Line Numbers for RichText Control in C#
from: http://www.codeproject.com/Articles/38858/Line-Numbers-for-RichText-Control-in-C using Microso ...
- HTML新手推荐
对于前端的学习要先了解一下浏览器和html的发展史其次看看这篇文章:https://kb.cnblogs.com/page/129756/#chapter1我读到这句话时候感觉到了科技这个东西有很多时 ...
- java截取字符串
public class Temp { public static void main(String[] args) { String a="dsadgafa34"; System ...
- css 关闭按钮实现
通过css的伪元素:before,:after以及transform: rotate(45deg);旋转来实现(支持IE9及其以上版本) <div class="close" ...
- C++学习笔记(8)----C++类的大小
C++类的大小 (i) 如下代码: #include<iostream> using namespace std; class CBase { }; class CDerive :publ ...
- iOS各种profile文件
iOS是一个非常封闭的系统.授权文件(.mobileprovision)和签名证书文件(.cer)的存在就是为了验证身份信息.一般情况下,比如ssh登陆或者scp需要私钥.公钥对即可,iOS也是基本采 ...
- 3 使用selenium模拟登录csdn
之前通过F12开发者模式调试,获取网站后台服务器验证用户名和密码的URL之后,再构造post数据的方式会存在一个问题,就是对目标网站的验证机制不明确,构造post数据除了用户名和密码之外,还可能有更复 ...
- CSS盒子模型中()是透明的,这部分可以显示背景()
CSS盒子模型中()是透明的,这部分可以显示背景() border margin padding content 我的理解: · Margin(外边距) - 清除边框外的区域,外边距是透明 ...
- Java笔记-IO流的运用
--如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3877386.html "谢谢-- 1.InputStream和System ...