POJ3468(树状数组区间更新和区间查询-线段树)

版权声明:Dream_dog专属 https://blog.csdn.net/Dog_dream/article/details/88592685

                                                                        A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 151635   Accepted: 47026
Case Time Limit: 2000MS

Description

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 AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.

Source

POJ Monthly--2007.11.25, Yang Yi


 题意:给一个长度为n的数列,有Q次操作Q代表查询区间 a b之间的累加和,操作C代表将a-b区间的所有数加上c

题解:Sum_{n}=a_{1}+a_{2}+a_{3}+....+a_{n}   

    令:c_{1}=a_{1}    c_{2}=a_{2}-a_{1}   .......   c_{n}=a_{n+1}-a_{n-1}   

所以:a_{1}=c_{1}    a_{2}=c_{1}+c_{2}    .......   a_{n}=c_{1}+c_{2}+.....+c_{n}

所以:Sum_{n}=c_{1}+(c_{1}+c_{2})+(c_{1}+c_{2}+c_{3})+....+(c_{1}+c_{2}+c_{3}+...+c_{n}) 

                     =n\times c_{1}+(n-1)\times c_{2}+(n-3)\times c_{3}...+c_{n}

                     =(n+1)\times\sum_{i=1}^{n} c_{i} -\sum_{i=1}^{n} (i\times c_{i})

   令:数组c1[n]=c_{n}  数组 c2[n]=n\times c_{n}   则  Sum(a)=(n+1)*Sum(c1)-Sum(c2)

证明:对于区间更新 l-r 增加 d 因为  Sum(i)=a[i]   所以对于执行lowbit(l,c1,d)lowbit(r+1,c1,-d)  就相当于将l-ra[i]进行了更新

    


树状数组代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f3f3f3f
#define clr(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
const int maxn=100000+2;
typedef long long ll;
typedef unsigned long long ull;
ll a[maxn],c1[maxn],c2[maxn];
int n,m;
void  add(int k,ll w1,ll w2)
{
    while(k<=n)
    {
        c1[k]+=w1;
        c2[k]+=w2;
        k+=lowbit(k);
    }
}
ll sum(ll *tc, int k)
{
    ll ans=0;
    while(k>0)
    {
        ans+=tc[k];
        k-=lowbit(k);
    }
    return ans;
}

int main()
{
       scanf("%d%d",&n,&m);
        a[0]=0;
        for(int i=1;i<=n;++i)
        {
            scanf("%lld",a+i);
            add(i,a[i]-a[i-1],(i-1)*(a[i]-a[i-1]));
        }
        char op;
        int l,r;
        ll tm;
        while(m--)
        {
            cin>>op;
            if(op=='Q')
            {
                scanf("%d%d",&l,&r);
                ll ans=r*sum(c1,r)-(l-1)*sum(c1,l-1)-(sum(c2,r)-sum(c2,l-1));
                printf("%lld\n",ans);
            }
            else
            {
                scanf("%d%d%lld",&l,&r,&tm);
                add(l,tm,(l-1)*tm);
                add(r+1,-tm,-1*r*tm);
            }
        }
    return 0;
}

线段树:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
using namespace std;
const int MAX=100000+2;
typedef long long ll;
int n,a,b,m,x,y;
ll ans;
struct node
{
    ll l,r,w,f;
} tree[MAX*4];
inline void build(int k,int ll,int rr)//建树
{
    tree[k].l=ll,tree[k].r=rr;
    if(tree[k].l==tree[k].r)
    {
        scanf("%lld",&tree[k].w);
        return;
    }
    int m=(ll+rr)/2;
    build(k*2,ll,m);
    build(k*2+1,m+1,rr);
    tree[k].w=tree[k*2].w+tree[k*2+1].w;
}
inline void down(int k)//标记下穿
{
    tree[k*2].f+=tree[k].f;
    tree[k*2+1].f+=tree[k].f;
    tree[k*2].w+=tree[k].f*(tree[k*2].r-tree[k*2].l+1);
    tree[k*2+1].w+=tree[k].f*(tree[k*2+1].r-tree[k*2+1].l+1);
    tree[k].f=0;
}
inline void ask_interval(int k)//区间查询
{
    if(tree[k].l>=a&&tree[k].r<=b)
    {
        ans+=tree[k].w;
        return;
    }
    if(tree[k].f)
        down(k);
    int m=(tree[k].l+tree[k].r)/2;
    if(a<=m)
        ask_interval(k*2);
    if(b>m)
        ask_interval(k*2+1);
}
inline void change_interval(int k)//区间修改
{
    if(tree[k].l>=a&&tree[k].r<=b)
    {
        tree[k].w+=(tree[k].r-tree[k].l+1)*y;
        tree[k].f+=y;
        return;
    }
    if(tree[k].f)
        down(k);
    int m=(tree[k].l+tree[k].r)/2;
    if(a<=m)
        change_interval(k*2);
    if(b>m)
        change_interval(k*2+1);
    tree[k].w=tree[k*2].w+tree[k*2+1].w;
}
int main()
{
    //freopen("data.txt","r",stdin);
    scanf("%d%d",&n,&m);
    build(1,1,n);

    for(int i=1; i<=m; i++)
    {
        getchar();
        char q;
        scanf("%c",&q);
        switch(q)
        {
        case 'C':
        {
            scanf("%d%d%d",&a,&b,&y);//区间修改
            change_interval(1);
            break;
        }
        case 'Q':
        {
            scanf("%d%d",&a,&b);//区间查询
            ans=0;
            ask_interval(1);
            printf("%lld\n",ans);
            break;
        }
        }
    }
    return 0;
}

       

猜你喜欢

转载自blog.csdn.net/Dog_dream/article/details/88592685