Hdu_3437
It is easy to prove that X should take the median of the interval, so the problem is converted into the number of (L + r)/2 + 1 in the [L, R] range, then, computation and.
The median of an interval can be achieved through the partitioning tree, but the sum cannot be calculated directly using the original sequence after the median is obtained, because the elements of the original interval are unordered, we cannot calculate the difference sum. The condition for difference summation is to specify which numbers are larger than the median and which are smaller than the median, while the elements of the Left subtree of the tree are always smaller than those of the right subtree, so if the median is in the left subtree, We can first find the difference between the number and the median in the [L, R] interval of the right subtree. If the median is in the right subtree, we can first find out the difference between the number and the median in the [L, R] range in the left subtree. The process of summation can be implemented when the specific value of the median is obtained and then traced back. To achieve quick summation, we can create an additional array a [d] [I] when creating a tree, indicates the sum of the I elements and the previous elements on a node at Layer D.
# Include <stdio. h>
# Include < String . H>
# Include <stdlib. h>
# Define Maxk 20
# Define Maxd 100010
Int N, m, sa [maxd], a [maxd], rank [maxk] [maxd], H [maxk] [maxd];
Long Long Int A [maxk] [maxd], ans;
Int CMP ( Const Void * _ P, Const Void * _ Q)
{
Int * P = ( Int *) _ P, * q = ( Int *) _ Q;
If (A [* p] = A [* q])
Return * P-* q;
Return A [* p]-A [* q];
}
Void Init ()
{
Int I, J, K;
Scanf ( " % D " , & N );
For (I =1 ; I <= N; I ++)
{
Scanf ( " % D " , & A [I]);
Sa [I] = I;
}
}
Void Build ( Int Lx, Int RX, Int D)
{
If (Lx = RX)
{
A [d] [lx] = A [SA [rank [d] [lx];
Return ;
}
Int I, J, K, P = 0 , Mid = (LX + RX )/ 2 ;
For (I = Lx; I <= RX; I ++)
{
If (Rank [d] [I] <= mid)
Rank [d + 1 ] [Lx + P ++] = rank [d] [I];
Else
Rank [d +1 ] [Mid + I-lx + 1 -P] = rank [d] [I];
H [d] [I] = P;
A [d] [I] = A [SA [rank [d] [I] + (I = Lx? 0 : A [d] [I- 1 ]);
}
Build (LX, mid, D + 1 );
Build (Mid + 1 , RX, D + 1 );
}
Int Search ( Int Lx, Int RX, Int X, Int Y, Int K, Int D)
{
If (Lx = RX)
Return Sa [rank [d] [lx];
Int J, n, m, mid = (LX + RX )/ 2 , TX, Ty;
N = H [d] [Y], M = x = Lx? 0 : H [d] [x-1 ];
If (N-M> = K)
{
J = search (LX, mid, LX + M, LX + N- 1 , K, D + 1 );
Tx = Mid + 1 + X-lx-M, Ty = Mid + 1 + Y-lx-N;
If (TX <= ty)
Ans + = A [d + 1 ] [Ty]-(Tx = Mid + 1 ?0 : A [d + 1 ] [TX- 1 ])-( Long Long Int ) (Ty-Tx + 1 ) * A [J];
}
Else
{
J = search (Mid + 1 , RX, Mid + 1 + X-lx-M, Mid + 1 + Y-lx-N, K-N + M, D +1 );
Tx = Lx + M, Ty = Lx + N- 1 ;
If (TX <= ty)
Ans + = ( Long Long Int ) (Ty-Tx + 1 ) * A [J]-A [d + 1 ] [Ty] + (Tx = Lx? 0 : A [d + 1 ] [TX- 1 ]);
}
Return J;
}
Void Solve ()
{
Int I, J, K, X, Y;
Qsort (SA + 1 , N, Sizeof (SA [ 0 ]), CMP );
For (I = 1 ; I <= N; I ++)
Rank [ 0 ] [SA [I] = I;
Build (1 , N, 0 );
Scanf ( " % D " , & M );
For (I = 0 ; I <m; I ++)
{
Scanf ( " % D " , & X, & Y );
++ X, ++ y;
K = (Y-x )/ 2 +1 ;
Ans = 0 ;
Search ( 1 , N, x, y, K, 0 );
Printf ( " % I64d \ n " , ANS );
}
}
Int Main ()
{
Int T, TT;
Scanf (" % D " , & T );
For (Tt = 0 ; TT <t; TT ++)
{
Init ();
Printf ( " Case # % d: \ n " , Tt + 1 );
Solve ();
Printf ( " \ N " );
}
Return 0 ;
}