POJ-2352 Stars【树状数组】
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
树状数组的线段树都可以做,每输入一个x,统计小于x的数量,即前缀和。
Code:
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 const int maxn = 32000 + 5; 7 int C[maxn], cnt[maxn], n; 8 9 int lowbit(int x) {return x & -x;} 10 11 int sum(int x) { 12 int ret = 0; 13 while (x) { 14 ret += C[x]; x -= lowbit(x); 15 } 16 return ret; 17 } 18 19 void add(int x, int d) { 20 while (x <= maxn) { 21 C[x] += d; x += lowbit(x); 22 } 23 } 24 25 int main() { 26 int x, y; 27 while(~scanf("%d", &n)) { 28 memset(C, 0, sizeof(C)); 29 memset(cnt, 0, sizeof(cnt)); 30 for (int i = 0; i < n; ++i) { 31 scanf("%d%d", &x, &y); 32 ++x; 33 ++cnt[sum(x)]; 34 add(x, 1); 35 } 36 for (int i = 0; i < n; ++i) 37 printf("%d\n", cnt[i]); 38 } 39 40 return 0; 41 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 为DeepSeek添加本地知识库
· .NET程序员AI开发基座:Microsoft.Extensions.AI
· 精选4款基于.NET开源、功能强大的通讯调试工具
· 数据不出内网:基于Ollama+OneAPI构建企业专属DeepSeek智能中台
· 大模型工具KTransformer的安装