【codevs 1369】题解

 #include <iostream>
#include <stdio.h>

using namespace std;

int n , m;
int a[200000 + 2];
int i;
int l , r;

struct Node
{
       int l , r;
       int value;
       Node * left , * right;
};

int merge( int a , int b )
{
    return a + b;
}

void modify( int x , Node * cur )
{
     if( cur -> l == cur -> r )
     {
         cur -> value = 0;
         return;
     }
     int mid = ( cur -> l + cur -> r ) / 2;
     if( x > mid )
         modify( x , cur -> right );
     else
         modify( x , cur -> left );
     cur -> value = merge( cur -> left -> value , cur -> right -> value );
     return;
}

int find( int l , int r , Node * cur )
{
    if( cur -> l == l && cur -> r == r )
        return cur -> value;
    int mid = ( cur -> l + cur -> r ) / 2;
    if( l > mid )
        return find( l , r , cur -> right );
    else if( mid >= r )
        return find( l , r , cur -> left );
    else
        return merge( find( l , mid , cur -> left ) , find( mid + 1 , r , cur -> right ) );
}

Node * build( int l , int r )
{
     Node * cur = new Node();
     cur -> l = l;
     cur -> r = r;
     if( cur -> l == cur -> r )
     {
         cur -> value = a[l];
         return cur;
     }
     int mid = ( cur -> l + cur -> r ) / 2;
     cur -> left = build( l , mid );
     cur -> right = build( mid + 1 , r );
     cur -> value = merge( cur -> left -> value , cur -> right -> value );
     return cur;
}

Node * root;

int main()
{
    scanf( "%d" , &n );
    for( i = 1 ; i <= n ; i++ )
         scanf( "%d" , &a[i] );
    root = build( 1 , n );
    scanf( "%d" , &m );
    for( i = 0 ; i < m ; i++ )
    {
         scanf( "%d %d" , &l , &r );
         printf( "%.2f\n" , 3.14 * find( l , r , root ) );
         modify( ( l + r ) / 2 , root );
    }
    return 0;
}

你可能感兴趣的:(【codevs 1369】题解)