• HDU1166 (线段树/修改点,询问区间)


    详见代码

    View Code
     1 /*
     2 改变一个点,询问区间和
     3 线段树
     4 */
     5 #include<stdio.h>
     6 #define lson l , mid , rt << 1
     7 #define rson mid + 1 , r , rt << 1 | 1
     8 const int maxn = 50005;
     9 int sum[ maxn<<2 ],data[ maxn ];
    10 
    11 void PushUp( int rt ){
    12     sum[ rt ]=sum[ rt<<1 ]+sum[ rt<<1 | 1 ];
    13 }
    14 void build( int l,int r,int rt ){
    15     if( l==r ){
    16         sum[ rt ]=data[ l ];
    17         return ;
    18     }
    19     int mid;
    20     mid=((l+r)>>1);
    21     build( lson );
    22     build( rson );
    23     PushUp( rt );
    24     return ;
    25 }
    26 
    27 void update( int pos,int add,int l,int r,int rt ){
    28     if( l==r ){
    29         sum[ rt ] += add;
    30         return ;
    31     }
    32     int mid;
    33     mid=((l+r)>>1);
    34     if( pos<=mid )
    35         update( pos,add,lson );
    36     else 
    37         update( pos,add,rson );
    38     PushUp( rt );
    39     return ;
    40 }
    41 
    42 int query( int from,int to,int l,int r,int rt ){
    43     if( from==l && to==r ){
    44         return sum[ rt ];
    45     }
    46     int mid;
    47     mid=((l+r)>>1);
    48     if( to<=mid )
    49         return query( from,to,lson );
    50     else if( from>mid )
    51         return query( from,to,rson );
    52     else return query( from,mid,lson )+query( mid+1,to,rson );
    53 }
    54 
    55 int main(){
    56     int T;
    57     scanf("%d",&T);
    58     for( int t=1;t<=T;t++ ){
    59         int n;
    60         scanf("%d",&n);
    61         for( int i=1;i<=n;i++ )
    62             scanf("%d",&data[ i ]);
    63         build( 1,n,1 );
    64         char s[ 10 ];
    65         printf("Case %d:\n",t);
    66         while( 1 ){
    67             scanf("%s",s);
    68             int a,b;
    69             if( s[0]=='E' ) break;
    70             if( s[0]=='Q' ){
    71                 scanf("%d%d",&a,&b);
    72                 printf("%d\n",query( a,b,1,n,1 ) );
    73             }
    74             else if( s[0]=='S' ){
    75                 scanf("%d%d",&a,&b);
    76                 update( a,-b,1,n,1 );
    77             }
    78             else if( s[0]=='A' ){
    79                 scanf("%d%d",&a,&b);
    80                 update( a,b,1,n,1 );
    81             }
    82         }
    83     }
    84     return 0;
    85 }

     另一种个人标准写法:

    View Code
      1 /*
      2 线段树+询问一段区间,改变一个点
      3 */
      4 #include<stdio.h>
      5 #include<string.h>
      6 #include<stdlib.h>
      7 #include<algorithm>
      8 #include<iostream>
      9 #include<queue>
     10 #include<vector>
     11 #include<map>
     12 #include<math.h>
     13 typedef long long ll;
     14 //typedef __int64 int64;
     15 const int maxn = 50005;
     16 const int maxm = 1005;
     17 const int inf = 0x7FFFFFFF;
     18 const double pi = acos(-1.0);
     19 const double eps = 1e-8;
     20 #define left( x ) (x<<1)
     21 #define right( x ) ((x<<1)+1) 
     22 struct tree{
     23     int l,r,num;
     24 }anode[ maxn<<2 ];
     25 int data[ maxn ];
     26 
     27 void pushup( int n ){
     28     anode[ n ].num = anode[ left( n ) ].num+anode[ right( n ) ].num;
     29     return ;
     30 }//向上update    
     31     
     32 void build( int l,int r,int n ){
     33     int mid;
     34     mid = (l+r)>>1;
     35     if( l==r ){
     36         anode[ n ].l = l;
     37         anode[ n ].r = r;
     38         anode[ n ].num = data[ l ];
     39         return ;
     40     }
     41     anode[ n ].l = l;
     42     anode[ n ].r = r;
     43     build( l,mid,left( n ) );
     44     build( mid+1,r,right( n ) );
     45     pushup( n );
     46 }
     47 
     48 void update( int aim_id,int new_num,int n ){
     49     if( anode[ n ].l==aim_id&&anode[ n ].r==aim_id ){
     50         anode[ n ].num += new_num;
     51         return ;
     52     }
     53     if( anode[ left( n ) ].l<=aim_id &&anode[ left( n ) ].r>=aim_id )
     54         update( aim_id,new_num,left( n ) );
     55     else update( aim_id,new_num,right( n ) );
     56     pushup( n );
     57     return ;
     58 }
     59 
     60 int query( int l,int r,int n ){
     61     if( anode[ n ].l==l&&anode[ n ].r==r ){
     62         return anode[ n ].num;
     63     }
     64     int mid;
     65     mid = (anode[ n ].l+anode[ n ].r)>>1;
     66     if( mid>=r ) return query( l,r,left( n ) );
     67     else if( mid<l) return query( l,r,right( n ) );
     68     else return query( l,mid,left( n ) )+query( mid+1,r,right( n ) ); 
     69 } 
     70 int main(){
     71     int ca;
     72     scanf("%d",&ca);
     73     for( int tt=1;tt<=ca;tt++ ){
     74         int n;
     75         scanf("%d",&n);
     76         for( int i=1;i<=n;i++ )
     77             scanf("%d",&data[ i ]);
     78         build( 1,n,1 );
     79         printf("Case %d:\n",tt);
     80         char od[ 12 ];
     81         while( 1 ){
     82             scanf("%s",od);
     83             if( od[ 0 ]=='E' ) break;
     84             if( od[ 0 ]=='A' ){
     85                 int a,b;
     86                 scanf("%d%d",&a,&b);
     87                 update( a,b,1 );
     88             }
     89             else if( od[ 0 ]=='S' ){
     90                 int a,b;
     91                 scanf("%d%d",&a,&b);
     92                 update( a,-b,1 );
     93             }
     94             else if( od[ 0 ]=='Q' ){
     95                 int a,b;
     96                 scanf("%d%d",&a,&b);
     97                 printf("%d\n",query( a,b,1 ));
     98             }
     99         }
    100     }
    101     return 0;
    102 }
    keep moving...
  • 相关阅读:
    小例子-使用JS/JQ获取a标签的href网址
    R语言随手记-线性回归模型诊断
    R语言随手记-数据处理
    正选择分析-PAML discussion group
    ggtree-基本函数使用
    PhastCons
    R语言随手记-批量读取和循环处理多个数据文件
    变异位点有害性软件评估
    ggplot画图-分页and排序
    R画图-标题和坐标轴篇
  • 原文地址:https://www.cnblogs.com/xxx0624/p/2880300.html
Copyright © 2020-2023  润新知