HDU 4031 Attack(樹狀數組),hdu4031
Problem 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 formats
1. Attack si ti
Al Qaeda attack the wall from si to ti, inclusive. 1 ≤ si ≤ ti ≤ N
2. Query p
How many times the pth unit have been successfully attacked. 1 ≤ p ≤ N
The kth attack happened at the kth second. Queries don’t take time.
1 ≤ N, Q ≤ 20000
1 ≤ 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 Input
23 7 2Attack 1 2Query 2Attack 2 3Query 2Attack 1 3Query 1Query 39 7 3Attack 5 5Attack 4 6Attack 3 7Attack 2 8Attack 1 9Query 5Query 3
Sample Output
Case 1:0101Case 2:32
題意:
美國建立了一套新的防禦系統。每個點都可以進行自動防禦,但是防禦過後該點的防禦
需要一段冷卻時間,也就是說在此期間不能再進行防禦。現在又兩種操作:1.恐怖分子
每次會對一段範圍進行攻擊。2.指揮官詢問某點被攻擊的次數
BIT做法:我們知道由於詢問不費時間,所以我們考慮設計一個數組tt[i]記錄i點可以
進行防禦的時間,當時間t>=tt[i]的時候表示攻擊可以被防禦。那麼被攻擊的次數=
總的攻擊次數-防禦次數。
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;const int maxn = 20005;int N,Q,T,t;int c[maxn],d[maxn],tt[maxn];//tt[i]記錄某點可以進行防禦的時間,大於這個時間都可以進行防禦;struct node//d[i]記錄某點成功防禦次數{ int l,r;}e[maxn];int lowbit(int x){ return x&(-x);}void update(int x,int w){ while(x<maxn) { c[x]+=w; x+=lowbit(x); }}int query(int x){ int s=0; while(x>0) { s+=c[x]; x-=lowbit(x); } return s;}int main(){ int cas=1; int pos,l,r; char str[5]; scanf("%d",&t); while(t--) { int cnt=0; CLEAR(c,0); CLEAR(tt,0); CLEAR(d,0); scanf("%d%d%d",&N,&Q,&T); printf("Case %d:\n",cas++); while(Q--) { scanf("%s",str); if(str[0]=='A') { scanf("%d%d",&l,&r); e[cnt].l=l; e[cnt++].r=r; update(l,1); update(r+1,-1); } else { scanf("%d",&pos); for(int i=tt[pos];i<cnt;i++)//記錄某點可以進行防禦的時間,大於等於該時間的攻擊都可以自動進行防禦 { if(pos>=e[i].l&&pos<=e[i].r) { d[pos]++; tt[pos]=i+T; i+=T-1; } } printf("%d\n",query(pos)-d[pos]); } } } return 0;}