HDU4031(树状数组)详解

举报
知识浅谈 发表于 2022/06/29 01:18:18 2022/06/29
【摘要】 #include<cstdio> //类是差分的思想区间更新 #include<cstring> //思路:用每个点总的攻击次数-无效次数 #include&lt...
#include<cstdio>     //类是差分的思想区间更新 
#include<cstring>    //思路:用每个点总的攻击次数-无效次数 
#include<algorithm>
using namespace std;
int n,q,t;
int s[20005];
int l[20005],r[20005],last[20005],useless[20005];
int lowbit(int x){
    return x&(-x);
}
void insert(int i,int num)  
{
    while(i<=n)
    {
        s[i]+=num;
        i+=lowbit(i);
    }
}
int sum(int x)
{
    int ans=0;
    while(x)
    {
        ans+=s[x];
        x-=lowbit(x);
    }
    return ans;
}
int main()
{
    int a,b,c,cnt=1;
    char s1[20];
    scanf("%d",&c);
    while(c--)
    {
        printf("Case %d:\n",cnt++);
        int add=0;
        memset(l,0,sizeof(l));  //记录每次攻击的左坐标 
        memset(r,0,sizeof(r));  //记录每次攻击的右坐标 
        memset(s,0,sizeof(s));  //树状数组 
        memset(last,0,sizeof(last)); //记录上次攻击的侯能再次攻击无效的的时间 
        memset(useless,0,sizeof(useless));//记录每个点的无效攻击次数 
        scanf("%d %d %d",&n,&q,&t);
        for(int i=1;i<=q;i++)
        {
            scanf("%s",s1);
            if(s1[0]=='A')
            {
                scanf("%d %d",&a,&b);
                insert(a,1);
                insert(b+1,-1);
                l[add]=a,r[add]=b;
                add++;
            }
            else if(s1[0]=='Q')
            {
                scanf("%d",&a);
                for(int j=last[a];j<add;j++)
                {
                    if(l[j]<=a&&a<=r[j])
                    {
                        useless[a]++;     //记录无用的攻击次数 
                        last[a]=j+t;    //记录的试有防御的上次的位置 
                        j=j+t-1;      //移动位置到回复后的位置 
                    }//这个坑点试因为这里不加t而加t-1 因为for循环里边有个j++ 
                }
                printf("%d\n",sum(a)-useless[a]); //所有次数减去无用次数 
            }
        }
    }
    return 0; 
}
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

文章来源: englishcode.blog.csdn.net,作者:知识浅谈,版权归原作者所有,如需转载,请联系作者。

原文链接:englishcode.blog.csdn.net/article/details/78585432

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。