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

POJ3468---线段树模版--A Simple Problem with Integers

时间:2015-04-11 11:51:42      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

#include<stdio.h>
#include<algorithm>
#include<string.h>
#define ll __int64
#define M 100007
using namespace std;
struct node
{
    ll l,r,mid,val,mark;
}tree[M<<2];
ll s[M];
void build(ll left,ll right,ll i)//建树
{
    tree[i].l=left;tree[i].r=right;
    tree[i].mid=(left+right)>>1;tree[i].mark=0;
    if(left==right){tree[i].val=s[left]; return;}
    build(left,tree[i].mid,i*2);
    build(tree[i].mid+1,right,i*2+1);
    tree[i].val=tree[i*2].val+tree[i*2+1].val;
}
void update(int pos,ll val,int i)//点更新
{
    tree[i].val+=val;
    if(tree[i].l==tree[i].r)return;
    if(pos<=tree[i].mid)update(pos,val,i*2);
    else update(pos,val,i*2+1);
}
//ll query(int left,int right,int i)//点查询
//{
//    if(tree[i].l==left&&tree[i].r==right)return tree[i].val;
//    if(right<=tree[i].mid)query(left,right,i*2);
//    else if(left>tree[i].mid)query(left,right,i*2+1);
//    else return query(left,tree[i].mid,i*2)+query(tree[i].mid+1,right,i*2+1);
//}
void update(int left,int right,ll val,int i)//区间更新
{
    if(tree[i].l==left&&tree[i].r==right){tree[i].mark+=val;return;}
    tree[i].val+=val*(right-left+1);
    if(tree[i].mid<left)update(left,right,val,2*i+1);
    else if(tree[i].mid>=right)update(left,right,val,2*i);
    else
    {
        update(left,tree[i].mid,val,2*i);
        update(tree[i].mid+1,right,val,2*i+1);
    }
}
ll query(int left,int right,int i)//区间查询
{
    if(tree[i].l==left&&tree[i].r==right)  return tree[i].val+tree[i].mark*(right-left+1);
    if(tree[i].mark!=0)
    {
        tree[i*2].mark+=tree[i].mark;tree[i*2+1].mark+=tree[i].mark;
        tree[i].val+=(tree[i].r-tree[i].l+1)*tree[i].mark;tree[i].mark=0;
    }
    if(tree[i].mid>=right){return query(left,right,i*2);}
    else if(tree[i].mid<left){return query(left,right,i*2+1);}
    else{return query(left,tree[i].mid,i*2)+query(tree[i].mid+1,right,i*2+1);}
}
int main()
{
    ll n,m,i,j,k;
    ll l,f,num;
    char str[5];
    while(scanf("%I64d%I64d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%I64d",&s[i]);
        build(0,n,1);

        for(i=0;i<m;i++)
        {
          //  printf("i=%I64d",i);
            scanf("%s",&str);
            if(str[0]=='Q')
            {
                scanf("%I64d%I64d",&l,&f);
                printf("%I64d\n",query(l,f,1));
            }
            if(str[0]=='C')
            {
                scanf("%I64d%I64d%I64d",&l,&f,&num);
                update(l,f,num,1);
            }
        }



    }
    return 0;
}


 

POJ3468---线段树模版--A Simple Problem with Integers

标签:

原文地址:http://blog.csdn.net/hy1405430407/article/details/44993139

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