Can you answer these questions do (tree line)

topic:

Given the number of columns of length N A, and M instructions, each instruction may be either of the following:

1, "1 xy", the query interval [x, y] is the maximum continuous and sub-segment, i.e., 

 

2, "2 xy", the A [x] into y.

For each query command, output a integer answer.

Input Format

The first line of two integers N, M.

The second row of the N integers A [i].

Next, 3 M lines each integers k, x, y, k = 1 indicates that the query (in this case, if x> y, swap the x, y), k = 2 represents a modification.

Output Format

For each query command output represents an integer answer.

Each answer per line.

data range

N500000,M100000

Sample input:

5 3
1 2 -3 4 5
1 2 3
2 2 -1
1 3 2

Sample output:

2
-1






解题报告:
区间最大连续子段和,根据区间的可见性,咱们要在线段树的基础上增加lmax,rmax,分别是管理前缀最带子段和和后缀最大子段和,
然后再区间[l,r]这个区间的最大子段和就是左区间的最大子段和,右区间的最大子段和,以及左右区间结合在一起中间的最大子段和。

ac代码:
  . 1 #include <bits / STDC ++ H.>
   2  the using  namespace STD;
   . 3 typedef Long  Long LL;
   . 4  
  . 5  const  int MAXN = 500001 ;
   . 6  LL Maxx (LL A, LL B, LL C)
   . 7  {
   . 8      return max (A, max (B, C));
   . 9  }
 10  struct Node
 . 11  {
 12 is      int L, R & lt;
 13 is      LL Lmax, Rmax of, SUM, DAT;
 14      // Lmax and a maximum prefix section
 15      // Rmax of the maximum section and the suffix
 16      //dat 最大子段和 
 17     //sum  区间全加起来的值 
 18     int mid()
 19     {
 20         return (l+r)>>1;
 21     }
 22 }tree[maxn<<2];
 23 
 24 void pushup(int rt)
 25 {
 26     tree[rt].lmax=maxx(tree[rt<<1].sum,tree[rt<<1].sum+tree[rt<<1|1].lmax,tree[rt<<1].lmax);
 27     tree[rt].rmax=maxx(tree[rt<<1|1].sum,tree[rt<<1|1].sum+tree[rt<<1].rmax,tree[rt<<1|1].rmax);
 28     tree[rt].dat=maxx(tree[rt<<1].dat,tree[rt<<1|1].dat,tree[rt<<1].rmax+tree[rt<<1|1].lmax);
 29     tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
 30 }
 31 
 32 void build(int l,int r,int rt)
 33 {
 34     tree[rt].l=l;
 35     tree[rt].r=r;
 36     if(l==r)
 37     {
 38         scanf("%lld",&tree[rt].dat);
 39         tree[rt].sum=tree[rt].lmax=tree[rt].rmax=tree[rt].dat;
 40         return;
 41     }
 42     int m=tree[rt].mid();
 43     build(l,m,rt<<1);
 44     build(m+1,r,rt<<1|1);
 45     pushup(rt);
 46 }
 47 void update(int rt,int x,int y)
 48 {
 49     if(tree[rt].l==tree[rt].r)
 50     {
 51         tree[rt].sum=tree[rt].lmax=tree[rt].rmax=tree[rt].dat=y;
 52         return;
 53     }
 54     int m=tree[rt].mid();
 55     if(x<=m)
 56         update(rt<<1,x,y);
 57     else
 58         update(rt<<1|1,x,y);
 59     pushup(rt);
 60 }
 61 
 62 node query(int rt,int l,int r)
 63 {
 64     if(tree[rt].l==l&&tree[rt].r==r)
 65     {
 66         return tree[rt];
 67     }
 68     int m=tree[rt].mid();
 69     if(m>=r)
 70     {
 71         return query(rt<<1,l,r);
 72     }
 73     else if(m<l)
 74     {
 75         return query(rt<<1|1,l,r);
 76     }
 77     else
 78     {
 79         node ls=query(rt<<1,l,m);
 80         node rs=query(rt<<1|1,m+1,r);
 81         node ans;
 82         ans.dat=maxx(ls.dat,rs.dat,ls.rmax+rs.lmax);
 83         ans.lmax=maxx(ls.sum,ls.lmax,ls.sum+rs.lmax);
 84         ans.rmax=maxx(rs.sum,rs.rmax,rs.sum+ls.rmax);
 85         ans.sum=ls.sum+rs.sum;
 86         return ans;
 87     }
 88 }
 89 int main()
 90 {
 91     int n,m;
 92     cin>>n>>m;
 93     build(1,n,1);
 94     int i,j,t;
 95     for(i=0;i<m;i++)
 96     {
 97         scanf("%d",&j);
 98         if(j==1)
 99         {
100             int a,b;
101             scanf("%d%d",&a,&b);
102             if(a>b)
103                 swap(a,b);
104             node ans=query(1,a,b);
105             printf("%d\n",ans.dat);
106         }
107         else
108         {
109             int x,y;
110             scanf("%d%d",&x,&y);
111             update(1,x,y);
112         }
113     }
114 }
115 
116 /*
117 5 3
118 1 2 -3 4 5
119 1 2 3
120 2 2 -1
121 1 3 2
122 */

 




Guess you like

Origin www.cnblogs.com/Spring-Onion/p/11322976.html