• spoj 1716 Can you answer these queries III(线段树)


    和I相比有了单点更新,所以不能只记录一个前缀和,而是要在线段树上多维护一个sum,表示这个结点的区间和,然后其他的就和I一样了。

      1 #include <iostream>
      2 #include <cstring>
      3 #include <cstdio>
      4 using namespace std;
      5 
      6 const int N = 50001;
      7 int a[N];
      8 
      9 struct Node 
     10 {
     11     int l, r, sum;
     12     int maxl, maxr, maxn;
     13 } node[N << 2];
     14 
     15 void pushup( int i )
     16 {
     17     int lc = i << 1, rc = lc | 1;
     18     node[i].sum = node[lc].sum + node[rc].sum;
     19     node[i].maxl = max( node[lc].maxl, node[lc].sum + node[rc].maxl );
     20     node[i].maxr = max( node[rc].maxr, node[rc].sum + node[lc].maxr );
     21     node[i].maxn = max( node[lc].maxn, node[rc].maxn );
     22     node[i].maxn = max( node[i].maxn, node[lc].maxr + node[rc].maxl );
     23 }
     24 
     25 void build( int i, int l, int r )
     26 {
     27     node[i].l = l, node[i].r = r;
     28     if ( l == r )
     29     {
     30         node[i].sum = node[i].maxl = node[i].maxr = node[i].maxn = a[l];
     31         return ;
     32     }
     33     int lc = i << 1, rc = lc | 1, mid = ( l + r ) >> 1;
     34     build( lc, l, mid );
     35     build( rc, mid + 1, r );
     36     pushup(i);
     37 }
     38 
     39 void update( int i, int pos, int val )
     40 {
     41     if ( node[i].l == pos && node[i].r == pos )
     42     {
     43         node[i].sum = node[i].maxl = node[i].maxr = node[i].maxn = val;
     44         return ;
     45     }
     46     int mid = ( node[i].l + node[i].r ) >> 1;
     47     if ( pos <= mid )
     48     {
     49         update( i << 1, pos, val );
     50     }
     51     else
     52     {
     53         update( i << 1 | 1, pos, val );
     54     }
     55     pushup(i);
     56 }
     57 
     58 Node query( int i, int l, int r )
     59 {
     60     if ( node[i].l == l && node[i].r == r )
     61     {
     62         return node[i];
     63     }
     64     int lc = i << 1, rc = lc | 1, mid = ( node[i].l + node[i].r ) >> 1;
     65     if ( r <= mid )
     66     {
     67         return query( lc, l, r );
     68     }
     69     else if ( l > mid )
     70     {
     71         return query( rc, l, r );
     72     }
     73     else
     74     {
     75         Node ln = query( lc, l, mid ), rn = query( rc, mid + 1, r ), res;
     76         res.sum = ln.sum + rn.sum;
     77         res.maxl = max( ln.maxl, ln.sum + rn.maxl );
     78         res.maxr = max( rn.maxr, rn.sum + ln.maxr );
     79         res.maxn = max( ln.maxn, rn.maxn );
     80         res.maxn = max( res.maxn, ln.maxr + rn.maxl );
     81         return res;
     82     }
     83 }
     84 
     85 int main ()
     86 {
     87     int n, m;   
     88     while ( scanf("%d", &n) != EOF )
     89     {
     90         for ( int i = 1; i <= n; i++ )
     91         {
     92             scanf("%d", a + i);
     93         }
     94         build( 1, 1, n );
     95         scanf("%d", &m);
     96         while ( m-- )
     97         {
     98             int op, x, y;
     99             scanf("%d%d%d", &op, &x, &y);
    100             if ( op == 0 )
    101             {
    102                 update( 1, x, y );
    103             }
    104             else
    105             { 
    106                 Node tmp = query( 1, x, y );
    107                 printf("%d
    ", tmp.maxn);
    108             }
    109         }
    110     }
    111     return 0;
    112 }
  • 相关阅读:
    第五小节之JAVA IO流
    第四小节之Java 集合类
    初学的重点
    第三小节之Java API
    实践周java基础软件开发app之五子棋
    出栈合法性
    Python介绍
    Ubuntu主题美化篇
    Ubuntu16.04比较好的一系列软件安装介绍
    Ubuntu使用Shadow socks-qt5
  • 原文地址:https://www.cnblogs.com/huoxiayu/p/4691141.html
Copyright © 2020-2023  润新知