zoukankan      html  css  js  c++  java
  • Stars(树状数组+线段树)

    Stars

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 6676    Accepted Submission(s): 2659


    Problem Description
    Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.



    For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.

    You are to write a program that will count the amounts of the stars of each level on a given map.
     
    Input
    The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.
     
    Output
    The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.
     
    Sample Input
    5 1 1 5 1 7 1 3 3 5 5
     
    Sample Output
    1 2 1 1 0
     题解:给出n个点的坐标,定义一个点的等级为这个点的左下方的点的个数,输出等级为0的点的个数,等级为1的点的个数,一直输出到等级为n-1的点的个数
     
    做这个题真是曲折啊,没读清题意就开始写,首先Stars are listed in ascending order of Y coordinate,Stars with equal Y coordinates are listed in ascending order of X coordinate没看到,人家题上给的数据本来就是以y轴升序排列的,自己还傻逼的排序,另外The output should contain N lines这句没看见,自己是以空格输出。。。然后就是思路了,自己竟然想着把点都存在一个数组里面,再用二分去找点的个数。。。。好麻烦的思路,直接ans【sum】++不就妥了么。。。。最后还是超时,怎么办呐,极限数据不对,哪个数据?0呗,如果是0点,update就死循环了。。。
    可能麻烦点,但自己的代码还是对的,以后写代码还是要看清题,找到最简单的思路,写的也好写点;
    简单代码:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<queue>
     6 #include<stack>
     7 #include<vector>
     8 using namespace std;
     9 const int INF=0x3f3f3f3f;
    10 //#define LOCAL
    11 const int MAXM=32005;
    12 const int MAXN=15010;
    13 /*struct Node{
    14     int x,y;
    15 };*/
    16 int tree[MAXM+1],ans[MAXN];//,die[MAXN];
    17 //Node dt[MAXN];
    18 /*int cmp(Node a,Node b){
    19     if(a.y!=b.y){
    20         return a.y<b.y;
    21     }
    22     else return a.x<b.x;
    23 }*/
    24 int lowbit(int x){
    25     return x&(-x);
    26 }
    27 void update(int x){
    28     while(x<=MAXM){
    29         tree[x]++;
    30         x+=lowbit(x);
    31     }
    32 }
    33 int SUM(int x){
    34     int temp=0;
    35     while(x>0){
    36         temp+=tree[x];
    37         x-=lowbit(x);
    38     }
    39     return temp;
    40 }
    41 /*int erfen(int l,int r,int x){
    42     int mid;
    43     while(l<=r){
    44         mid=(l+r)>>1;
    45         if(die[mid]>x)r=mid-1;
    46         else l=mid+1;
    47     }
    48     return l;
    49 }*/
    50 int main(){
    51 /*    #ifdef LOCAL
    52     freopen("data.in","r",stdin);
    53     freopen("data.out","w",stdout);
    54     #endif*/
    55     int N;
    56     int a;
    57     while(~scanf("%d",&N)){
    58         memset(tree,0,sizeof(tree));
    59         memset(ans,0,sizeof(ans));
    60         for(int i=0;i<N;i++){
    61             scanf("%d%*d",&a);
    62             a++;
    63             ans[SUM(a)]++;update(a);
    64         //    scanf("%d%d",&dt[i].x,&dt[i].y);
    65         }
    66         //sort(dt,dt+N,cmp);
    67     /*    for(int i=0;i<N;i++){
    68             die[i]=SUM(dt[i].x);
    69             update(dt[i].x);
    70         }
    71         sort(die,die+N);
    72          for(int i=0;i<N;i++){
    73              int temp=0,t=erfen(0,N-1,i);
    74          //    printf("%d**%d**%d
    ",die[t-1],i,t);
    75                  temp=t;
    76              ans[i]=temp;
    77             // printf("%d
    ",ans[i]);
    78          }*/
    79          for(int i=0;i<N;i++){
    80          /*    if(i){
    81                  printf(" ");
    82              }*/
    83          //    printf("%d",i==0?ans[i]:ans[i]-ans[i-1]);
    84          printf("%d
    ",ans[i]);
    85          }
    86          
    87     }
    88     return 0;
    89 }

    刚开始写的也ac了:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<queue>
     6 #include<stack>
     7 #include<vector>
     8 using namespace std;
     9 const int INF=0x3f3f3f3f;
    10 //#define LOCAL
    11 const int MAXM=32010;
    12 const int MAXN=15010;
    13 struct Node{
    14     int x,y;
    15 };
    16 int tree[MAXM+1],die[MAXN],ans[MAXN];
    17 Node dt[MAXN];
    18 /*int cmp(Node a,Node b){
    19     if(a.y!=b.y){
    20         return a.y<b.y;
    21     }
    22     else return a.x<b.x;
    23 }*/
    24 int lowbit(int x){
    25     return x&(-x);
    26 }
    27 void update(int x){
    28     while(x<=MAXM){
    29         tree[x]++;
    30         x+=lowbit(x);
    31     }
    32 }
    33 int SUM(int x){
    34     int temp=0;
    35     while(x){
    36         temp+=tree[x];
    37         x-=lowbit(x);
    38     }
    39     return temp;
    40 }
    41 int main(){
    42     #ifdef LOCAL
    43     freopen("data.in","r",stdin);
    44     freopen("data.out","w",stdout);
    45     #endif
    46     int N;
    47     while(~scanf("%d",&N)){
    48         for(int i=0;i<N;i++){
    49             scanf("%d%d",&dt[i].x,&dt[i].y);
    50             dt[i].x++;
    51         }
    52         //sort(dt,dt+N,cmp);
    53         memset(tree,0,sizeof(tree));
    54         for(int i=0;i<N;i++){
    55             die[i]=SUM(dt[i].x);
    56             update(dt[i].x);
    57         }
    58         sort(die,die+N);
    59          for(int i=0,j=0;i<N;i++){
    60              int temp=0;
    61              while(die[j]==i){
    62                  j++;temp++;
    63              }
    64              ans[i]=temp;
    65          }
    66          for(int i=0;i<N;i++){
    67              //if(i)printf(" ");
    68              printf("%d
    ",ans[i]);
    69          }
    70         // puts("");
    71     }
    72     return 0;
    73 }

    刚开始发现超时改成二分找了,更快点的最后也ac了:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<queue>
     6 #include<stack>
     7 #include<vector>
     8 using namespace std;
     9 const int INF=0x3f3f3f3f;
    10 //#define LOCAL
    11 const int MAXM=32010;
    12 const int MAXN=15010;
    13 struct Node{
    14     int x,y;
    15 };
    16 int tree[MAXM+1],die[MAXN],ans[MAXN];
    17 Node dt[MAXN];
    18 /*int cmp(Node a,Node b){
    19     if(a.y!=b.y){
    20         return a.y<b.y;
    21     }
    22     else return a.x<b.x;
    23 }*/
    24 int lowbit(int x){
    25     return x&(-x);
    26 }
    27 void update(int x){
    28     while(x<=MAXM){
    29         tree[x]++;
    30         x+=lowbit(x);
    31     }
    32 }
    33 int SUM(int x){
    34     int temp=0;
    35     while(x){
    36         temp+=tree[x];
    37         x-=lowbit(x);
    38     }
    39     return temp;
    40 }
    41 int erfen(int l,int r,int x){
    42     int mid;
    43     while(l<=r){
    44         mid=(l+r)>>1;
    45         if(die[mid]>x)r=mid-1;
    46         else l=mid+1;
    47     }
    48     return l;
    49 }
    50 int main(){
    51     #ifdef LOCAL
    52     freopen("data.in","r",stdin);
    53     freopen("data.out","w",stdout);
    54     #endif
    55     int N;
    56     while(~scanf("%d",&N)){
    57         for(int i=0;i<N;i++){
    58             scanf("%d%d",&dt[i].x,&dt[i].y);
    59             dt[i].x++;
    60         }
    61         //sort(dt,dt+N,cmp);
    62         memset(tree,0,sizeof(tree));
    63         for(int i=0;i<N;i++){
    64             die[i]=SUM(dt[i].x);
    65             update(dt[i].x);
    66         }
    67         sort(die,die+N);
    68         for(int i=0;i<N;i++){
    69              int temp=0,t=erfen(0,N-1,i);
    70          //    printf("%d**%d**%d
    ",die[t-1],i,t);
    71                  temp=t;
    72              ans[i]=temp;
    73             // printf("%d
    ",ans[i]);
    74          }
    75          for(int i=0;i<N;i++){
    76              //if(i)printf(" ");
    77            printf("%d
    ",i==0?ans[i]:ans[i]-ans[i-1]);
    78          }
    79         // puts("");
    80     }
    81     return 0;
    82 }

     线段树来一发:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<queue>
     6 #include<stack>
     7 #include<vector>
     8 using namespace std;
     9 const int INF=0x3f3f3f3f;
    10 const int MAXN=32010;
    11 #define L tree[root].l
    12 #define R tree[root].r
    13 #define S tree[root].sum
    14 #define lson root<<1,l,mid
    15 #define rson root<<1|1,mid+1,r
    16 
    17 struct Node{
    18     int l,r,sum;
    19 };
    20 Node tree[MAXN<<2];
    21 int res[MAXN>>1];
    22 int ans; 
    23 void build(int root,int l,int r){
    24     L=l;R=r;
    25     S=0;
    26     if(l==r)return;
    27     int mid=(l+r)>>1;
    28     build(lson);
    29     build(rson);
    30 }
    31 void update(int root,int x){
    32     if(L==x&&R==x){
    33         S++;
    34         return;
    35     }
    36     int mid=(L+R)>>1;
    37     if(mid>=x)update(root<<1,x);
    38     else update(root<<1|1,x);
    39     S=tree[root<<1].sum+tree[root<<1|1].sum;
    40 }
    41 void query(int root,int x){
    42     if(L>=1&&R<=x){
    43         ans+=S;
    44         return;
    45     }
    46     int mid=(L+R)>>1;
    47     if(mid>=1)query(root<<1,x);
    48     if(mid<x)query(root<<1|1,x);
    49 }
    50 int main(){
    51     int N,a;
    52     while(~scanf("%d",&N)){
    53         build(1,1,MAXN-1);
    54         memset(res,0,sizeof(res));
    55         for(int i=0;i<N;i++){
    56         scanf("%d%*d",&a);
    57         a++;
    58         ans=0;
    59         query(1,a);
    60         res[ans]++;
    61         update(1,a);
    62         }
    63         for(int i=0;i<N;i++)printf("%d
    ",res[i]);
    64     }
    65     return 0;
    66 }
  • 相关阅读:
    BZOJ1299 [LLH邀请赛]巧克力棒
    BZOJ1046 [HAOI2007]上升序列
    BZOJ1798 [Ahoi2009]Seq 维护序列seq
    BZOJ2045 双亲数
    BZOJ2301 [HAOI2011]Problem b
    BZOJ1021 [SHOI2008]Debt 循环的债务
    BZOJ2618 [Cqoi2006]凸多边形
    BZOJ1069 [SCOI2007]最大土地面积
    BZOJ1051 [HAOI2006]受欢迎的牛
    2017年09月23日普级组 环
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4900373.html
Copyright © 2011-2022 走看看