首页 > 代码库 > E - A Simple Problem with Integers

E - A Simple Problem with Integers

#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>using namespace std;#define N 100002struct node{    int l,r;    long long lz,w;}q[4*N];void pushup(int rt){    q[rt].w=q[rt*2].w+q[rt*2+1].w;}void pushdown(int rt,int m){    if(q[rt].lz)    {        q[rt*2].lz+=q[rt].lz;        q[rt*2+1].lz+=q[rt].lz;        q[rt*2].w+=(m-m/2)*q[rt].lz;        q[rt*2+1].w+=(m/2)*q[rt].lz;        q[rt].lz=0;    }}void build(int l,int r,int rt){    q[rt].l=l;    q[rt].r=r;    q[rt].lz=0;    q[rt].w=0;    if(l==r)    {        scanf("%lld",&q[rt].w);        return ;    }    build(l,(r+l)/2,rt*2);    build((r+l)/2+1,r,rt*2+1);    pushup(rt);}void update(int key,int ll,int rr,int l,int r,int rt){    if(ll<=l&&rr>=r)    {        q[rt].w+=(long long)key*(r-l+1);        q[rt].lz+=key;        return ;    }    pushdown(rt,r-l+1);    int m=(r+l)/2;    if(ll<=m)    update(key,ll,rr,l,m,rt*2);    if(rr>m)    {        update(key,ll,rr,m+1,r,rt*2+1);    }    pushup(rt);}long long query(int ll,int rr,int l,int r,int rt){    if(ll<=l&&rr>=r)    {        return q[rt].w;    }    pushdown(rt,r-l+1);    int m=(r+l)/2;    long long L=0;    if(ll<=m) L+=query(ll,rr,l,m,rt*2);    if(rr>m) L+=query(ll,rr,m+1,r,rt*2+1);    return L;}int main(){    int n,m;    while(scanf("%d%d",&n,&m)!=EOF)    {        build(1,n,1);        while(m--)        {            char op[10];            int a , b , c;            scanf("%s",op);            if (op[0] == Q)            {                scanf("%d%d",&a,&b);                long long tt=query(a,b,1,n,1);                printf("%lld\n",tt);            }            else if(op[0]==C)            {                scanf("%d%d%d",&a,&b,&c);                update(c,a,b,1,n,1);            }        }    }    return 0;}