poj-3468a simple Problem with integers (segment tree changes to partial values and summation)

Source: Internet
Author: User
A simple Problem with integers
Time Limit: 5000MS Memory Limit: 131072K
Total submissions: 96612 accepted: 30145
Case Time Limit: 2000MS

Description

You have N integers, A1, A2, ..., an. You are need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other was to ask for the sum of numbers in a given interval.

Input

The contains two numbers N and Q. 1≤n,q≤100000.
The second line contains N numbers, the initial values of A1, A2, ..., an. -1000000000≤ai≤1000000000.
Each of the next Q lines represents a operation.
"C A B C" means adding C to each of Aa, aa+1, ..., Ab. -10000≤c≤10000.
"Q a B" means querying the sum of Aa, aa+1, ..., Ab.

Output

You are need to answer all Q commands in order. One answer in a line.

Sample Input

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

Sample Output

4
9
15

Hint the sums may exceed the range of 32-bit integers.

Source POJ monthly--2007.11.25, Yang Yi

#include <iostream> #include <string> #include <cstring> #include <cstdio> #define Lson l,m,rt
<<1 #define Rson m+1,r,rt<<1|1 #define N 111111 #define LL __int64 using namespace std; ll add[n<<2];//save Change the size of the value ll sum[n<<2];//save and void Pushup (ll RT) {Sum[rt]=sum[rt<<1]+sum[rt<<1|1
]; } void Pushdown (LL rt,ll m) {if (Add[rt]) {add[rt<<1]+=add[rt];//Saozi the total amount of change that needs to be changed does not directly pass each variable to each node. Just first mark the last
		The sum of the final change in the size of the speed add[rt<<1|1]+=add[rt];
		sum[rt<<1]+=add[rt]* (M (m>>1));//Change the sum value of the left subtree node sum[rt<<1|1]+=add[rt]* (m>>1);
	add[rt]=0;//clear the tag of this node} void Build (LL l,ll r,ll RT) {add[rt]=0;
		if (l==r) {scanf ("%i64d", &sum[rt]);
	Return
	LL m= (l+r) >>1;
	Build (Lson);
	Build (Rson);
Pushup (RT);
		} void Update (LL l,ll r,ll c,ll l,ll r,ll RT) {if (l<=l&&r>=r) {add[rt]+=c;
	sum[rt]+= (LL) c* (r-l+1);//r-l+1 indicates the number of nodes R to l interval return; } pushdown (rt,r-l+1);//Update child node LL m= (l+r) >>1;
	if (l<=m) update (L,r,c,lson);
	if (m<r) update (L,r,c,rson);
Pushup (RT);
	ll query (ll l,ll R,LL l,ll r,ll RT) {if (l<=l&&r>=r) {return SUM[RT];
	} pushdown (RT,R-L+1)//push tag down with new to each sum or else it will go wrong ll m= (l+r) >>1;
	LL res=0;
	if (m>=l) {res+=query (L,r,lson);
	} if (M<r) {res+=query (L,r,rson);
return res;
	int main () {LL n,q;
	scanf ("%i64d%i64d", &n,&q);
	Build (1,n,1);
		while (q--) {char op[2];
		LL A,b,c;
		scanf ("%s", op);
			if (op[0]== ' Q ') {scanf ("%i64d%i64d", &a,&b);
		printf ("%i64d\n", Query (a,b,1,n,1));
			else {scanf ("%i64d%i64d%i64d", &a,&b,&c);
		Update (a,b,c,1,n,1);
} return 0;  }


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.