hdu 4031 Attack

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=4031

区间更新,询问点

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN =20010;

int n;
int c[MAXN];

struct Range_Attack
{
    int l;
    int r;
    Range_Attack()
    {
        l = r = 0;
    }
};

Range_Attack Attack[MAXN];

int LowBit(int x)
{
    return x & (-x);
}

void UFSet_BIT(int pos, int data)
{
    while(pos <= n)
    {
        c[pos] += data;
        pos += LowBit(pos);
    }
}

int Query_BIT(int pos, int sum)
{
    while(pos > 0)
    {
        sum += c[pos];
        pos -= LowBit(pos);
    }
    return sum;
}

int main()
{
    int T, kcase = 1, i, pos;
    char str[6];
    int times;//攻击的次数
    int coolTime, oper;//盾牌冷却的时间,以及操作数
    int used_Time[MAXN], protected_Times[MAXN];//used_Time[pos]表示pos这个位置已经花费了多少时间, protected_Times[pos]表示pos这个位置已经成功防御了多少次
    scanf("%d", &T);
    while(T--)
    {
        memset(c, 0, sizeof(c));
        memset(used_Time, 0, sizeof(used_Time));
        memset(protected_Times, 0, sizeof(protected_Times));
        printf("Case %d:\n", kcase++);
        scanf("%d %d %d", &n, &oper, &coolTime);
        times = 0;
        while(oper--)
        {
            scanf("%s", str);
            if(str[0] == 'A')
            {
                scanf("%d %d", &Attack[times].l, &Attack[times].r);
                UFSet_BIT(Attack[times].l, 1);
                UFSet_BIT(Attack[times++].r+1, -1);
            }
            else
            {
                scanf("%d", &pos);
                for(i = used_Time[pos]; i < times; ++i)
                {
                    if(Attack[i].l <= pos && pos <= Attack[i].r)//第i次攻击范围内必须要包含pos
                    {
                        protected_Times[pos]++;
                        used_Time[pos] = i + coolTime;
                        i = i+coolTime - 1;
                    }
                }
                printf("%d\n", Query_BIT(pos, 0)-protected_Times[pos]);
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值