[POJ2352] Stars(树状数组)

传送门

先按照下标x排序,然后依次把y加入树状数组,边加入边统计即可。

注意下标re从零开始,需+1s

——代码

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <string>
 5 # include <cmath>
 6 # include <vector>
 7 # include <map>
 8 # include <queue>
 9 # include <cstdlib>
10 # include <algorithm>
11 # define MAXN 32002
12 using namespace std;
13
14 inline int get_num() {
15     int k = 0, f = 1;
16     char c = getchar();
17     for(; !isdigit(c); c = getchar()) if(c == ‘-‘) f = -1;
18     for(; isdigit(c); c = getchar()) k = k * 10 + c - ‘0‘;
19     return k * f;
20 }
21
22 int n;
23 int c[MAXN], ans[MAXN];
24 struct node
25 {
26     int a, b;
27 }p[MAXN];
28
29 inline bool cmp(node x, node y)
30 {
31     return x.a < y.a || (x.a == y.a && x.b < y.b);
32 }
33
34 inline int lowbit(int x)
35 {
36     return x & -x;
37 }
38
39 inline int query(int x)
40 {
41     int ans = 0;
42     for(; x; x -= lowbit(x)) ans += c[x];
43     return ans;
44 }
45
46 inline void add(int x)
47 {
48     for(; x <= 32001; x += lowbit(x)) c[x] += 1;
49 }
50
51 int main()
52 {
53     int i, t;
54     n = get_num();
55     for(i = 1; i <= n; i++)
56     {
57         p[i].a = get_num() + 1;
58         p[i].b = get_num() + 1;
59     }
60     sort(p + 1, p + n + 1, cmp);
61     for(i = 1; i <= n; i++)
62     {
63         t = query(p[i].b);
64         add(p[i].b);
65         ans[t]++;
66     }
67     for(i = 0; i < n; i++) printf("%d\n", ans[i]);
68     return 0;
69 }

时间: 2024-10-23 03:46:50

[POJ2352] Stars(树状数组)的相关文章

POJ2352 Stars 树状数组

POJ2352 非常裸的树状数组的题. 注意数组下标不能从0开始 因为lowbit(0)==0 所以 所有横坐标统一加1 数组要开的够大 就酱 #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<cmath> #include<vector> using namespace std; const int maxn=15000,

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. As

hdu 1541 Stars 树状数组水题

Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5210    Accepted Submission(s): 2053 Problem Description Astronomers often examine star maps where stars are represented by points on a pla

POJ2352(树状数组)

Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33786   Accepted: 14741 题目链接:http://poj.org/problem?id=2352 Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesia

POJ 2481 Cows &amp;&amp; POJ 2352 Stars(树状数组妙用)

题目链接:POJ 2481 Cows POJ 2352 Stars 发现这两个题目都跟求逆序数有着异曲同工之妙,通过向树状数组中插入点的位置,赋值为1,或者++,然后通过求和来判断比当前 点 "小" 的有多少点. Cows需要自己排序, Stars题目已经给排好序. POJ 2352 Stars 题目大意为在二维坐标上给出一些星星的坐标,求某一个星星左方,下方,左下方的星星个数.题目已经把星星按照Y坐标从小到大,X从小到大排序.因此,在每次对一个星星进行统计时,之前出现过的星星,只要X

Stars(树状数组单点更新)

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 wa

POJ 2352 Stars(树状数组 or 线段树)

链接: http://poj.org/problem?id=2352 题目大意: 在坐标上有n个星星,如果某个星星坐标为(x, y), 它的左下位置为:(x0,y0),x0<=x 且y0<=y.如果左下位置有a个星星,就表示这个星星属于level x 按照y递增,如果y相同则x递增的顺序给出n个星星,求出所有level水平的数量. 思路: 由于输入的顺序,对于第i颗星星,它的等级是之前输入的星星中,横坐标x小于等于i星横坐标的那些星星的总数量(前面的y一定比后面的y小). 所以是查询+更新操作

POJ 2352 &amp;&amp; HDU 1541 Stars (树状数组)

一开始想,总感觉是DP,可是最后什么都没想到.还暴力的交了一发. 然后开始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~~~~~~ 树状数组不懂的去看刘汝佳的大白书,那个图画得很清楚. 题目大意:星星的坐标以y递增的顺序给出,这些点的左下方的点数代表这个点的级数,问0~N-1的级数有多少个?其实y根本木有用. 题目链接:http://poj.org/problem?id=2352 http://acm.hdu.edu

POJ 2352 &amp;amp;&amp;amp; HDU 1541 Stars (树状数组)

一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~~~~~~ 树状数组不懂的去看刘汝佳的大白书,那个图画得非常清楚. 题目大意:星星的坐标以y递增的顺序给出,这些点的左下方的点数代表这个点的级数,问0~N-1的级数有多少个?事实上y根本木实用. 题目链接:http://poj.org/problem?id=2352 http://acm.hdu.e

poj2352 starts 树状数组

有n个星星,按照y坐标的升序给出n个星星的坐标, 对于每一个星星,其level为在其左下方(包括正左,正下)的星星个数,输出n行,第i行代表等级为i个星星的星星的个数. 树状数组的插点问段 思路: a[i] 存储星星的x坐标, b[i] 表示0到i中x,y坐标小于i的个数,即i的等级 则对于每个b[i], 做一次扫描 ans[i] 表示等级为i的个数 则ans[b[i]]++; 明显,这样肯定tle. 所以要采用树状数组 . a[i] 表示目前的星星中x坐标为i的个数 c[i] 为a[i]的树状