hdoj 1166 敌兵布阵(树状数组)

本文详细解析了HDU 1166动态连续和查询问题,介绍了使用数组和线段树解决该问题的思路与方法,并提供了完整的代码实现。通过本文,读者可以深入理解动态连续和查询问题的解决策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166

思路分析:该问题为动态连续和查询问题,使用数组数组可以解决;也可使用线段树解决该问题;

 

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int MAX_N = 50000 + 10;
int c[MAX_N];

inline int Lowbit(int x) { return x & -x; }
inline int Sum(int x)
{
    int ret = 0;
    while (x) {
        ret += c[x];
        x -= Lowbit(x);
    }
    return ret;
}

inline void Add(int x, int d, int n)
{
    while (x <= n) {
        c[x] += d;
        x += Lowbit(x);
    }
}

int main()
{
    int a = 0, b = 0, ans = 0, temp = 0;
    int test_case = 0, case_id = 0, n = 0;
    char command[10];

    scanf("%d", &test_case);
    while (test_case--) {
        scanf("%d", &n);
        memset(c, 0, sizeof(c));
        for (int i = 1; i <= n; ++ i) {
            scanf("%d", &temp);
            Add(i, temp, n);
        }
        printf("Case %d:\n", ++case_id);
        while(scanf("%s", command) != EOF && command[0] != 'E') {
            scanf("%d %d", &a, &b);
            if (command[0] == 'Q') {
                ans = Sum(b) - Sum(a - 1);
                printf("%d\n", ans);
            } else if (command[0] == 'A') {
                Add(a, b, n);
            } else {
                Add(a, -b, n);
            }
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/tallisHe/p/4657499.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值