hdu 1541 (基本树状数组) Stars
题目http://acm.hdu.edu.cn/showproblem.php?pid=1541
n个星星的坐标,问在某个点左边(横坐标和纵坐标不大于该点)的点的个数有多少个,输出n行,每行有一个数字,代表左下角个点数为i( i 从0开始)的点的个数。
首先将横坐标排序,这样就值需要比较纵坐标,可以采用hash的方法记录纵坐标为 i 的个数,每枚举到一个点hash加一,然后树状数组求纵坐标 不大于该点的
个数。值得注意的是树状数组是由 1 开始的,而题目可以从0开始,会超时,加一就好。
具体看代码。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct point {
int x,y;
};
point a[];
int has[],ha[];
bool cmp(point a,point b){
if (a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
void add(int x,int y){
while (x<=) {
has[x]+=y;
x+=(x&(-x));
}
}
int getsum(int x){
int ans=;
while (x>) {
ans+=has[x];
x-=(x&(-x));
}
return ans;
}
int main()
{
int n,i;
while (~scanf("%d",&n)){
memset(has,,sizeof(has));
memset(ha,,sizeof(ha));
for (i=;i<=n;i++)
scanf("%d %d",&a[i].x,&a[i].y),a[i].x++,a[i].y++;
sort(a+,a+n+,cmp);
//printf("%d\n",getsum(a[1].y));
for (i=;i<=n;i++){
ha[getsum(a[i].y)]++;
add(a[i].y,);
}
for (i=;i<n;i++) printf("%d\n",ha[i]);
}
return ;
}
hdu 1541 (基本树状数组) Stars的更多相关文章
- HDU 1541 STAR(树状数组)
Stars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 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 ...
- hdu 4046 Panda 树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 When I wrote down this letter, you may have been ...
- hdu 5497 Inversion 树状数组 逆序对,单点修改
Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...
- HDU 5493 Queue 树状数组
Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Des ...
- hdu 4031(树状数组+辅助数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Attack Time Limit: 5000/3000 MS (Java/Others) ...
- HDU 4325 Flowers 树状数组+离散化
Flowers Problem Description As is known to all, the blooming time and duration varies between differ ...
- hdu 5877 (dfs+树状数组) Weak Pair
题目:这里 题意: 给出一个n个结点的树和一个数k,每个结点都有一个权值,问有多少对点(u,v)满足u是v的祖先结点且二者的权值之积小于等于k. 从根结点开始dfs,假设搜的的点的权值是v,我们需要的 ...
随机推荐
- java程序员到底该不该了解一点算法(一个简单的递归计算斐波那契数列的案例说明算法对程序的重要性)
为什么说 “算法是程序的灵魂这句话一点也不为过”,递归计算斐波那契数列的第50项是多少? 方案一:只是单纯的使用递归,递归的那个方法被执行了250多亿次,耗时1分钟还要多. 方案二:用一个map去存储 ...
- Oracle 监听器日志解析
Oracle监听器是驻留在Oracle实例所在服务器上的独立进程.作为客户端进程连接实例的重要沟通组件,Oracle监听器扮演着重要的地位.本篇将从监听器日志入手,分析阅读监听器日志和日常监听器常见行 ...
- centos 7.4安装教程
1. 2. 3. 4. 5. 6. 7. 8.
- java 方法引用(method reference)
it -> it != null等价于Objects::nonNull
- html -引入其他html页面
其他页面html为:ip.html 主页面代码 <body> <div id="ip"></div> </body> <scr ...
- mysql 连表查询
现有tablea: tableb: ...
- 【Spider】学习使用XMLFeedSpider
前面写了学习CrawlSpider遇到的问题后,今天学XMLFeedSpider又出现了启动后没爬取到数据,但又不报错的情况 经过排查,发现又是一个粗心大意的错误: class SpiderUserX ...
- jquery mobile两个页面以及源码(登录与注册) 转
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...
- AttributeError: 'dict' object has no attribute 'iteritems'
在python3.6中运行 sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse= ...
- Codeforces Round #541 (Div. 2)
Codeforces Round #541 (Div. 2) http://codeforces.com/contest/1131 A #include<bits/stdc++.h> us ...