Attack
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 2011 Accepted Submission(s): 588
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.
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
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
2 3 7 2 Attack 1 2 Query 2 Attack 2 3 Query 2 Attack 1 3 Query 1 Query 3 9 7 3 Attack 5 5 Attack 4 6 Attack 3 7 Attack 2 8 Attack 1 9 Query 5 Query 3
Sample Output
Case 1: 0 1 0 1 Case 2: 3 2
这题有个限制,在攻击一次后的t秒内,失去保护,因此我们开个数组记录被保护的次数,注意询问具有继承性,也就是我们无须从第一次攻击开始算,而是从上一次询问达到的时间开始,继续统计被保护次数,每保护我们可以跳跃时间间隔t,而且由于总是从上一次询问时间开始,每个时间点最多算一次,这样总复杂度为O(nq/t),q为query不同点的个数,这是估计的上界,实际会有Attack,q达不到n级别.总体上复杂度是可以接受的,加上树状数组更新,复杂度O(nlogn)
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#define Maxn 20010
using namespace std;
int n,c[Maxn];
void update(int x,int d){
while(x<=n){
c[x]+=d;
x+=x&-x;
}
}
int sum(int x){
int res=0;
while(x){
res+=c[x];
x-=x&-x;
}
return res;
}
char s[20];
int use[Maxn],p[Maxn];
int ata[Maxn],atb[Maxn];
int main()
{
int T,t,cas=1,m,a,b;
cin>>t;
while(t--){
printf("Case %d:\n",cas++);
cin>>n>>m>>T;
memset(c,0,sizeof c);
memset(p,0,sizeof p);
memset(use,0,sizeof use);
int tm=0;
for(int i=0;i<m;i++){
scanf("%s%d",s,&a);
if(s[0]=='A'){
scanf("%d",&b);
ata[tm]=a,atb[tm]=b;
update(a,1);
update(b+1,-1);
tm++;
}
else{
for(int j=use[a];j<tm;j++){
if(ata[j]<=a&&a<=atb[j])
p[a]++,use[a]=j+T,j=use[a]-1;
}
printf("%d\n",sum(a)-p[a]);
}
}
}
return 0;
}