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
Case 1: 0 1 0 1 Case 2: 3 2
#include<iostream>
#include<cstdio>
#include<cstring>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define Max 20005
int N,Q,t;
struct
{
int atk;
int cover;
}tree[Max<<2];
void push_up(int rt)
{
tree[rt].atk=tree[rt<<1].atk+tree[rt<<1|1].atk;
}
void push_down(int rt,int m)
{
if(tree[rt].cover)
{
tree[rt<<1].cover+=tree[rt].cover;
tree[rt<<1|1].cover+=tree[rt].cover;
tree[rt<<1].atk+=(m-(m>>1))*tree[rt].cover;
tree[rt<<1|1].atk+=(m>>1)*tree[rt].cover;
tree[rt].cover=0;
}
}
void update(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
tree[rt].cover++;
tree[rt].atk+=r-l+1;
return ;
}
push_down(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m)
update(L,R,lson);
if(R>m)
update(L,R,rson);
push_up(rt);
}
int query(int pos,int l,int r,int rt)
{
if(l==r)
return tree[rt].atk;
push_down(rt,r-l+1);
int m=(l+r)>>1;
if(pos<=m)
return query(pos,lson);
else
return query(pos,rson);
}
int main()
{
int i,T,time,a,b,atk[Max][2],pre[Max],def[Max],ncase=1;
char op[10];
scanf("%d",&T);
while(T--)
{
time=0;
memset(tree,0,sizeof(tree));
scanf("%d%d%d",&N,&Q,&t);
memset(atk,0,sizeof(atk));
memset(def,0,sizeof(def));
for(i=1;i<=N;i++)
pre[i]=1;
printf("Case %d:\n",ncase++);
while(Q--)
{
scanf("%s",op);
if(op[0]=='A')
{
scanf("%d%d",&a,&b);
atk[++time][0]=a;atk[time][1]=b;
update(a,b,1,N,1);
}
else
{
scanf("%d",&a);
if(t==0)
{
printf("0\n");
continue;
}
for(i=pre[a];i<=time;i++)
{
if(atk[i][0]<=a&&atk[i][1]>=a)
{
def[a]++;
pre[a]=i+t;
i+=t-1;
}
}
printf("%d\n",query(a,1,N,1)-def[a]);
}
}
}
return 0;
}
原文:http://blog.csdn.net/hqu_fritz/article/details/39373161