[Cpp]
/*
The line segment tree is very powerful. This should also be a problem of interval processing and multiple queries, so it must be solved by the line segment tree.
Sum [] indicates the sum of the remainder of % 5 in the interval, and cnt indicates the total number in the interval.
First, discretization is performed. Then, A line segment tree is created to match the l, r, and A [] arrays in the line segment tree. Then, add and delete elements.
*/
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
Const _ int64 nMax = 100007;
_ Int64 N;
Struct Node
{
Char opr [5];
_ Int64;
Node (){}
Node (char * _ opr, _ int64 _)
{
Memcpy (opr, _ opr, sizeof (opr ));
A = _;
}
} Node [nMax];
Struct Tree
{
_ Int64 l, r;
_ Int64 sum [5];
_ Int64 cnt;
} Tree [nMax * 4];
_ Int64 A [nMax];
_ Int64 cnt;
Int cmp (const void * a, const void * B)
{
_ Int64 * pa = (_ int64 *);
_ Int64 * pb = (_ int64 *) B;
Return * pa-* pb;
}
Void build (_ int64 rt, _ int64 l, _ int64 r)
{
If (l <r)
{
_ Int64 mid = (l + r)/2;
Build (rt * 2, l, mid );
Build (rt * 2 + 1, mid + 1, r );
}
Tree [rt]. l = l;
Tree [rt]. r = r;
Tree [rt]. cnt = 0;
Memset (tree [rt]. sum, 0, sizeof (tree [rt]. sum ));
}
Void add (_ int64 rt, _ int64)
{
If (tree [rt]. l = tree [rt]. r)
{
Tree [rt]. cnt = 1;
Tree [rt]. sum [1] =;
Return;
}
_ Int64 mid = (tree [rt]. l + tree [rt]. r)/2;
If (a <= A [mid])
Add (rt * 2, );
Else
Add (rt * 2 + 1, );
Tree [rt]. cnt = tree [rt * 2]. cnt + tree [rt * 2 + 1]. cnt;
_ Int64 I;
Memset (tree [rt]. sum, 0, sizeof (tree [rt]. sum ));
For (I = 0; I <5; ++ I)
{
Tree [rt]. sum [I] + = tree [rt * 2]. sum [I];
Tree [rt]. sum [(tree [rt * 2]. cnt + I) % 5] + = tree [rt * 2 + 1]. sum [I];
}
}
Void del (_ int64 rt, _ int64)
{
If (tree [rt]. l = tree [rt]. r)
{
Tree [rt]. cnt = 0;
Tree [rt]. sum [1] = 0;
Return;
}
_ Int64 mid = (tree [rt]. l + tree [rt]. r)/2;
If (a <= A [mid])
Del (rt * 2, );
Else
Del (rt * 2 + 1, );
Tree [rt]. cnt = tree [rt * 2]. cnt + tree [rt * 2 + 1]. cnt;
_ Int64 I;
Memset (tree [rt]. sum, 0, sizeof (tree [rt]. sum ));
For (I = 0; I <5; ++ I)
{
Tree [rt]. sum [I] + = tree [rt * 2]. sum [I];
Tree [rt]. sum [(tree [rt * 2]. cnt + I) % 5] + = tree [rt * 2 + 1]. sum [I];
}
}
Int main ()
{
// Freopen ("e: // data. in", "r", stdin)
While (scanf ("% I64d", & N )! = EOF)
{
_ Int64 I, j;
Char opr [5];
_ Int64;
Cnt = 0;
For (I = 0; I <N; ++ I)
{
Scanf ("% s", opr );
If (opr [0] = 'A ')
{
Scanf ("% I64d", & );
A [cnt ++] =;
}
Else if (opr [0] = 'D ')
{
Scanf ("% I64d", & );
}
Else a = 0;
Node [I] = Node (opr, );
}
Qsort (A, cnt, sizeof (A [0]), cmp );
For (I = 1, j = 1; I <cnt; ++ I)
If (A [I]! = A [I-1])
A [j ++] = A [I];
Cnt = j; www.2cto.com
Build (1, 0, cnt-1 );
For (I = 0; I <N; ++ I)
{
If (node [I]. opr [0] = 'A ')
Add (1, node [I]. );
Else if (node [I]. opr [0] = 'D ')
Del (1, node [I]. );
Else
Printf ("% I64d \ n", tree [1]. sum [3]);
}
}
Return 0;
}