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

                                                                        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;
}

 

       

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
POJ 2182是一道使用树状数组解决的题目,题目要求对给定的n个数进行排序,并且输出每个数在排序后的相对位置。树状数组是一种用来高效处理前缀和问题的数据结构。 根据引用中的描述,我们可以通过遍历数组a,对于每个元素a[i],可以使用二分查找找到a到a[i-1]中小于a[i]的数的个数。这个个数就是它在排序后的相对位置。 代码中的query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。 最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。 参考代码如下: ```C++ #include <iostream> #include <cstdio> using namespace std; int n, a += y; } } int main() { scanf("%d", &n); f = 1; for (int i = 2; i <= n; i++) { scanf("%d", &a[i]); f[i = i & -i; } for (int i = n; i >= 1; i--) { int l = 1, r = n; while (l <= r) { int mid = (l + r) / 2; int k = query(mid - 1); if (a[i > k) { l = mid + 1; } else if (a[i < k) { r = mid - 1; } else { while (b[mid]) { mid++; } ans[i = mid; b[mid = true; add(mid, -1); break; } } } for (int i = 1; i <= n; i++) { printf("%d\n", ans[i]); } return 0; } ``` 这段代码使用了树状数组来完成题目要求的排序功能,其中query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [poj2182Lost Cows——树状数组快速查找](https://blog.csdn.net/aodan5477/article/details/102045839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [poj_2182 线段树/树状数组](https://blog.csdn.net/weixin_34138139/article/details/86389799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值