LightOJ--1164--区间查询与求和

World is getting more evil and it's getting tougher to get into the Evil League of Evil. Since the legendary Bad Horse has retired, now you have to correctly answer the evil questions of Dr. Horrible, who has a PhD in horribleness (but not in Computer Science). You are given an array of n elements, which are initially all 0. After that you will be given q commands. They are -

1.      0 x y v - you have to add v to all numbers in the range of x to y (inclusive), where x and y are two indexes of the array.

2.      1 x y - output a line containing a single integer which is the sum of all the array elements between x and y (inclusive).

The array is indexed from 0 to n - 1.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case contains two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). Each of the next q lines contains a task in one of the following form:

0 x y v (0 ≤ x ≤ y < n, 1 ≤ v ≤ 1000)

1 x y (0 ≤ x ≤ y < n)

Output

For each case, print the case number first. Then for each query '1 x y', print the sum of all the array elements between x and y.

Sample Input
2
10 5
0 0 9 10
1 1 6
0 3 7 2
0 4 5 1
1 5 5
20 3
0 10 12 1
1 11 12
1 19 19
Sample Output
Case 1:
60
13
Case 2:
2
0

题意: 1--查询x到y区间的和;

           0--更新x到y区间的数据,全部加为z;

思路:https://blog.csdn.net/queque_heiya/article/details/104310596

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 10000+10
#define LL long long
LL sum[N],lazy[N];
///向上更新求和
void pushUp(int root){
    sum[root]=sum[root<<1] + sum[(root<<1)+1];
}
///向下更新求和
void pushDown(int root,int len){
    lazy[root<<1]+=lazy[root];
    lazy[(root<<1)+1]+=lazy[root];
    sum[root<<1]+=(len-(len>>1))*lazy[root];
    sum[(root<<1)+1]+=(len>>1)*lazy[root];
    lazy[root]=0;
}
///建树
void build(int l,int r,int root){
    lazy[root]=0;
    if(l==r){
        sum[root]=0;
        return;
    }
    int m=(l+r)>>1;
    build(l,m,root<<1);
    build(m+1,r,(root<<1)+1);
    pushUp(root);
}
///在(l,r)区间内,根节点为root,将(L,R)区间的每个数 +c
void update(int L,int R,long long c,int l,int r,int root){
   if(L<=l&&R>=r){
        lazy[root]+=c;
        sum[root]+=c*(r-l+1);
        return;
    }
    if(lazy[root]) pushDown(root,r-l+1);
    int m=(l+r)>>1;
    if(L<=m) 
		update(L,R,c,l,m,root<<1);
    if(R>m) 
		update(L,R,c,m+1,r,(root<<1)+1);
    pushUp(root);
}
///在(l,r)区间内,根节点为root,求(L,R)区间的数的和
LL query(int L,int R,int l,int r,int root){
    if(L<=l&&R>=r) return sum[root];
    if(lazy[root]){
        pushDown(root,r-l+1);
    }
    int m=(l+r)>>1;
    LL ans=0;
    if(L<=m) ans+=query(L,R,l,m,root<<1);
    if(R>m) ans+=query(L,R,m+1,r,(root<<1)+1);
    return ans;
}
int main(){
	int t,k=1;
	scanf("%d",&t);
	while(t--){
		int n,m;
		printf("Case %d:\n",k++);
		scanf("%d",&n);
		build(1,n,1);
		scanf("%d",&m);
		while(m--){
			int x,y,z,ch;
			scanf("%d",&ch);
			if(ch==0){
				scanf("%d%d%d",&x,&y,&z);
				x++;
				y++;
				update(x,y,z,1,n,1);
			}else{
				scanf("%d%d",&x,&y);
				x++;
				y++;
				printf("%lld\n",query(x,y,1,n,1));
			}
		}
	}
}
发布了150 篇原创文章 · 获赞 73 · 访问量 6575

猜你喜欢

转载自blog.csdn.net/queque_heiya/article/details/104310631