# 10:challenge 3 (tree-like array directly modified)

Source: Internet
Author: User

Total time limit:
10000ms
Single test point time limit:
1000ms
Memory Limit:
262144kB
Describe

to a series of n long, there are m operations, each operation is one of the following two:

(1) Modify a number in the series

(2) To find a continuous paragraph in a series and

Input
The first line is two positive integers n and M.
The second row, n integers, represents the sequence.
Next M line, each line begins with a character, if the character is ' M ', it represents a modification operation, the next two integers x and Y, indicating that the value of the X position is modified to Y, if the character is ' Q ', then a query operation, the next two integers x and y, for the [x, Y] interval of the and.
Output
output a single line for each query operation, indicating the answer.
Sample input
`5 to 2 3 4 5Q 1 5M 2 7Q 1 5`
Sample output
`1520`
Tips
1<=n<=10^5,1<=m<=10^5, the input is guaranteed to be valid, and all integers and answers are available with signed 32-bit integer storage.
There must be no doubt about the tree-like array,
but this is not a plus or minus, but a direct modification,
However, direct modification will explode zero, the reason for their yy to know.
So, every time we change, we add the difference between the number and the current number, and then we change the current number to the number we're going to change
.
`1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intmaxn=100001;6 intA[MAXN];7 intTREE[MAXN];8 intn,m;9 voidReadint&N)Ten { One     CharC='+';intx=0;BOOLflag=0; A      while(c<'0'|| C>'9') {C=getchar ();if(c=='-') flag=1;} -      while(c>='0'&&c<='9') -X= (x<<1) + (x<<3) +c- -, c=GetChar (); theflag==1? n=-x:n=x; - } - intlbintp) - { +     return(p) & (-p); - } + voidAddintPosintv) A { at     intp=Pos; -      while(p<=N) -     { -tree[p]+=v; -p+=lb (p); -     } in } - intSumintPOS) to { +     intans=0; -     intp=Pos; the      while(P) *     { \$ans+=Tree[p];Panax Notoginsengp-=lb (p); -     } the     returnans; + } A intMain () the { + read (n); read (m); -      for(intI=1; i<=n;i++) \$     { \$         intp; - read (p); -a[i]=p; the Add (i,p); -     }Wuyi      for(intI=1; i<=m;i++) the     { -         CharCintx, y; WuCin>>c;read (x); Read (y); -         if(c=='Q') Aboutprintf"%d\n", sum (y)-sum (X-1)); \$         Else -         { -Add (x,y-a[x]); -a[x]=y; A         } +              the     } -     return 0; \$}`

10:challenge 3 (tree-like array directly modified)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.