Codevs 1080 Segment Tree Contact

Source: Internet
Author: User
Title Description Description

A row of n squares, beginning with an integer in each lattice. Some questions and modifications are presented dynamically: the form of questioning is the summation of all elements in a particular sub-interval [a, b]; the modified rule is to specify a lattice x, plus or minus a specific value, a. You are now asked to make the right answer to each question. 1≤n<100000, the total number of questions and modifications m<10000. Enter a description input Description

The input file first behaves as an integer n, followed by n rows n integers, representing the original integers in the lattice. followed by a positive integer m, followed by M-line, the M-query, the first integer to indicate the query code, the number 1 indicates an increase, the following two numbers x and a for the value on position x to increase a, ask the code 2 for the interval sum, followed by two integers for A and B, representing the interval between [A, a] and Outputs description Output Description

A total of M-lines, each integer sample entered as Sample input

6

4

5

6

2

1

3

4

1 3 5

2 1 4

1 1 9

2 2 6 sample outputs sample output

22

22 data range and hints, size & Hint

1≤n≤100000, m≤10000.

Tree-like array:

1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 int n,m,t[1000];
 6 void Add (int k,int z)
 7 {
 8     while (k<=n)
 9     {         t[k]+=z;         k+=k& (-K);
(int x)-{+-
int '     ans=0;     (x) (
ans+=t[x)         ;         x-=x& (-X);
*     *     return ans;
$
int main ()
{     cin>>n;     i=1;i<=n;i++ for (int) (int.)         x;         cin>>x;         Add (i,x);     cin>>m;     i=1;i<=m;i++ for (int),     {         a,b,c,d,e int;
Panax Notoginseng         cin>>a;         if (a==1) {             cin>>b>>c;             Add (b,c);         (a==2) (+) (
+             cin>>d>>e;             printf ("%d\n", find (E)-find (d-1));
(         +
}     ), and return 0;
50}
View Code

Line segment Tree:

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std;
 5 int n,m;
 6 struct node{7 int l,r,sum;
 8};
9 node tr[100010*4]; Ten void built (int l,int r,int k)//build one {tr[k].l=l;tr[k].r=r; if (l==r) {scanf ("%d", &tr[k].sum); return;
} mid= Int (l+r)/2;
Built (l,mid,k*2); built (mid+1,r,k*2+1);
Tr[k].sum=tr[k*2].sum+tr[k*2+1].sum;    + void Change (int k,int pos,int x), {$ int l=tr[k].l;int r=tr[k].r;
if (l==r) {tr[k].sum=tr[k].sum+x;return;} int mid= (L+R)/2;
if (pos<=mid) change (k*2,pos,x);
if (pos>mid) change (k*2+1,pos,x);
Tr[k].sum=tr[k*2].sum+tr[k*2+1].sum;      30} int que (int k,int l,int R) (int.) {ans=0, if (l==tr[k].l&&tr[k].r==r) {return tr[k].sum;}
int mid= (TR[K].L+TR[K].R)/2;
if (l<=mid) Ans+=que (K*2,l,min (R,mid));
if (r>mid) Ans+=que (K*2+1,max (mid+1,l), R);
return ans; 36 {37} int main ()     scanf ("%d", &n);
Built (1,n,1);
scanf ("%d", &m);         Max for (int i=1;i<=m;i++) $ {a,x,a,d,c int, cin>>a; if (a==1) 45             {cin>>x>>a; (1,x,a); (a==2) 50 {51
cin>>c>>d;
printf ("%d\n", Que (1,c,d));
+ 0};  56}
View Code

Contact Us

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.