zoukankan      html  css  js  c++  java
  • hdu1166-敌兵布阵(线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=1166

    区间更新,区间求和

    // File Name: hdu1166.cpp
    // Author: bo_jwolf
    // Created Time: 2013年08月16日 星期五 11时27分03秒
    
    #include<vector>
    #include<list>
    #include<map>
    #include<set>
    #include<deque>
    #include<stack>
    #include<bitset>
    #include<algorithm>
    #include<functional>
    #include<numeric>
    #include<utility>
    #include<sstream>
    #include<iostream>
    #include<iomanip>
    #include<cstdio>
    #include<cmath>
    #include<cstdlib>
    #include<cstring>
    #include<ctime>
    
    using namespace std;
    
    #define lson l , mid , rt << 1 
    #define rson mid + 1 , r , rt << 1 | 1 
    
    const int maxn = 55555 ;
    //int sum[ maxn << 2 ] ;
    
    struct node
    {
    	int sum ;
    }tree[ maxn << 2 ] ;
    
    void PushUp( int rt )
    {
    	tree[ rt ].sum  = tree[ rt << 1 ].sum + tree[ (rt << 1 | 1 ) ].sum ;
    }
    void build( int l , int r , int rt )
    {
    	if( l == r )
    	{
    		scanf( "%d" , &tree[ rt ].sum );
    		return ;
    	}
    	int mid = ( l + r ) >> 1 ;
    	build( lson ) ;
    	build( rson ) ;
    	PushUp( rt ) ;
    }
    
    void update( int p , int add , int l , int r , int rt )
    {
    	if( l == r )
    	{
    		tree[ rt ].sum  += add ;
    		return ;
    	}
    	int mid = ( l + r ) >> 1 ;
    	if( p <= mid )
    		update( p , add , lson ) ;
    	else
    		update( p , add , rson ) ;
    	PushUp( rt ) ;
    }
    
    int query( int L , int R , int l , int r , int rt )
    {
    	if( L <= l && r <=R )
    	{
    		return tree[ rt ].sum ;
    	}
    	int mid = ( l + r ) >> 1 ;
    	int ret = 0 ; 
    	if( L <= mid )
    			ret += query( L , R , lson ) ;
    	if( R > mid )
    			ret += query( L , R , rson ) ;
    	return ret ;
    }
    
    int main()
    {
    	int T , n ; 
    	scanf( "%d" , &T ) ;
    	for( int cas = 1 ; cas <= T ; ++cas )
    	{
    		printf( "Case %d:
    " , cas ) ;
    		scanf( "%d" , &n ) ;
    		build( 1 , n , 1 ) ;
    		char op[ 10 ] ;
    		while( scanf( "%s" , op ) )
    		{
    			if( op[ 0 ] == 'E' ) 
    				break ;
    			int a , b ;
    			scanf( "%d%d" , &a , &b ) ;
    			if( op[ 0 ] == 'Q' )
    				printf( "%d
    " , query( a , b , 1 , n , 1 ) ) ;
    			else if( op[ 0 ] == 'S' )
    				update( a , -b , 1 , n , 1 ) ;
    			else
    				update( a , b , 1 , n , 1 ) ;
    		}
    	}
    return 0;
    }


  • 相关阅读:
    mac redis 安装及基本设置 python操作redis
    mac webstorm自动编译typescript配置
    MySQL数据库的基本操作
    python 面试基础考试题收集
    pyhon 列表的增删改查
    python 文件读取方法详解
    MAC下绕开百度网盘限速下载的方法,三步操作永久生效
    浏览器窗口输入网址后发生的一段事情(http完整请求)
    CMDB
    django适当进阶篇
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3263056.html
Copyright © 2011-2022 走看看