POJ 2352 && HDU 1541 Stars (树状数组)
来源:互联网 发布:移动网络电视不清楚 编辑:程序博客网 时间:2024/05/16 08:48
一开始想,总感觉是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+10using 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;}
2 1
- POJ 2352 && HDU 1541 Stars (树状数组)
- poj-2352 && HDU-1541 --Stars(树状数组)
- 树状数组 POJ 2352 HDU 1541 Stars
- poj 2352 && hdu 1541 Stars (树状数组)
- HDU 1541 & POJ 2352 Stars (树状数组)
- poj 2352 && hdu 1541 Stars (树状数组)
- poj 2352 OR hdu 1541 Stars(数据结构:树状数组)
- POJ 2352 & HDU 1541 Stars ( 树状数组)
- poj 2352 && hdu 1541 Stars (树状数组水题)
- poj 2352 && hdu 1541 Stars(树状数组)
- POJ 2352 HDOJ 1541 Stars(树状数组)
- POJ 2352 Stars 树状数组
- POJ 2352 Stars【树状数组】
- (树状数组) poj 2352 Stars
- POJ-2352-Stars-树状数组
- POJ 2352 Stars 树状数组
- POJ 2352 Stars 【树状数组】
- POJ 2352 Stars 树状数组
- 超级牛人在华为工作十年的感悟
- ubuntu12.4 安装最新mongodb数据库
- 二级listview列表
- cms 配置的二级菜单没有出来
- UICollectionView
- POJ 2352 && HDU 1541 Stars (树状数组)
- 日积月累--关于win2008配置mvc4 webapi的解决方案
- 解决code.google.com 无法clone代码
- 9个创建个人品牌的基本步骤
- java 泛型类
- scanf输入会将\n写入缓冲区问题
- 互联网时代:我仅仅是个用户
- LeetCode之旅(8)
- Spring事务管理(编程式事务和申明式事务)