POJ 3468 A Simple problem with integers segment tree

Source: Internet
Author: User

Description

Given a sequence, you need to deal with the following two types of queries.

"C A B C" indicates that the values in the [a, b] interval are all increased by C ( -10000≤c≤10000).

"Q a B" asks for the and of all values in the [a, b] interval.

Input

The first line consists of two integers n, Q. 1≤n,q≤100000.

The second line contains n integers that represent the initial sequence a ( -1000000000≤ai≤1000000000).

Next Q Line asks, format as title description.

Output

For each Q start query, you need to output the corresponding answer, one row for each answer.

Sample Input

2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample Output

455915

Line tree problem, note that the data is relatively important to define the variable with a long long.

#include <cstring> #include <cstdio>using namespace std;struct p{long long x,y,z,v;};    P Tree[1000000];long Long A[100010],sum;long long build (long long L,long long R,long long p) {tree[p].x=l;    Tree[p].y=r;    TREE[P].V = 0;    if (l==r) return tree[p].z=a[l];    Long Long m= (l+r)/2;    Long Long A=build (l,m,2*p);    Long Long B=build (m+1,r,2*p+1); return tree[p].z=a+b;} Long long un (long long l,long long R,long long Z,long long p) {if (tree[p].x==l&&tree[p].y==r) {tree[p].v+=z; t    ree[p].z+=z* (r-l+1); return tree[p].z;}        if (TREE[P].V) {tree[2*p].v+=tree[p].v;        tree[2*p].z+=tree[p].v* (tree[2*p].y-tree[2*p].x+1);        TREE[2*P+1].V+=TREE[P].V;        tree[2*p+1].z+=tree[p].v* (tree[2*p+1].y-tree[2*p+1].x+1);    tree[p].v=0;    } long m= (TREE[P].X+TREE[P].Y)/2;    if (l>m) return Tree[p].z=un (l,r,z,2*p+1) +tree[2*p].z;    if (m>=r) return Tree[p].z=un (l,r,z,2*p) +tree[2*p+1].z; Return Tree[p].z=un (l,m,z,2*p) +un (m+1,r,z,2*p+1);}    void find (Long long l,long long R,long long p) {if (tree[p].x==l&&tree[p].y==r) {Sum+=tree[p].z;return;}        if (TREE[P].V) {tree[2*p].v+=tree[p].v;        tree[2*p].z+=tree[p].v* (tree[2*p].y-tree[2*p].x+1);        TREE[2*P+1].V+=TREE[P].V;        tree[2*p+1].z+=tree[p].v* (tree[2*p+1].y-tree[2*p+1].x+1);    tree[p].v=0;    } long m= (TREE[P].X+TREE[P].Y)/2;    if (l>m) find (l,r,2*p+1);    else if (m>=r) find (l,r,2*p);        else {find (l,m,2*p);    Find (m+1,r,2*p+1); } return;    int main () {Long long n,q,i,x,y,z;    char s;        while (~SCANF ("%lld%lld", &n,&q)) {for (i=1;i<=n;i++) scanf ("%lld", &a[i]);        Build (1,n,1);            while (q--) {//getchar ();            scanf ("%c", &s);                if (s== ' C ') {scanf ("%lld%lld%lld", &x,&y,&z);            Un (x,y,z,1); } else {scanf ("%lld%lld", &AMp;x,&y);                sum=0;                Find (x,y,1);            printf ("%lld\n", sum); }}} return 0;}

  

POJ 3468 A Simple problem with integers segment tree

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.