码迷,mamicode.com
首页 > 其他好文 > 详细

LightOJ 1164 - Horrible Queries(线段树啊 功能:区间增减和区间求和)

时间:2015-03-29 10:56:21      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:lightoj   线段树   

题目链接:http://lightoj.com/volume_showproblem.php?problem=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

Output for 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

Case 1:

60

13

Case 2:

2

0




代码如下:

#include <cstdio>
#include <algorithm>
using namespace std;
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define LL __int64
const LL maxn = 111111;
LL add[maxn<<2];
LL sum[maxn<<2];//求和
void PushUp(LL rt)
{
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void PushDown(LL rt,LL len)
{
    if (add[rt])
    {
        add[rt<<1] += add[rt];
        add[rt<<1|1] += add[rt];

        sum[rt<<1] += add[rt] * (len - (len >> 1));
        sum[rt<<1|1] += add[rt] * (len >> 1);//更新右儿子的和
        add[rt] = 0;
    }
}
void build(LL l,LL r,LL rt)
{
    add[rt] = 0;//初始化为所有结点未被标记
    if (l == r)
    {
        //scanf("%lld",&sum[rt]);
        sum[rt] = 0;
        return ;
    }
    LL mid = (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);//更新代表某个区间的节点和,该节点不一定是叶子节点
        return ;
    }
    PushDown(rt , r - l + 1);
    LL mid = (l + r) >> 1;
    if (L <= mid)
        update(L , R , c , lson);//更新左儿子
    if (mid < 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);
    LL mid = (l + r) >> 1;
    LL ret = 0;
    if (L <= mid)
        ret += query(L , R , lson);
    if (mid < R)
        ret += query(L , R , rson);
    return ret;
}
int main()
{
    LL N , Q;
    LL t;
    LL cas = 0;
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld%lld",&N,&Q);//N为节点数
        build(1 , N , 1); //建树
        printf("Case %d:\n",++cas);
        while (Q--)//Q为询问次数
        {
            //char op[2];
            LL op;
            LL a , b , c;
            scanf("%lld",&op);
            if(op == 1)
            {
                scanf("%lld%lld",&a,&b);
                printf("%lld\n",query(a+1 , b+1 , 1 , N , 1));
            }
            else if(op == 0)
            {
                scanf("%lld%lld%lld",&a,&b,&c);//c为区间a到b增加的值
                update(a+1 , b+1 , c , 1 , N , 1);
            }
        }
    }
    return 0;
}
/*
2
10 5
0 0 9 10
1 0 6
0 3 7 2
0 4 5 1
1 5 5
20 3
0 10 12 1
1 11 12
1 19 19
*/


LightOJ 1164 - Horrible Queries(线段树啊 功能:区间增减和区间求和)

标签:lightoj   线段树   

原文地址:http://blog.csdn.net/u012860063/article/details/44724541

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!