zoukankan      html  css  js  c++  java
  • 【HDU1514】Stars(树状数组)

    绝对大坑。千万记住树状数组0好下标位置是虚拟节点。详见大白书P195。其实肉眼看也能得出,在add(有的也叫update)的点修改操作中如果传入0就会死循环。最后TLE。所以下标+1解决问题。上代码!

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdlib>
     4 #include <cstdio>
     5 #include <algorithm>
     6 #include <numeric>
     7 #include <cctype>
     8 #include <cmath>
     9 using namespace std;
    10 
    11 const int V = 32000 + 10;
    12 const int M = 15000 + 10;
    13 int C[V], res[M];
    14 
    15 int lowbit (int x) {
    16     return x & -x;
    17 }
    18 
    19 int sum (int x) {
    20     int ret = 0;
    21     while (x > 0) {
    22         ret += C[x]; x -= lowbit(x);
    23     }
    24     return ret;
    25 }
    26 
    27 void add (int x, int d) {
    28     while (x <= V) {
    29         C[x] += d; x += lowbit(x);
    30     }
    31 }
    32 
    33 int main () {
    34     int n, x, y;
    35     while (~scanf("%d", &n)) {
    36         memset(C, 0, sizeof(C));
    37         memset(res, 0, sizeof(res));
    38 
    39         for (int i = 0 ; i < n; ++ i) {
    40             scanf ("%d %d", &x, &y);
    41             res[sum(x + 1)] ++;
    42             add(x + 1, 1);
    43         }
    44         for (int i = 0 ; i < n; ++ i) {
    45             printf ("%d
    ", res[i]);
    46         }
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    闭包
    List(数组)里面常用的属性和方法
    drat笔记
    使用dd命令克隆整个系统
    Linux dd命令
    Linux 添加PPA源
    Linux 开机自动挂载windows分区
    Linux 格式化磁盘命令mkfs
    Linux 下面adb命令的使用
    linux下面which whereis find locate的使用
  • 原文地址:https://www.cnblogs.com/Destiny-Gem/p/3871287.html
Copyright © 2011-2022 走看看