POJ2352 Stars 【树状数组】or【线段树】
来源:互联网 发布:君理资本 知乎 编辑:程序博客网 时间:2024/05/21 21:33
Stars
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 31172 Accepted: 13595
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.
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.
树状数组节点保存区间和,总区间为[0, maxn)。
树状数组:
//#define DEBUG#include <stdio.h>#define maxn 32002int tree[maxn], level[maxn], n;int lowBit(int x){ return x & (-x); }void update(int pos){while(pos < maxn){++tree[pos];pos += lowBit(pos);}}int getSum(int pos){int sum = 0;while(pos > 0){sum += tree[pos];pos -= lowBit(pos);}return sum;}int main(){#ifdef DEBUGfreopen("stdin.txt", "r", stdin);freopen("stdout.txt", "w", stdout);#endifint x, y, i;scanf("%d", &n);for(i = 0; i < n; ++i){scanf("%d%d", &x, &y);++level[getSum(++x)];update(x);}for(i = 0; i < n; ++i)printf("%d\n", level[i]);return 0;}
线段树:
#include <stdio.h>#define maxn 32002#define lson l, mid, rt << 1#define rson mid + 1, r, rt << 1 | 1int tree[maxn << 2], level[maxn];void pushUp(int rt){tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];}void update(int pos, int l, int r, int rt){if(l == r){++tree[rt]; return;}int mid = (l + r) >> 1;if(pos <= mid) update(pos, lson);else update(pos, rson);pushUp(rt);}int query(int left, int right, int l, int r, int rt){if(l == left && r == right) return tree[rt];int mid = (l + r) >> 1;if(right <= mid) return query(left, right, lson);else if(left > mid) return query(left, right, rson);else return query(left, mid, lson) + query(mid + 1, right, rson);}int main(){int n, x, y, i;scanf("%d", &n);for(i = 0; i < n; ++i){scanf("%d%d", &x, &y);++level[query(0, x, 0, maxn - 1, 1)];update(x, 0, maxn - 1, 1);}for(i = 0; i < n; ++i)printf("%d\n", level[i]);return 0;}
0 0
- POJ2352 Stars 【树状数组】or【线段树】
- poj2352--Stars--线段树&树状数组
- POJ2352 Stars(线段树 & 树状数组)
- POJ2352 Stars(树状数组 or SplayTree)
- 【树状数组】 poj2352 Stars
- POJ2352 stars(树状数组)
- poj2352 - Stars(树状数组)
- poj2352 Stars 树状数组
- POJ2352:Stars(树状数组)
- POJ2352 Stars(树状数组)
- POJ2352 Stars 树状数组
- POJ2352 Stars[树状数组]
- 【poj2352】【树状数组】Stars
- poj2352 Stars (树状数组)
- POJ2352 Stars 树状数组
- POJ2352 Stars 树状数组
- poj2352 Stars(树状数组)
- 【树状数组】poj2352 stars
- 两道中等贪心题,poj 3040, poj 3190
- opengl 中的光照与颜色 转载
- 面试的知识(收集)
- 程序员的八种级别
- Java中Date及Timestamp时间相关内容(基本上看这一份就可以了)
- POJ2352 Stars 【树状数组】or【线段树】
- Android 百度地图 SDK v3.0.0 (三) 添加覆盖物Marker与InfoWindow的使用
- 数学 之 Codeforces 354A - Vasya and Robot
- JAVA学习 之 浅析J2EE
- http://dongxi.douban.com/article/1352234/
- Easyui tabs update 内容不显示
- ubuntu下安装rpm文件
- android 中的颜色设置方法和颜色的rgb值
- openwrt 编译glib2.28.6