码迷,mamicode.com
首页 > 编程语言 > 详细

[POJ2352] Stars(树状数组)

时间:2017-05-02 19:52:38      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:std   log   for   ++   tchar   open   owb   bool   onclick   

传送门

 

先按照下标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 }
View Code

 

[POJ2352] Stars(树状数组)

标签:std   log   for   ++   tchar   open   owb   bool   onclick   

原文地址:http://www.cnblogs.com/zhenghaotian/p/6797614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!