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 60 2 41 1 50 1 50 3 51 1 51 3 5 Output: 62723

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;
}

Problem B SPOJ DCEPC11I

时间: 2024-08-28 02:00:44

Problem B SPOJ DCEPC11I的相关文章

Problem A SPOJ SUB_PROB

Description String Matching is an important problem in computer science research and finds applications in Bioinformatics, Data mining,pattern recognition, Internet security and many more areas. The problem we consider here is a smaller version of it

SPOJ DCEPC11I

题目大意: 就是给定一段区间令其中的数增加一个递增序列(也就是说第一个+1,第二个+2.....) 询问操作是区间的和 这里的查询很简单,但是对于添加递增序列入区间就比较搞脑子了 我们需要一个add[]作为区间的首个数字增加的值,del[]表示等差数列的公差,因为你每次添加进入一个等差数列,是可以叠加的但公差变为了del[ls]+=del[o] 这里主要是pushdown函数的写法 我们要用到等差公式的求和:S =a*n+n(n-1)*d/2 void push_down(int o,int x

Problem E SPOJ ROCK

Description A manufacturer of sweets has started production of a new type of sweet called rock. Rock comes in sticks composed of one-centimetre-long segments, some of which are sweet, and the rest are sour. Before sale, the rock is broken up into sma

CSU-ACM暑假集训基础组训练赛(4)解题报告

•Problem A SPOJ SUB_PROB   AC自动机 •题意: 给定一个长为M(M≤100000 )的文本串,和N(N≤1000)个长度不超过2000的模式串,问每个模式串是否在文本串中出现过? •几乎和周一课件上的第一个例题一模一样.. •把文本串丢到AC自动机里面去跑. •注意: •1.可能有两个相同的模式串(略坑吧.) •2.一个模式串可能是另一个模式串的后缀,即如果一个点的fail指针指向的点是一个“危险节点”,那么它本身也是一个“危险节点”. 1 #include <ios

SPOJ GNYR09F 数字上的找规律DP

Problem C      SPOJ GNYR09F dp题,dp可能刚刚开始对大家来说比较难,但是静下心来分析还是比较简单的: dp(i ,j ,k)表示前i个数中,当前累积和为j,末尾数字为k的方案数. 考虑第i个位置的2种情况: 1.放0:dp(i,j,0) = dp(i-1,j,0) + dp(i-1,j,1) 2.放1:dp(i,j,1)= dp(i-1,j,0) 因为每一行最开始的状态均从i=j+1,dp(i,j,0)=0,dp(i,j,1)=1开始的,但因为这样子开头均为1,那些

Spoj PRIME1 - Prime Generator

题意翻译 求给定的两个数之间的素数 Translated by @kaiming 题目描述 Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! 输入输出格式 输入格式: The input begins with the number t of test cas

暑假集训-个人赛第四场

ID Origin Title   10 / 52 Problem A SPOJ AMR10A Playground     Problem B SPOJ AMR10B Regex Edit Distance     Problem C SPOJ AMR11C Robbing Gringotts   1 / 14 Problem D SPOJ AMR10D Soccer Teams   0 / 3 Problem E SPOJ AMR10E Stocks Prediction   17 / 19

数论十题

数论十题 Problem Zero:[neerc2011]Gcd guessing game 现在有一个数x,1 ≤ x≤ n,告诉你n,每次你可以猜一个数y,如果x==y则结束,否则返回gcd(x,y),问最少只要几次就可以保证猜出答案. 本题纯属娱乐.仅仅是一个GCD的游戏,跑题了. 因为本题要求最坏情况,我们直观地猜想就是每次返回都是1.由于答案有可能是质数,而判定一个数,必须要把含有这个质因子的数问一遍.于是,我们引出这样一个思路,将所有1-n的质数分组,每组的积<=n,答案就是组数.

CSU-ACM暑假集训基础组七夕专场

•Problem A Codeforces 20C       最短路(dij,spfa) •题意:给出一张n个点m条边的无向图 (2 ≤ n ≤ 105, 0 ≤ m ≤ 105),输出从1到n的任意一条最短路径. •解法:dijkstra或者spfa,用pre数组记录到达每个点最短距离的前驱结点. •注意:路径的长度范围是 1 ≤ wi ≤ 106,从1到n的最短路径长度可能超出int范围. •没有路径从1到达n时要输出-1 1 #include <cstdio> 2 #include &