POJ 2352 && HDU 1541 Stars (树状数组)
一開始想,总感觉是DP,但是最后什么都没想到。还暴力的交了一发。
然后開始写线段树,结果超时。感觉自己线段树的写法有问题。改天再写。先把树状数组的写法贴出来吧。
~~~~~~~~~~~~~~~~~~~~~~~~
树状数组不懂的去看刘汝佳的大白书,那个图画得非常清楚。
题目大意:星星的坐标以y递增的顺序给出,这些点的左下方的点数代表这个点的级数,问0~N-1的级数有多少个?事实上y根本木实用。
题目链接:http://poj.org/problem?id=2352
http://acm.hdu.edu.cn/showproblem.php?pid=1541
树状数组的思路在于:每一次询问,就相当于询问在1~x(包含x)间有多少个点。那么我们边读入,边更新就好。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 32000+10
using namespace std; int a[N],ans[N>>1];
int lowbit(int x)
{
return x&(-x);
}
int Sum(int x)
{
int tot=0;
while(x>0)
{
tot+=a[x];
x-=lowbit(x);
}
return tot;
}
void update(int x,int v)
{
while(x<=N)
{
a[x]+=v;
x+=lowbit(x);
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int x,y;
memset(a,0,sizeof(a));
memset(ans,0,sizeof(ans));
for(int i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
x++; //1~x的区间。
ans[ Sum(x) ]++;
update(x,1); //用1来更新。
}
for(int i=0;i<n;i++)
printf("%d\n",ans[i]);
}
return 0;
}
POJ 2352 && HDU 1541 Stars (树状数组)的更多相关文章
- hdu 1541 (基本树状数组) Stars
题目http://acm.hdu.edu.cn/showproblem.php?pid=1541 n个星星的坐标,问在某个点左边(横坐标和纵坐标不大于该点)的点的个数有多少个,输出n行,每行有一个数字 ...
- HDU 1541 STAR(树状数组)
Stars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- POJ 2892 Tunnel Warfare || HDU 1540(树状数组+二分 || 线段树的单点更新+区间查询)
点我看题目 题意 :N个村子连成一条线,相邻的村子都有直接的地道进行相连,不相连的都由地道间接相连,三个命令,D x,表示x村庄被摧毁,R ,表示最后被摧毁的村庄已经重建了,Q x表示,与x直接或间 ...
- Stars(树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=1541 Stars Time Limit: 2000/1000 MS (Java/Others) Memor ...
- hdu1541 Stars 树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目大意就是统计其左上位置的星星的个数 由于y已经按升序排列,因此只用按照x坐标生成一维树状数组 ...
- poj 1195:Mobile phones(二维树状数组,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14489 Accepted: 6735 De ...
- HDU 2838 (DP+树状数组维护带权排序)
Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...
- HDU 2689Sort it 树状数组 逆序对
Sort it Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- POJ-2352 Stars 树状数组
Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39186 Accepted: 17027 Description A ...
随机推荐
- 重新想象 Windows 8 Store Apps (21) - 动画: ThemeTransition(过渡效果)
原文:重新想象 Windows 8 Store Apps (21) - 动画: ThemeTransition(过渡效果) [源码下载] 重新想象 Windows 8 Store Apps (21) ...
- 【原创翻译】认识MVC设计模式:web应用开发的基础(实际编码篇)
原文地址:http://www.larryullman.com/2009/10/15/understanding-mvc-part-3/ 全系列INDEX [原创翻译]认识MVC设计模式:web应用开 ...
- AIX 7.1 install python
周围环境AIX7.1 设备python-2.6.2 因为互联网是非常多的安装文档.而且也没有细挑的版本号.因为我觉得python2.6 可能相对保守一些,至少之前用到的版本号是这个.所以此处依旧 ...
- C. Captain Marmot (Codeforces Round #271)
C. Captain Marmot time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Android 设计模式模式适配器
自定义适配器模式:一类的接口,转换成客户的期望,也是一个接口.适配器使原本接口不是与类兼容可以无缝.下面两个图看起来更加清晰 watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...
- Android异步任务
本文主要探讨Android平台提供的各种异步载入机制,包括它们的适用场景.用法等. 1. AsynTask AsynTask适用于最长能够持续几秒钟的短时间的操作,对于长时间的操作,建议使用java. ...
- 采用truelicense进行Java规划license控制 扩展可以验证后,license 开始结束日期,验证绑定一个给定的mac住址
采用truelicense进行Java规划license控制 扩展可以验证后,license 开始结束日期,验证绑定一个给定的mac住址. Truelicense 它是一个开源java license ...
- Could Not Connect
今天在写Quartz定时任务web应用时,访问项目页面出现Could Not Connect,后台也没报错. 然后我访问http://127.0.0.1:8080(Apache主页)的时候是正常的. ...
- 怎样设制 select 不可编辑 仅仅读
1. <select style="width:195px" name="role" id="role" onfocus=" ...
- Cocos2d-x场景变化相关功能介绍
现场由导演级交换机Director实现.之间的相关的功能,如下面: runWithScene(Scene* scene).该函数能够执行场景.仅仅能在启动第一个场景时候调用该函数.假设已经有一个场景执 ...