码迷,mamicode.com
首页 > 其他好文 > 详细

POJ - 2352 - Stars

时间:2017-08-20 00:43:39      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:str   add   pen   https   ace   bit   namespace   http   循环   

题目链接:POJ - 2352

题目大意:

给你N个星星的坐标,问每个星星的左下角有几颗星星。

注意输入坐标y是按递增输入的。

题目分析:

算是树状数组的一个基础题目吧,有些题目也是以这道题为基础的。

直接运用数组数组即可,具体看代码。

不过注意由于x坐标有可能是0,所以要对所有的X坐标加一(不要忘了区间上界也要增大一个),

防止add()函数出现死循环超时。

给出代码:

技术分享
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 using namespace std;
 6 const int N=32000;
 7 int vis[32000+10];
 8 int ans[32000];
 9 int bit(int x)
10 {
11     return (x&(-x));
12 }
13 int sum(int x)
14 {
15     int s=0;
16     while(x>0)
17     {
18         s+=vis[x];
19         x-=bit(x);
20     }
21     return s;
22 }
23 int add(int x,int pos)
24 {
25     while(pos<=N+1)
26     {
27         vis[pos]+=x;
28         pos+=bit(pos);
29     }
30 }
31 int main()
32 {
33    int n;
34    scanf("%d",&n);
35    memset(vis,0,sizeof(vis));
36    memset(ans,0,sizeof(ans));
37    for(int i=0;i<n;i++)
38    {
39        int x,y;
40        scanf("%d%d",&x,&y);
41        //printf("%d\n",sum(x+1));
42        ans[sum(x+1)]++;
43        add(1,x+1);
44    }
45    for(int i=0;i<n;i++)
46     printf("%d\n",ans[i]);
47    return 0;
48 }
View Code

 

POJ - 2352 - Stars

标签:str   add   pen   https   ace   bit   namespace   http   循环   

原文地址:http://www.cnblogs.com/DLKKILL/p/7398353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!