Problem B SPOJ DCEPC11I

Description

Vaibhav Sir and Saikat Sir, after completing their graduation, got a job together at a store.

Their boss at the store was Sidharth Sir, who was very strict and did not want anyone to have fun at the job. He gave Vaibhav and Saikat sir a very boring job of stacking columns of boxes in a row of size N.

To make it a little interesting, both of them decided that whenever they need to add more boxes, they will choose st and en, such that 1<=st<=en<=N, and place 1 box in column st, 2 boxes in column st+1, … and (en-st+1) boxes in column en.

When Sidharth sir saw this, he decided to have some fun of his own, and asked them to count the number of boxes in all columns from st to en, and tell him the result. Now Vaibhav and Saikat sir have come to you for help to answer the queries of their boss, as they do not want to lose their jobs.

Input

The first line contains N, the number of columns and Q, the number of queries.

The next Q lines can be of the following form –

1)      0 st en, meaning Vaibhav and Saikat sir add boxes to the columns from st to en as described above.

2)      1 st en, meaning Sidharth sir asks them to count the number of boxes in all columns from st to en.

Output

For all queries of type 2, output a line containing the answer to the query.

Example

Input: 5 6
0 2 4
1 1 5
0 1 5
0 3 5
1 1 5
1 3 5 Output: 6
27
23

Constraints:

1<=N, Q<=100000

 

  线段树
•题意: 对一个长为N(N≤100000)的序列A[]进行Q(Q≤100000)次操作,2种操作:
•(1)“0  st  en” :  A[st]增加1 , A[st+1]增加2 ...... A[en]增加 en - st + 1 .
•(2)“1  st  en” :  询问区间和A[st] + A[st+1] + ..... + A[en]
•典型的线段树成段更新,成段查询。
•利用到的性质:两个等差数列对应项相加得到的任是等差数列。
•因此“懒惰标记”只需要记录首项a[]和公比d[]即可。
•要记得等差数列求和公式。
•数据类型的范围long long
•难点:标记记录什么,标记如何下传。
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL ;
const int maxn = 100005 ;

#define Lson o<<1, L, mid
#define Rson o<<1|1 , mid+1 , R

LL a[maxn<<2] , d[maxn<<2] , sum[maxn<<2] ;

void pushup(int o){
    sum[o] = sum[o<<1] + sum[o<<1|1] ;
}
void pushdown(int o , int c1 , int c2) {
    if(a[o] || d[o]) {
        LL a1 = a[o] , a2 = a[o] + (LL)d[o] * c1 ;
        sum[o<<1] += a1*c1 + c1*(c1-1)*d[o]/2 ;
        sum[o<<1|1] += a2*c2 + c2*(c2-1)*d[o]/2;
        a[o<<1] += a1 , a[o<<1|1] += a2 ;
        d[o<<1] += d[o] , d[o<<1|1] += d[o] ;
        a[o] = d[o] = 0 ;
    }
}

int l , r ;
void update(int o ,int L ,int R){
    if(l<=L && R<=r) {
        LL a1 = L - l + 1 , c1 = R - L + 1 ;
        sum[o] += a1*c1 + c1*(c1-1)/2;
        a[o] += a1 , d[o]++ ;
        return ;
    }
    int mid = (L+R)>>1;
    pushdown(o, mid-L+1 , R-mid);
    if(l<=mid) update(Lson) ;
    if(r>mid)  update(Rson) ;
    pushup(o);
}

LL query(int o ,int L ,int R) {
    if(l<=L && R<=r) return sum[o];
    int mid = (L+R)>>1 ;
    pushdown(o , mid-L+1 , R-mid) ;
    LL ret = 0 ;
    if(l<=mid) ret += query(Lson) ;
    if(r> mid) ret += query(Rson) ;
    return ret;
}

int main()
{
    int N , Q;
    scanf("%d%d", &N ,&Q);
    while(Q--){
        int op ;
        scanf("%d%d%d", &op, &l ,&r) ;
        if(op == 0) update(1, 1 , N) ;
        else printf("%lld\n" , query(1, 1 ,N) ) ;
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/Run-dream/p/3903896.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值