E - A Simple Problem with Integers

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
#define N 100002
struct 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;

}

E - A Simple Problem with Integers,布布扣,bubuko.com

时间: 2024-10-19 22:33:52

E - A Simple Problem with Integers的相关文章

POJ 3468 A Simple Problem with Integers

链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 77302 Accepted: 23788 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two ki

poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 83959   Accepted: 25989 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. Yo

poj3468 A Simple Problem with Integers 线段树区间更新

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97722   Accepted: 30543 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ 3468 A Simple Problem with Integers(树状数组区间更新)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97217   Accepted: 30358 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ-3468 A Simple Problem with Integers(线段树)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 94899   Accepted: 29568 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

Poj 3468 A Simple Problem with Integers(线段树 区间更新 延迟标记)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 73239   Accepted: 22607 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

A Simple Problem with Integers 多树状数组分割,区间修改,单点求职。 hdu 4267

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4032    Accepted Submission(s): 1255 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

hdu 4267 A Simple Problem with Integers

题目链接:hdu 4267 A Simple Problem with Integers 类似于题目:hdu 1556 Color the ball 的技巧实现树状数组的段更新点查询. 由于该题对于段的更新并不是连续的,从而可以构造多个树状数组.因为$k \in [1,10] $,从而可以把更新划分为如下类型: 1,2,3,4,5... ------------- 1,3,5,7,9... 2,4,6,8,10... ------------- 1,4,7,10,13... 2,5,8,11,1

poj3468A Simple Problem with Integers(线段树的区域更新)

http://poj.org/problem?id=3468 真心觉得这题坑死我了,一直错,怎么改也没戏,最后tjj把q[rt].lz改成了long long 就对了,真心坑啊. 线段树的区域更新. 线段树功能:update:成段增减 query:区间求和 #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; #d