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

Problem B SPOJ DCEPC11I

时间:2014-08-11 08:22:01      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   strong   

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
•难点:标记记录什么,标记如何下传。
 
bubuko.com,布布扣
#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

 

Problem B SPOJ DCEPC11I,布布扣,bubuko.com

Problem B SPOJ DCEPC11I

标签:des   style   blog   http   color   os   io   strong   

原文地址:http://www.cnblogs.com/Run-dream/p/3903896.html

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