HDU 4031

 1 /*
 2 http://acm.hdu.edu.cn/showproblem.php?pid=4031
 3 */
 4 #include<iostream>
 5 #include<cstdio>
 6 #include<cstring>
 7 #include<algorithm>
 8 using namespace std;
 9 const int maxn=100005;
10 int tree[maxn],n,attack[maxn][2],start[maxn],sum[maxn];
11 char cmd[10];
12 int lowbit(int x)
13 {
14     return x&-x;
15 }
16 void update(int x,int d)//节点更新,x=x+d
17 {
18     for(int i=x;i>0;i-=lowbit(i))
19         tree[i]+=d;
20 }
21 int getsum(int x)//求到x的总和
22 {
23     int i,sum=0;
24     for(i=x;i<=n;i+=lowbit(i))
25         sum+=tree[i];
26     return sum;
27 }
28 int main()
29 {
30     int t,Case=0;
31     scanf("%d",&t);
32     while(t--)
33     {
34         int q,co;
35         scanf("%d %d %d",&n,&q,&co);
36         memset(tree,0,sizeof(tree));
37         memset(sum,0,sizeof(sum));
38         memset(start,0,sizeof(start));
39         int c=0;
40         printf("Case %d:\n",++Case);
41         while(q--)
42         {
43             int a,b;
44             scanf("%s %d",cmd,&a);
45             if(cmd[0]=='A')
46             {
47                 scanf("%d",&b);
48                 attack[c][0]=a;
49                 attack[c][1]=b;
50                 c++;//c代表攻击次数
51                 update(b,1);
52                 update(a-1,-1);//给被攻击的区间加1,代表一次攻击
53             }
54             else
55             {
56                 for(int j=start[a];j<c;j++)//为避免多次访问同一点造成的时间浪费,用start数组记录成功防御后到下一次冷却完的时间
57                     if(attack[j][0]<=a&&attack[j][1]>=a)
58                     sum[a]++,start[a]=j+co,j+=co-1;//计算总防御数sum
59                 printf("%d\n",getsum(a)-sum[a]);//被攻击数=总攻击数-总防御数
60             }
61         }
62     }
63     return 0;
64 }

 

posted @ 2017-03-01 21:30  boom~  阅读(144)  评论(0编辑  收藏  举报