Hdu4031【树状数组】

来源:互联网 发布:smt软件 编辑:程序博客网 时间:2024/05/12 11:47
/*AttackTime Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 1947    Accepted Submission(s): 566Problem DescriptionToday is the 10th Annual of “September 11 attacks”, the Al Qaeda is about to attack American again. However, American is protected by a high wall this time, which can be treating as a segment with length N. Al Qaeda has a super weapon, every second it can attack a continuous range of the wall. American deployed N energy shield. Each one defends one unit length of the wall. However, after the shield defends one attack, it needs t seconds to cool down. If the shield defends an attack at kth second, it can’t defend any attack between (k+1)th second and (k+t-1)th second, inclusive. The shield will defend automatically when it is under attack if it is ready.During the war, it is very important to understand the situation of both self and the enemy. So the commanders of American want to know how much time some part of the wall is successfully attacked. Successfully attacked means that the attack is not defended by the shield.InputThe beginning of the data is an integer T (T ≤ 20), the number of test case.The first line of each test case is three integers, N, Q, t, the length of the wall, the number of attacks and queries, and the time each shield needs to cool down.The next Q lines each describe one attack or one query. It may be one of the following formats1. Attack si ti  Al Qaeda attack the wall from si to ti, inclusive. 1 ≤ si ≤ ti ≤ N2. Query p  How many times the pth unit have been successfully attacked. 1 ≤ p ≤ NThe kth attack happened at the kth second. Queries don’t take time.1 ≤ N, Q ≤ 200001 ≤ t ≤ 50 OutputFor the ith case, output one line “Case i: ” at first. Then for each query, output one line containing one integer, the number of time the pth unit was successfully attacked when asked. Sample Input23 7 2Attack 1 2Query 2Attack 2 3 Query 2Attack 1 3Query 1Query 39 7 3Attack 5 5Attack 4 6Attack 3 7Attack 2 8Attack 1 9Query 5Query 3 Sample OutputCase 1:0101Case 2:32 SourceThe 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest */ #include<stdio.h>#include<string.h>struct attack{int l, r;}att[20010];//记录每次攻击的区间 int c[20010],   deft[20010], defn[20010];/*c记录某个点被攻击的总次数  deft记录每个点可抵御攻击时的时间 defn记录每个点成功抵御攻击的次数*/ int sum(int i)//求和 {int sum = 0;while( i > 0){sum += c[i];i -= i&(-i);} return sum;}void update(int i, int e, int n)//更新攻击次数 {while(i <= n){c[i] += e;i += i &(-i);}}int main(){int T, num = 1;scanf("%d", &T);while(T--){printf("Case %d:\n", num++);int N, Q, t;char s[10];scanf("%d%d%d", &N, &Q, &t);getchar();memset(c, 0, sizeof(c));//初始化攻击次数 memset(defn, 0, sizeof(defn));//初始化每个点抵御攻击成功的次数 for(int i = 1; i <= N; i++)//初始化每个点可以抵御攻击的时间 deft[i] = 1;int time = 1;//标记第几次攻击 while(Q--){scanf("%s", s);if( strcmp(s, "Attack") == 0){scanf("%d%d", &att[time].l, &att[time].r);//记录攻击次数 update(att[time].l, 1, N);update(att[time].r+1, -1, N);time++; }else{int pos; scanf("%d", &pos);//查询某个点攻击成功的次数 for(int i = deft[pos]; i < time; i++)//计算该点抵御成功的次数 ,从该点可以抵御攻击的时刻开始遍历 {if(att[i].l <= pos && pos <= att[i].r)//如果在该点可以抵御攻击的时候受到了攻击,则将该点抵御成功的次数+1 {defn[pos]++;deft[pos] = i + t;i += (t-1);}}printf("%d\n", sum(pos) - defn[pos]);//输出答案=攻击总次数 - 抵御成功的次数 }getchar();}}return 0;}

题意 :Al Qaeda要攻击American,美国有个城墙用来抵御攻击编号1~N,AL有个武器每秒可以攻击一段连续的城墙,美国在每个城墙里都有个防护装置,每t秒可以抵御一次攻击,现给出指令攻击城墙或者查询某个城墙成功被攻击的次数。

思路 :这题比较麻烦的就是处理城墙t秒的冷却时间,为了节省时间及空间,开两个数组统计城墙可以抵御攻击的时刻,和抵御成功的次数,并且用树状数组统计攻击的总次数,每次查询的时候就是从该点每个可以抵御攻击时刻开始遍历,直到当前攻击时刻,如果在每个可以抵御攻击的时刻,攻击的区间包含了当前查询点,则将抵御成功的次数+1.

难点:很难想出方法来处理t秒的冷却时间。

0 0
原创粉丝点击