码迷,mamicode.com
首页 > 其他好文 > 详细

A Simple Problem with Integers

时间:2018-09-16 21:07:37      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:res   string   put   线段   problem   stream   scanf   math   value   

来源poj3468

You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

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

区间修改的线段树

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h> 
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#define sf scanf
#define pf printf
#define scf(x) scanf("%d",&x)
#define scff(x,y) scanf("%d%d",&x,&y)
#define scfff(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define prf(x) printf("%d\n",x) 
#define mm(x,b) memset((x),(b),sizeof(x))
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+7;
const double eps=1e-8;
const int inf=0x3f3f3f3f;
using namespace std;
const double pi=acos(-1.0);
const int N=1e6+10;
struct TREE
{
    ll l,r;
    ll sum;
}tr[N];
ll a[N];
ll add[N];
void built_tree(ll x,ll y,ll i)
{
    tr[i].l =x;tr[i].r =y;
    if(x==y)
    tr[i].sum =a[x];
    else
    {
        ll mid=(x+y)>>1;
        built_tree(x,mid,i<<1);
        built_tree(mid+1,y,i<<1|1);
        tr[i].sum=tr[i<<1].sum+tr[i<<1|1].sum; 
    }
}
void pushdown(int i)//下放惰性标记 
{
    ll lc=i<<1,rc=i<<1|1;
    tr[lc].sum +=(tr[lc].r -tr[lc].l +1)*add[i];
    tr[rc].sum +=(tr[rc].r -tr[rc].l +1 )*add[i];
    add[lc]+=add[i];
    add[rc]+=add[i];
    add[i]=0;
}
void update_tree(ll x,ll y,ll k,int i)
{
    ll lc=i<<1;
    ll rc=i<<1|1;
    if(tr[i].l>y||tr[i].r<x) return; //如果不属于,则返回
    if(x <= tr[i].l &&tr[i].r <=y)
    {
        tr[i].sum+=(tr[i].r -tr[i].l +1)*k;//加至此处不继续往下加 
        add[i]+=k;//存惰性标记 
    }else
    {
        if(add[i])
            pushdown(i);
        update_tree(x,y,k,lc);
        update_tree(x,y,k,rc);
        tr[i].sum =tr[lc].sum+tr[rc].sum; 
    } //查找 
}

ll query(ll x,ll y,int i)
{
    ll lc=i<<1,rc=i<<1|1;
    if(x <= tr[i].l && tr[i].r <= y) return tr[i].sum;
    if(x > tr[i].r|| y < tr[i].l) return 0;
    if(add[i])
        pushdown(i);
    return query(x,y,lc)+query(x,y,rc);
}

int main()
{
    int n,q;
    scff(n,q);
    rep(i,1,n+1)
    sf("%lld",&a[i]);
    built_tree(1,n,1);
    while(q--)
    {
        char c;
        cin>>c;ll x,y,z;
        if(c==‘Q‘)
        {
            sf("%lld%lld",&x,&y);
            pf("%lld\n",query(x,y,1));
        }else
        {
            sf("%lld%lld%lld",&x,&y,&z);
            update_tree(x,y,z,1);
        }
    }
    return 0;
}

A Simple Problem with Integers

标签:res   string   put   线段   problem   stream   scanf   math   value   

原文地址:https://www.cnblogs.com/wzl19981116/p/9657078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!