HDU-4027-Can you answer these queries
Segment segments are updated. n is relatively small. It is updated to each leaf node. Note that after the root number is 1, it is still 1 and does not need to be updated.
[Cpp]
# Include <iostream>
# Include <cstring>
# Include <cstdlib>
# Include <cstdio>
# Include <cmath>
Using namespace std;
Typedef _ int64 LL;
# Define N 100010
Struct cam
{
Int x;
Int y;
LL sum;
} List [N * 5];
Void build (int k, int x, int y)
{
List [k]. x = x;
List [k]. y = y;
If (x = y)
{
Scanf ("% I64d", & list [k]. sum );
Return;
}
Int mid;
Mid = (x + y)/2;
Build (k <1, x, mid );
Build (k <1 | 1, mid + 1, y );
List [k]. sum = list [k <1]. sum + list [k <1 | 1]. sum;
}
Void update (int k, int x, int y)
{
If (list [k]. sum = list [k]. y-list [k]. x + 1) // The root number of 1 is not 1 and does not need to be updated.
Return;
If (list [k]. x = list [k]. y)
{
List [k]. sum = (LL) sqrt (double) list [k]. sum );
Return;
}
Int mid = (list [k]. x + list [k]. y)/2;
If (x> mid)
Update (k <1 | 1, x, y );
Else if (y <= mid)
Update (k <1, x, y );
Else
{
Update (k <1, x, mid );
Update (k <1 | 1, mid + 1, y );
}
List [k]. sum = list [k <1]. sum + list [k <1 | 1]. sum;
}
LL find (int k, int x, int y)
{
Int mid;
If (list [k]. x = x & list [k]. y = y)
Return list [k]. sum;
Mid = (list [k]. x + list [k]. y)/2;
If (x> mid)
Return find (k <1 | 1, x, y );
Else if (y <= mid)
Return find (k <1, x, y );
Else
Return find (k <1, x, mid) + find (k <1 | 1, mid + 1, y );
}
Int main ()
{
Int n, m, t, x, y;
Int cnt = 0;
While (scanf ("% d", & n )! = EOF)
{
Cnt ++;
Printf ("Case # % d: \ n", cnt );
Build (1, 1, n );
Scanf ("% d", & m );
While (m --)
{
Scanf ("% d", & t, & x, & y );
If (x> y)
Swap (x, y );
If (t = 0)
Update (1, x, y );
Else
Printf ("% I64d \ n", find (1, x, y ));
}
Printf ("\ n ");
}
Return 0;
}
By Cambridgeacm