大二训练第二周 A Simple Problem with Integers 线段树

区间更新,一开始 想着单点根新加个循环,果断t了,如果这样就会是n^2logn的复杂度。所以要区间更新,要有个延迟标记,等到下一次查询的时候更新。//要用long long 同样的代码G++太费时间。。。。


B - A Simple Problem with Integers
Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u

Description

给出了一个序列,你需要处理如下两种询问。

"C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000)。

"Q a b" 询问[a, b]区间中所有值的和。

Input

第一行包含两个整数N, Q。1 ≤ N,Q ≤ 100000.

第二行包含n个整数,表示初始的序列A (-1000000000 ≤ Ai ≤ 1000000000)。

接下来Q行询问,格式如题目描述。

Output

对于每一个Q开头的询问,你需要输出相应的答案,每个答案一行。

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15
ACcode:

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%I64d",&x)
#define rd2(x,y) scanf("%I64d%I64d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
using namespace std;
ll tree[maxn<<2],add[maxn<<2];
void pushdown(int left,int right,int st){
    if(add[st]){
        int m=(left+right)>>1;
        int temp=st<<1;
        add[temp]+=add[st];
        add[temp|1]+=add[st];
        tree[temp]+=(m-left+1)*add[st];
        tree[temp|1]+=(right-m)*add[st];
        add[st]=0;
    }
}
void creatTree(ll st,ll left,ll right){
    tree[st]=add[st]=0;
    if(left==right){
        cin>>tree[st];
        return ;
    }
    ll m=(left+right)>>1;
    ll temp=st<<1;
    creatTree(temp,left,m);
    creatTree(temp|1,m+1,right);
    tree[st]=tree[temp]+tree[temp|1];
}
void updata(ll st,ll left,ll right,ll L,ll R,ll ad){
    if(L<=left&&R>=right){
        tree[st]+=(right-left+1)*ad;
        add[st]+=ad;
        return;
    }
    int m=(left+right)>>1;
    int temp=st<<1;
    pushdown(left,right,st);
    if(L<=m)
        updata(temp,left,m,L,R,ad);
    if(R>m)
        updata(temp|1,m+1,right,L,R,ad);
    tree[st]=tree[temp]+tree[temp|1];
}
ll qurey(ll st,ll left,ll right,ll L,ll R){
    if(L<=left&&R>=right)return tree[st];
    ll m=(left+right)>>1;
    ll temp=st<<1;
    pushdown(left,right,st);
    ll ret=0;
    if(L<=m)ret+=qurey(temp,left,m,L,R);
    if(R>m)ret+=qurey(temp|1,m+1,right,L,R);
    return ret;
}
char q[4];
ll n,loop;
int main(){
    scanf("%I64d %I64d",&n,&loop);
    creatTree(1,1,n);
    FOR(i,1,loop){
        rds(q);
        if(q[0]=='C'){
            ll x,y,ad;
            scanf("%I64d %I64d %I64d",&x,&y,&ad);
            updata(1,1,n,x,y,ad);
        }
        else {
            ll x,y;
            scanf("%I64d %I64d",&x,&y);
            printf("%I64d\n",qurey(1,1,n,x,y));
        }
    }
    return 0;
}
/*
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
*/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值