很容易想到 成功攻击次数 = 总攻击次数 - 防御次数。 然后把这两个分开处理就OK了
拿过来练习splay tree
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 20007;
#define KT ch[ch[root][1]][0]
int root, ch[N][2], fa[N], sz[N];
int sum[N], num[N], pos[N], def[N], al[N], ar[N];
inline void pushup(int x)
{
sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1;
}
inline void pushdown(int x)
{
if(sum[x])
{
sum[ch[x][0]] += sum[x];
sum[ch[x][1]] += sum[x];
num[ch[x][0]] += sum[x];
num[ch[x][1]] += sum[x];
sum[x] = 0;
}
}
void build(int l, int r, int f)
{
if(l > r)
return;
int m = (l + r) >> 1;
fa[m] = f;
ch[m][0] = m - 1 >= l? (m - 1 + l) >> 1: 0;
ch[m][1] = r >= m + 1? (r + m + 1) >> 1: 0;
build(l, m - 1, m);
build(m + 1, r, m);
pushup(m);
}
void init(int ans)
{
memset(num, 0, sizeof(num));
memset(sum, 0, sizeof(sum));
sz[0] = 0;
root = (ans + 1) >> 1;
build(1, ans, 0);
}
inline void rotate(int x, bool f)
{
int y = fa[x];
int z = fa[y];
pushdown(y);
pushdown(x);
ch[y][!f] = ch[x][f];
fa[ch[x][f]] = y;
fa[x] = z;
if(z)
ch[z][ch[z][1] == y] = x;
ch[x][f] = y;
fa[y] = x;
pushup(y);
}
void splay(int x, int g)
{
int y = fa[x];
pushdown(x);
while(y != g)
{
int z = fa[y];
bool f = (ch[y][0] == x);
if(z != g && f == (ch[z][0] == y))
rotate(y, f);
rotate(x, f);
y = fa[x];
}
pushup(x);
if(g == 0)
root = x;
}
void rotateto(int k, int g)
{
int x = root;
pushdown(x);
while(sz[ch[x][0]] != k)
{
if(k < sz[ch[x][0]])
x = ch[x][0];
else
{
k -= sz[ch[x][0]] + 1;
x = ch[x][1];
}
pushdown(x);
}
splay(x, g);
}
void update(int a, int b)
{
rotateto(a - 1, 0);
rotateto(b + 1, root);
num[KT] += 1;
sum[KT] += 1;
}
int que(int a)
{
rotateto(a - 1, 0);
rotateto(a + 1, root);
return num[KT];
}
int main()
{
// freopen("in.txt", "r", stdin);
int T, C = 1;
scanf("%d", &T);
while(T--)
{
int n, m, t;
scanf("%d%d%d", &n, &m, &t);
init(n + 2);
printf("Case %d:\n", C++);
int ans = 0;
memset(def, 0, sizeof(def));
memset(pos, 0 ,sizeof(pos));
while(m--)
{
char s[10];
int a, b;
scanf("%s", s);
if(s[0] == 'A')
{
scanf("%d%d", &a, &b);
update(a, b);
al[ans] = a;
ar[ans++] = b;
}
else
{
scanf("%d", &a);
if(t == 1)
{
puts("0");
continue;
}
for(int i = pos[a]; i < ans; i++)
if(a >= al[i] && a <= ar[i])
{
def[a]++;
pos[a] = i + t;
i += t - 1;
}
printf("%d\n", que(a) - def[a]);
}
}
}
return 0;
}