• [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
    ", ans[i]);
    68     return 0;
    69 }
    View Code
  • 相关阅读:
    request和request.form和request.querystring的区别
    PL/SQL Developer连接64位Oracle
    C# Winform控件对透明图片重叠时导致图片不透明的解决方法
    C++11多线程编程-两个进程轮流打印1~100
    使用 C++11 并发编程入门
    STL vector动态扩容
    GDB入门教程
    统计整数中1的个数
    gulp的使用
    nvm安装教程
  • 原文地址:https://www.cnblogs.com/zhenghaotian/p/6797614.html
Copyright © 2020-2023  润新知