POJ 2352[树状数组]
来源:互联网 发布:剑灵捏脸数据龙男 编辑:程序博客网 时间:2024/06/07 17:36
题目连接
http://acm.hust.edu.cn/vjudge/problem/14610
Sample Input
5
1 1
5 1
7 1
3 3
5 5
Sample Output
1
2
1
1
0
Hint
数据量极大,建议不要用cin读入数据,用scanf读入。
题意
给出所有星星的坐标位置,问每个星星的左下方有多少个星星。
题解
题目数据的给定是按照x,y坐标递增给定的,不需要重新排序。所有当前的星星的坐标y一定是大于先前的所有坐标的y的,只需要找出比当前星星的x坐标小的其他星星的个数,即可以得到该星星左下方[左下,包括左,下,左下],根据题目要求是在一个区间内求和,并且要快速地改变某个数值后修改区间的值,利用该性质,使用树状数组。特别注意,坐标可能是(0,0),把所有x坐标+1后构成树状数组。
代码
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<string> #include<cctype> #include<algorithm> #include<vector>using namespace std;#define maxn 32005int a[maxn];int c[maxn];int ans[maxn];int n=32005;int bits(int k){ return k&-k;}int sum(int k){ int ans=0; while(k>0) { ans+=c[k]; k-=bits(k); } return ans;}int add(int i,int val){ int t=sum(i); ans[t]++; while(i<=n) { c[i]+=val; i+=bits(i); }}int main(){ int m; scanf("%d",&m); memset(a,0,sizeof(a)); memset(c,0,sizeof(c)); memset(ans,0,sizeof(ans)); for(int i=1;i<=m;i++) { int p,q; scanf("%d%d",&p,&q); add(p+1,1); } for(int i=0;i<m;i++)printf("%d\n",ans[i]); return 0;}
0 0
- POJ 2352 树状数组
- poj 2352 树状数组
- POJ 2352 树状数组
- poj 2352(树状数组)
- POJ 2352 树状数组
- POJ 2352 (树状数组)
- poj 2352 树状数组
- poj 2352树状数组
- 【树状数组】poj 2352
- POJ 2352(树状数组)
- poj 2352 树状数组
- poj 2352 树状数组
- poj 2352 树状数组
- poj 2352 树状数组
- POJ 2352 树状数组
- poj 2352 树状数组
- poj 2352 树状数组
- poj 2352 树状数组
- QTableWidget使用注意要点总结
- 二分查找
- java获取当前日期时间代码
- rsync 错误报告
- 蜂巢晶格_Honeycomb Lattice Triangular Lattice初始化邻居信息
- POJ 2352[树状数组]
- 【转】JSP/SERVLET入门教程--Servlet 使用入门
- thinkphp报错 找不到think/log
- Android获取输入法高度——输入法与页面布局无缝切换
- MongoDB小结32 - 聚合管道【$skip】
- apache bench(ab) 压测,模拟post请求
- solr 增量更新
- monkey简介
- HD--2544最短路