(樹狀數組) poj 2352 Stars

來源:互聯網
上載者:User
Stars
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14588   Accepted: 6301

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

51 15 17 13 35 5

Sample Output

12110

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed./*<br />題目大意:略<br />題目解答:樹狀數組入門,由於輸入按照Y升序,統計左下方的星星的時候,順序計算就可以了.(Xk<Xi && Yk<Yi)<br />Source Code</p><p>Problem: 2352 User: wawadimu<br />Memory: 356K Time: 125MS<br />Language: C++ Result: Accepted </p><p>Source Code<br />*/<br />#include<iostream><br />using namespace std;</p><p>#define maxx 32010<br />#define maxn 15010</p><p>int c[maxx],ans[maxn];<br />int low_bit(int x)//返回x二進位最右邊1<br />{<br />return x&(x^(x-1));<br />}<br />int get_sum(int i)//sum(a[1]+a[2]+a[3]+...a[i])<br />{<br />int sum=0;<br />while(i>0)<br />{<br />sum+=c[i];//C[i]=C[i-2^k+1]+...+C[i]<br />i-=low_bit(i);<br />}<br />return sum;<br />}<br />void modify(int i,int add)//更新包含C[i]的所有C[k]<br />{<br />while(i<=maxx)<br />{<br />c[i]+=add;<br />i+=low_bit(i);<br />}<br />}</p><p>int main()<br />{<br />//freopen("2352.txt","r",stdin);<br />int i,j;<br />int x,y;<br />int n;<br />while(scanf("%d",&n)!=EOF)<br />{<br />//cout<<n<<endl;<br />for(i=0;i<n;i++)<br />ans[i]=0;<br />for(i=0;i<=maxx;i++)<br />c[i]=0;<br />for(i=0;i<n;i++)<br />{<br />scanf("%d%d",&x,&y);<br />//printf("%d %d ",x,y);<br />x++;<br />int level=get_sum(x);<br />ans[level]++;<br />modify(x,1);<br />}</p><p>for(i=0;i<n;i++)//列印<br />printf("%d/n",ans[i]);<br />}<br />return 0;<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.