线段树两题 hdu1166 hdu1754

#include
#include
#include
#include
#include
using namespace std;
const int maxn=200000+50;
int n,m,arr[maxn],casenum=0,start,endi,q[maxn],num=0,sum;
char str[10];
struct SegTreeNode{
int val;
}segTree[maxn];

void build(int root,int arr[],int istart,int iend)
{
    if(istart==iend) segTree[root].val=arr[istart];
    else
    {
        int mid=(istart+iend)/2;
        build(root*2+1,arr,istart,mid);
        build(root*2+2,arr,mid+1,iend);
        segTree[root].val=segTree[root*2+1].val+segTree[root*2+2].val;
    }
}

int query(int root,int nstart,int nend,int qstart,int qend)
{
    if(qstart>nend||qend=nend)  {/*printf("sum:%d\n",segTree[root].val);*/return sum=(sum+segTree[root].val);}
    int mid=(nstart+nend)/2;
    query(root*2+1,nstart,mid,qstart,qend);
    query(root*2+2,mid+1,nend,qstart,qend);

}

void addOne(int root,int nstart,int nend,int index,int addVal)
{
    segTree[root].val+=addVal;
    if(nstart==nend)
    {
        if(index==nstart) //{printf("root:%d add:%d\n",root,segTree[root].val);}
        return;
    }
    int mid=(nstart+nend)/2;
    if(index<=mid) addOne(root*2+1,nstart,mid,index,addVal);
    else addOne(root*2+2,mid+1,nend,index,addVal);
}

void subOne(int root,int nstart,int nend,int index,int subVal)
{
    segTree[root].val-=subVal;
    if(nstart==nend)
    {
        if(index==nstart) //{printf("root: %d sub:%d\n",root,segTree[root].val);}
        return;
    }
    int mid=(nstart+nend)/2;
    if(index<=mid) subOne(root*2+1,nstart,mid,index,subVal);
    else subOne(root*2+2,mid+1,nend,index,subVal);
}

int main()
{
    char s[10];
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
            scanf("%d",&arr[i]);

        build(1,arr,1,m);
        printf("Case %d:\n",++casenum);
        while(1)
        {
            //
            scanf("%s",str);
            if(str[0]=='E')
                break;
            if(str[0]=='Q')
            {
                scanf("%d %d",&start,&endi);
                sum=0;
                query(1,1,m,start,endi);
                printf("%d\n",sum);
            }
            if(str[0]=='A')
            {
                scanf("%d %d",&start,&endi);
                addOne(1,1,m,start,endi);
            }
            if(str[0]=='S')
            {
                scanf("%d %d",&start,&endi);
                subOne(1,1,m,start,endi);
            }
        }
    }
    return 0;
}
#include
#include
#include
#include
#include
using namespace std;
const int maxn=200000+50;
int n,m,arr[maxn],casenum=0,start,endi,q[maxn],num=0,sum;
char str[10];
struct SegTreeNode{
int val;
}segTree[maxn];

void build(int root,int arr[],int istart,int iend)
{
    if(istart==iend) segTree[root].val=arr[istart];
    else
    {
        int mid=(istart+iend)/2;
        build(root*2+1,arr,istart,mid);
        build(root*2+2,arr,mid+1,iend);
        segTree[root].val=segTree[root*2+1].val+segTree[root*2+2].val;
    }
}

int query(int root,int nstart,int nend,int qstart,int qend)
{
    if(qstart>nend||qend=nend)  {/*printf("sum:%d\n",segTree[root].val);*/return sum=(sum+segTree[root].val);}
    int mid=(nstart+nend)/2;
    query(root*2+1,nstart,mid,qstart,qend);
    query(root*2+2,mid+1,nend,qstart,qend);

}

void addOne(int root,int nstart,int nend,int index,int addVal)
{
    segTree[root].val+=addVal;
    if(nstart==nend)
    {
        if(index==nstart) //{printf("root:%d add:%d\n",root,segTree[root].val);}
        return;
    }
    int mid=(nstart+nend)/2;
    if(index<=mid) addOne(root*2+1,nstart,mid,index,addVal);
    else addOne(root*2+2,mid+1,nend,index,addVal);
}

void subOne(int root,int nstart,int nend,int index,int subVal)
{
    segTree[root].val-=subVal;
    if(nstart==nend)
    {
        if(index==nstart) //{printf("root: %d sub:%d\n",root,segTree[root].val);}
        return;
    }
    int mid=(nstart+nend)/2;
    if(index<=mid) subOne(root*2+1,nstart,mid,index,subVal);
    else subOne(root*2+2,mid+1,nend,index,subVal);
}

int main()
{
    char s[10];
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
            scanf("%d",&arr[i]);

        build(1,arr,1,m);
        printf("Case %d:\n",++casenum);
        while(1)
        {
            //
            scanf("%s",str);
            if(str[0]=='E')
                break;
            if(str[0]=='Q')
            {
                scanf("%d %d",&start,&endi);
                sum=0;
                query(1,1,m,start,endi);
                printf("%d\n",sum);
            }
            if(str[0]=='A')
            {
                scanf("%d %d",&start,&endi);
                addOne(1,1,m,start,endi);
            }
            if(str[0]=='S')
            {
                scanf("%d %d",&start,&endi);
                subOne(1,1,m,start,endi);
            }
        }
    }
    return 0;
}//HDU1754


对于要输入字符的注意要吸收空格

你可能感兴趣的:(线段树,区间和,区间最值)