Acm--a simple problem with integers (the essence version of the 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

Explanation: The difficulty of this problem is mainly how to be able to update the value of a region in a timely manner without time- out ( because each update results will cost a lot of time ), so this problem
It is necessary to use the essence of line tree: Delay mark


The delay tag is mainly to help mark whether a certain value has been updated, if there is an update, then only in the time of access to the interval, the delay mark will accumulate his
The value is passed to the following node, which plays a time-saving role.

1#include <cstdio>2#include <cstring>3#include <iostream>4 5 using namespacestd;6 7 Const intmaxn=500000;8 9 structStudentTen { One     intLeft,right; A __int64 NSum; - __int64 Mark; -}segtree[maxn*4]; the__int64 NUM[MAXN];//__int64 is a dedicated Windows-only type that is designed to record very large data, preventing out-of-bounds -  -  -  + voidBuild (intIndexintLintR//Achievements - { +segtree[index].left=l; Asegtree[index].right=R; at     if(segtree[index].left==segtree[index].right) -     { -segtree[index].nsum=num[l-1]; -         return ; -     } -     intMid= (L+R)/2; inBuild (2*index,l,mid); -Build (2*index+1, mid+1, R); tosegtree[index].nsum=segtree[index*2].nsum+segtree[index*2+1].nsum;//each node and + } -  the  *  $ Panax Notoginseng  - voidADD (intIintLeftintRightintb) the { +     if(segtree[i].left>=left&&segtree[i].right<=Right ) A     { thesegtree[i].nsum+= (segtree[i].right-segtree[i].left+1) *b;//Multiplier plus +Segtree[i]. Mark+=b;//with + = reason is that a paragraph may be marked multiple times -         return ; $     }     $     Else -     { -         if(Segtree[i]. Mark)//The most critical part of this is the tag, which adds Val to his left and right children. the         { -segtree[i*2].nsum+=segtree[i]. Mark* (segtree[i*2].right-segtree[i*2].left+1);Wuyisegtree[i*2]. mark+=Segtree[i]. Mark; thesegtree[i*2+1].nsum+=segtree[i]. Mark* (segtree[i*2+1].right-segtree[i*2+1].left+1); -segtree[i*2+1]. mark+=Segtree[i]. Mark; WuSegtree[i]. mark=0; -         } About         if(right>=segtree[i*2+1].left) $ADD (2*i+1, left,right,b); -         if(left<=segtree[i*2].right) -ADD (2*i,left,right,b);  -  Asegtree[i].nsum=segtree[i*2].nsum+segtree[i*2+1].nsum; +     } the  -  $ } the  the  the  the__int64 Query (intIintLeftintRight ) - { in     if(segtree[i].left>=left&&segtree[i].right<=Right ) the             returnsegtree[i].nsum; the     if(Segtree[i]. Mark)//the sum also needs to be labeled About         { thesegtree[i*2].nsum+=segtree[i]. Mark* (segtree[i*2].right-segtree[i*2].left+1); thesegtree[i*2]. mark+=Segtree[i]. Mark; thesegtree[i*2+1].nsum+=segtree[i]. Mark* (segtree[i*2+1].right-segtree[i*2+1].left+1); +segtree[i*2+1]. mark+=Segtree[i]. Mark; -Segtree[i]. mark=0; the         }Bayi         intMid= (segtree[i].left+segtree[i].right)/2; the__int64 max1=0, max2=0; the     if(mid<Left ) -Max1= Query (2*i+1, left,right); -     Else the         if(right<=mid) theMax1=query (2*i,left,right); the         Else the             { -Max1=query (2*i,left,mid); theMax2=query (2*i+1, mid+1, right); the             } the     returnmax1+Max2;94  the  the  the }98  About  - 101 102 103 intMain ()104 { the     intn,t;106     intx, y, z107 108     CharC;109      while(SCANF ("%d%d", &n,&t)! =EOF) the     {111memset (Segtree,0,sizeof(Segtree)); the          for(intI=0; i<n; i++)113scanf"%i64d",&num[i]); theBuild (1,1, n); the          for(intj=0; j<t;j++) the         {117 GetChar ();118scanf"%c%d%d",&c,&x,&y);119  -             if(C = ='C')121             {122scanf"%d",&z);123ADD (1, x, Y, z);124             } the             Else 126printf"%i64d\n", Query (1, x, y)); 127         } -     129     }     the     return 0;131}

Acm--a simple problem with integers (the essence version of the 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.