算法学习:http://www.cnblogs.com/George1994/p/7710886.html

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541

Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.

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

The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.

Output

The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.

Sample Input

5
1 1
5 1
7 1
3 3
5 5

Sample Output

1
2
1
1
0

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.
 
题目大意:求左下方有多少个点,包括左方和下方
个人思路:也没什么思路,就是用树状数组吧,第一次接触,有点难
看代码:
#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(树状数组)的更多相关文章

  1. POJ-2352 Stars 树状数组

    Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39186 Accepted: 17027 Description A ...

  2. Stars(树状数组或线段树)

    Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37323 Accepted: 16278 Description A ...

  3. Stars(树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=1541 Stars Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  4. Stars(树状数组单点更新)

    Astronomers often examine star maps where stars are represented by points on a plane and each star h ...

  5. HDU-1541 Stars 树状数组

    题目链接:https://cn.vjudge.net/problem/HDU-1541 题意 天上有许多星星 现给天空一个平面坐标轴,统计每个星星的level, level是指某一颗星星的左下角(x& ...

  6. POJ 2352 &amp;&amp; HDU 1541 Stars (树状数组)

    一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...

  7. hdu1541 Stars 树状数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目大意就是统计其左上位置的星星的个数 由于y已经按升序排列,因此只用按照x坐标生成一维树状数组 ...

  8. Stars(树状数组+线段树)

    Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  9. POJ2352 Stars 树状数组

    emm,ssy说可以直接CDQ分治...%%%但是注意到y是有序的,所以可以直接求一下前缀和就行了. 题干: Astronomers often examine star maps where sta ...

  10. POJ2352 Stars [树状数组模板]

    题意:输入一n颗星星的x,y坐标,给定判断level的标准,即某颗星星左下边(不高于它,不超过他,相当于以他为基准的第三象限)星星的数目为level, 输出level从0到n的星星个数. //poj2 ...

随机推荐

  1. BZOJ4364:[IOI2014]Wall

    浅谈区间最值操作与历史最值问题:https://www.cnblogs.com/AKMer/p/10225100.html 题目传送门:https://lydsy.com/JudgeOnline/pr ...

  2. virtual judge(专题一 简单搜索 C)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  3. Java常见设计模式之工厂模式

    工厂模式在我们日常的应用中应当算是比较广泛的一种设计模式了.今天让我们一起来学习一下,工厂的设计模式. 工厂模式在<Java与模式>中分为三类:     1)简单工厂模式(Simple F ...

  4. java注解的基本知识

    1: 注解:Annotation是一种应用于类.方法.参数.变量.构造器及包生命中的特殊修饰符,是一种由JSR-175标准选择用来描述代码的元数据. Java中如下的4种注解,专门负责新注解的创建: ...

  5. js 函数定义的两种方式以及事件绑定(扫盲)

    一.事件(例如:onclick)绑定的函数定义放在jsp前面和放后面没影响 二. $(function() { function func(){}; }) onclick通过如下方式绑定事件到jsp中 ...

  6. Material使用04 MdCardModule和MdButtonModule综合运用

    设计需求:设计一个登陆页面 1 模块导入 1.1 将MdCardModule和MdButtonModule模块导入到共享模块中 import { NgModule } from '@angular/c ...

  7. p1640&bzoj1854 连续攻击游戏(游戏)

    传送门(洛谷) 传送门(bzoj) 题目 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用 ...

  8. linux 统计 程序 运行时间

    测试 代码运行时间 linux 中的 <sys/time.h> 中 有个函数可以获取当前时间,精确到 微秒 ---->  gettimeofday() #include <sy ...

  9. MQTT协议实现Eclipse Paho学习总结二

    一.概述 前一篇博客(MQTT协议实现Eclipse Paho学习总结一) 写了一些MQTT协议相关的一些概述和其实现Eclipse Paho的报文类别,同时对心跳包进行了分析.这篇文章,在不涉及MQ ...

  10. 【机器学习】关联规则挖掘(二):频繁模式树FP-growth

    Apriori算法的一个主要瓶颈在于,为了获得较长的频繁模式,需要生成大量的候选短频繁模式.FP-Growth算法是针对这个瓶颈提出来的全新的一种算法模式.目前,在数据挖掘领域,Apriori和FP- ...