zoukankan      html  css  js  c++  java
  • hdu 1166 赤裸裸的树状数组

    题目没啥说的,赤裸裸的树状数组

    /*
    * hdu1166/win.cpp
    * Created on: 2011-9-6
    * Author : ben
    */
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    #include
    <cmath>
    #include
    <algorithm>
    using namespace std;

    const int MAXN = 50100;
    int N, M;
    int array[MAXN];

    inline
    int lowbit(int x) {
    return x & (x ^ (x - 1));
    }

    int sum(int n) {
    int ret = 0;
    for(int i = n; i > 0; i -= lowbit(i)) {
    ret
    += array[i];
    }
    return ret;
    }

    void update(int index, int value) {
    for (int i = index; i <= N; i += lowbit(i)) {
    array[i]
    += value;
    }
    }

    void work();
    int main() {
    #ifndef ONLINE_JUDGE
    freopen(
    "data.in", "r", stdin);
    #endif
    work();
    return 0;
    }

    void work() {
    char op[20];
    int a, b, T, temp;
    scanf(
    "%d", &T);
    for (int t = 1; t <= T; t++) {
    printf(
    "Case %d:\n", t);
    scanf(
    "%d", &N);
    memset(array,
    0, sizeof(array));
    for (int i = 1; i <= N; i++) {
    scanf(
    "%d", &temp);
    update(i, temp);
    }
    while (scanf("%s", op) == 1) {
    if (strcmp(op, "End") == 0) {
    break;
    }
    scanf(
    "%d%d", &a, &b);
    if (strcmp(op, "Query") == 0) {
    printf(
    "%d\n", sum(b) - sum(a - 1));
    }
    else if (strcmp(op, "Add") == 0) {
    update(a, b);
    }
    else {
    update(a,
    -b);
    }
    }
    }
    }
  • 相关阅读:
    Problem C: 爬楼梯
    Problem E: 倒水(Water)
    Problem H: tmk买礼物
    HDU 1078 FatMouse and Cheese
    POJ 3186 Treats for the Cows
    POJ 1661 Help Jimmy
    POJ 1458 Common Subsequence
    2018-软工机试-D-定西
    2018-软工机试-F-庙会
    2018-软工机试-C-和你在一起
  • 原文地址:https://www.cnblogs.com/moonbay/p/2168484.html
Copyright © 2011-2022 走看看