Stars(树状数组)
算法学习:http://www.cnblogs.com/George1994/p/7710886.html
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541
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
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=;
const int maxn=+;
const int maxk=+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int ans[maxn];//ans[i]代表有i个星星的点有多少个
int a[maxk];//a[i]代表横坐标为i的左下角(包括左和下)有多少个星星
int lowbit(int x)//可以求出x的二进制最低位1的位置的数值比如110,答案就是10
{
return x&(-x);
}
int getsum(int x)//求有多少个点
{
int sum=;
while(x>)
{
sum+=a[x];
x-=lowbit(x);
}
return sum;
}
void updata(int x)//更新操作
{
while(x<maxk)
{
a[x]++;
x+=lowbit(x);
}
}
int main()
{
int n,x,y;
while(scanf("%d",&n)!=EOF)
{
memset(ans,,sizeof(ans));
memset(a,,sizeof(a));
for(int i=;i<n;i++)
{
cin>>x>>y;
ans[getsum(x+)]++;//因为x可能为0,但是树状数组是从1开始的,所以整个坐标往右移
updata(x+);
}
for(int i=;i<n;i++)
cout<<ans[i]<<endl;
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=;
const ll maxa=;
#define INF 0x3f3f3f3f3f3f
int a[maxn],c[maxa];
int lowbit(int x)
{
return x&(-x);
}
int getsum(int x)
{
int res=;
while(x>)
{
res+=c[x];
x=x-lowbit(x);
}
return res;
}
void updata(int x)
{
while(x<maxa)
{
c[x]++;
x=x+lowbit(x);
}
}
int main()
{
int n,x,y;
memset(a,,sizeof(a));
memset(c,,sizeof(c));
scanf("%d",&n);
for(int i=;i<n;i++)
{
cin>>x>>y;
a[getsum(x+)]++;
updata(x+);
}
for(int i=;i<n;i++)
cout<<a[i]<<endl;
return ;
}
Stars(树状数组)的更多相关文章
- 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 ...
- Stars(树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=1541 Stars Time Limit: 2000/1000 MS (Java/Others) Memor ...
- Stars(树状数组单点更新)
Astronomers often examine star maps where stars are represented by points on a plane and each star h ...
- HDU-1541 Stars 树状数组
题目链接:https://cn.vjudge.net/problem/HDU-1541 题意 天上有许多星星 现给天空一个平面坐标轴,统计每个星星的level, level是指某一颗星星的左下角(x& ...
- POJ 2352 && HDU 1541 Stars (树状数组)
一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...
- hdu1541 Stars 树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目大意就是统计其左上位置的星星的个数 由于y已经按升序排列,因此只用按照x坐标生成一维树状数组 ...
- Stars(树状数组+线段树)
Stars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- POJ2352 Stars 树状数组
emm,ssy说可以直接CDQ分治...%%%但是注意到y是有序的,所以可以直接求一下前缀和就行了. 题干: Astronomers often examine star maps where sta ...
- POJ2352 Stars [树状数组模板]
题意:输入一n颗星星的x,y坐标,给定判断level的标准,即某颗星星左下边(不高于它,不超过他,相当于以他为基准的第三象限)星星的数目为level, 输出level从0到n的星星个数. //poj2 ...
随机推荐
- python 3中对list进行sort时,返回值为None
进行在用python的list结构时, 发现一个问题: methods = ['blogger.deletePost', 'blogger.get ...
- Python:sample函数
sample(序列a,n) 功能:从序列a中随机抽取n个元素,并将n个元素生以list形式返回. 例: from random import randint, sample date = [randi ...
- HTTP 2 VS HTTP 1.1
提升H5应用加载速度的方式有很多,比如缓存.cdn加速.代码压缩合并和图片压缩等技术. 今天介绍的是HTTP 2.0
- oracle--存储过程2--bk
oracle存储过程demo1---无返回值的存储过程: /* 写一个过程,可以向book表添加书 */ create table book( id number(4), book_name varc ...
- javascript基础之回调函数
简单来说,回调函数:也就是将要执行的函数. 回调函数具体的定义为:函数A作为参数(函数引用)传递到另一个函数B中,并且这个函数B执行函数A.我们就说函数A叫做回调函数.如果没有名称(函数表达式),就叫 ...
- 修改sharepoint列表样式
1.将sharepoint 的样式修改为阴影 2.用designer打开列表的AllItems.aspx文件,将下面的样式拷贝到里面 <style type="text/css ...
- Hadoop 1.2.1 MapReduce 例子
自学hadoop真的很困难,主要是hadoop版本太混乱了,各个版本之间兼容性并不算太好.更主要的是网上的很多MapReduce的Java例子不写import!!!只写类名!!!偏偏Hadoop中有很 ...
- TCP三次握手四次挥手原理
转自http://www.cnblogs.com/liuxiaoming/archive/2013/04/27/3047803.html TCP协议三次握手原理: 首先,给张图片,建立TCP三次握手的 ...
- Umbraco中根据ID获取IPublishedContent
Umbraco中根据ID来获取IPublishedContent 在Umbraco网站上的 https://our.umbraco.com/documentation/Reference/Templa ...
- 1.6 xss挑战平台练习
------------------------- XSS挑战之旅 ------------------------- 最近在学习xss,找到了一个xss练习平台,在线地址:http://test.x ...