hdu4031之树状数组

来源:互联网 发布:盘古数据 编辑:程序博客网 时间:2024/06/06 04:32

Attack

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1618    Accepted Submission(s): 469


Problem Description
Today 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.
 

Input
The 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
 

Output
For 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
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <queue>#include <algorithm>#include <map>#include <cmath>#include <iomanip>#define INF 99999999typedef long long LL;using namespace std;const int MAX=20000+10;int n,q,T;int a[MAX],b[MAX],t[MAX],ans[MAX];int c[MAX],now[MAX];int lowbit(int x){return x&(-x);}void Update(int x,int d){while(x<=n){c[x]+=d;x+=lowbit(x);}}int Query(int x){int sum=0;while(x>0){sum+=c[x];x-=lowbit(x);}return sum;}void Init(int num){for(int i=0;i<=num;++i)c[i]=ans[i]=0,now[i]=1;}int main(){int tt,size,num=0,x;char op[10];scanf("%d",&tt);while(tt--){scanf("%d%d%d",&n,&q,&T);Init(n);size=1;t[0]=-INF;printf("Case %d:\n",++num);for(int i=1;i<=q;++i){scanf("%s",op);if(op[0] == 'A'){scanf("%d%d",&a[size],&b[size]);Update(a[size],1);Update(b[size]+1,-1);t[size]=size++;}else{scanf("%d",&x);while(now[x]<size){if(a[now[x]]<=x  && x<=b[now[x]]){now[x]+=T;++ans[x];}else ++now[x]; }printf("%d\n",Query(x)-ans[x]);}}}return 0;}



0 0
原创粉丝点击