The number of attacks equals to the total number of attacks minus the number of successful defenses
This question is mainly used to calculate the number of successful defenses.
Add an auxiliary array pos [] to record the number of attacks at a certain point after the last query (the first attack can be defended)
Every defensive hold once, the t0-1 attack, continue to judge the next t0 attack has included (that is, the attack to) Ask the point
Some people say that the complexity is O (n ^ 2). The worst complexity is indeed O (q) * O (q/t) log (n), which should be the edge of timeout, but it's over, and the speed is good after opening a link... Data may not be strong
View Code
#include<cstdio>
#include<cstring>
const int maxn = 20001;
int c[maxn];
struct pp{
int l,r;
}att[maxn];
inline int lowbit(int x){
return x&-x;
}
inline void update(int x,int d){
for(;x<maxn;x+=lowbit(x))
c[x]+=d;
}
int sum(int x){
int ans=0;
for(;x>0;x-=lowbit(x))
ans+=c[x];
return ans;
}
int defen[maxn],pos[maxn];
int main(){
int t,cases=1,i,j,t0,a,b,n,q;
char s[10];
scanf("%d",&t);
while(t--){
int tot=0;
memset(c,0,sizeof(c));
memset(pos,0,sizeof(pos));
memset(defen,0,sizeof(defen));
scanf("%d%d%d",&n,&q,&t0);att[0].l=att[0].r=0;
printf("Case %d:\n",cases++);
while(q--){
scanf("%s",s);
if(s[0]=='A'){
scanf("%d%d",&a,&b);
tot++;
att[tot].l=a;att[tot].r=b;
update(a,1);
update(b+1,-1);
}
else {
scanf("%d",&a);
for(i=pos[a];i<=tot;i++){
if(a>=att[i].l&&a<=att[i].r){
pos[a]=i+t0;
defen[a]++;
i+=t0-1;
}
}
printf("%d\n",sum(a)-defen[a]);
}
}
}
return 0;
}
Line Segment tree + comment
View Code
# Include <cstdio>
# Include <cstring>
# Define lson l, m, rt <1
# Define rson m + 1, r, rt <1 | 1
Const int maxn = 20001;
Int sum [maxn <2];
Int col [maxn <2];
Struct pp {
Int l, r;
} Att [maxn];
Int defen [maxn], pos [maxn];
Void update (int L, int R, int l, int r, int rt ){
If (L <= l & r <= R) {// if it is completely overwritten, it will not be updated. Otherwise, it will be degraded to O (n), which will inevitably lead to timeout.
Sum [rt] ++;
Return;
}
Int m = (l + r)> 1;
If (L <= m) update (L, R, lson );
If (R> m) update (L, R, rson );
}
Int query (int p, int l, int r, int rt) {// Add the number of overwrites on the tree in sequence, and add the number of overwrites to a certain vertex.
If (l = r ){
Return sum [rt];
}
Int m = (l + r)> 1;
Int ret = 0;
Ret + = sum [rt];
If (p <= m) return ret + query (p, lson );
Else return ret + query (p, rson );
}
Int main (){
Int t, cases = 1, I, j, t0, a, B, n, q;
Char s [10];
Scanf ("% d", & t );
While (t --){
Int tot = 0;
Memset (pos, 0, sizeof (pos ));
Memset (defen, 0, sizeof (defen ));
Scanf ("% d", & n, & q, & t0); att [0]. l = att [0]. r = 0;
Memset (rt, 0, sizeof (rt ));
Printf ("Case % d: \ n", cases ++ );
While (q --){
Scanf ("% s", s );
If (s [0] = 'A '){
Scanf ("% d", & a, & B );
Tot ++;
Att [tot]. l = a; att [tot]. r = B;
Update (a, B, 1, n, 1 );
}
Else {
Scanf ("% d", & );
For (I = pos [a]; I <= tot; I ++ ){
If (a> = att [I]. l & a <= att [I]. r ){
Pos [a] = I + t0;
Defen [a] ++;
I + = t0-1;
}
}
Printf ("% d \ n", query (a, 1, n, 1)-defen [a]);
}
}
}
Return 0;
}