黄李龙 这个题目有一处不好处理那么就是存在一个冷却时间,写了一个超时的版本,实在不行然后,参考了下课件上的代码,佩服啊。别人代码核心在于维护了一个保存最初冷却时间的数组。 我的: #include<iostream>
#include<stdio.h>
#include<map>
#include<string.h>
using namespace std;
const int M=100000+32;
int cnt[M];
int lowbit(int x)
{
return x&(-x);
}
long long sum(int x)
{
long long s=0;
while(x>0)
{
s+=cnt[x];
x-=lowbit(x);
}
return s;
}
void add(int x,int val)
{
while(x<=M)
{
cnt[x]+=val;
x+=lowbit(x);
}
}
int main()
{
char buf[23];
int T,n,q,t,temp1,temp2,xx=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&q,&t);
printf("Case %d:\n",++xx);
map<int,int>dict;
memset(cnt,0,sizeof(cnt));
for(int k=1;k<=n;k++)
dict[k]=0;
for(int i=0; i<q; i++)
{
scanf("%s",buf);
if(buf[0]=='A')
{
scanf("%d",&temp1);
if(dict[temp1]==0)
{
dict[temp1]++;
}
else if(dict[temp1]!=t)
{
add(temp1,1);
dict[temp1]++;
}
else if(dict[temp1]==t)
{
dict[temp1]=0;
}
}
for(int it=1; it<=n; it++)
{
if(dict[it]>0&&it!=temp1)
{
dict[it]++;
if(dict[it]==t)
dict[it]=0;
}
}
if(buf[0]=='Q')
{
scanf("%d%d",&temp1,&temp2);
if(temp1>temp2)
swap(temp1,temp2);
printf("%lld\n",sum(temp2)-sum(temp1-1));
}
}
}
return 0;
}
AC:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int M=100000+32;
int cnt[M];
int lowbit(int x)
{
return x&(-x);
}
long long sum(int x)
{
long long s=0;
while(x>0)
{
s+=cnt[x];
x-=lowbit(x);
}
return s;
}
void add(int x,int val)
{
while(x<=M)
{
cnt[x]+=val;
x+=lowbit(x);
}
}
int main()
{
char buf[23];
int T,n,q,t,temp1,temp2,xx=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&q,&t);
printf("Case %d:\n",++xx);
int p[M];
for(int l=0; l<=n; l++)
{
p[l]=-t;
}
memset(cnt,0,sizeof(cnt));
int attack_cnt=0;
for(int i=0; i<q; i++)
{
scanf("%s",buf);
if(buf[0]=='A')
{
attack_cnt++;
scanf("%d",&temp1);
if(p[temp1]+t<=attack_cnt)//就是说如果发现冷却时间已经过了,那么这次完全
p[temp1]=attack_cnt;//可以防御,并这时候更新最初冷却时间
else
add(temp1,1);
}
if(buf[0]=='Q')
{
scanf("%d%d",&temp1,&temp2);
if(temp1>temp2)
swap(temp1,temp2);
printf("%lld\n",sum(temp2)-sum(temp1-1));
}
}
}
return 0;
}
|