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

来源:互联网 发布:电影下载 知乎 编辑:程序博客网 时间:2024/05/16 17:05

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


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

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 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample Output

455915
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 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4*/


0 0