数组机器人--(线段树模板题)

数组计算机

  1000 ms         65536 KiB
Submit Status My Status  Origin

Description

bLue 有一个神器的机器,这个机器可以读入一个数组,并按照用户要求快速地进行数组的处理和计算,它支持如下两种操作:

  • 操作 1:把数组中第 p 个元素的值增加 v。
  • 操作 2:计算数组中 [l, r] 区间内所有数的和。

这个机器就是这么的神奇,但是 bLue 的计算机坏掉了,你能帮他修一下吗?

Input

输入数据有多组(数据组数不超过 20),到 EOF 结束。

对于每组数据:

  • 第 1 行输入一个整数 n (1 <= n <= 10^5),表示数组中元素的个数。
  • 第 2 行输入 n 个用空格隔开的整数 ai (1 <= ai <= 10^10),表示初始输入到计算机中的数组。
  • 第 3 行输入一个整数 q (1 <= q <= 50000),表示用户的操作次数。
  • 接下来 q 行,每行输入先输入 1 个整数,表示操作类型,根据不同的操作类型:
    • 如果类型为 1,则紧接着输入 2 个用空格隔开的整数 p (1 <= p <= n) 和 v (1 <= v <= 10^10),表示要把数组中第 p 个数的值增加 v。
    • 如果类型为 2,则紧接着输入 2 个用空格隔开的整数 l, r (1 <= l <= r <= n),表示要计算区间 [l, r] 内所有数的和(数组下标从 1 开始)。

Output

对于每组数据中的每次类型为 2 的操作,输出 1 行,包含一个整数,表示计算出的和。

Sample

Input

Copy5
1 2 3 4 5
5
2 1 2
2 1 5
1 4 10
2 4 5
2 1 5

Output

Copy3
15
19
25

题解:刚学了一下线段树的主要思(mu)想(ban),于是赶紧来水一道模板题,借着做题熟悉一下主要运用和基本思路,题目不难,就是对区间的修改和区间查询,使用线段树可以达到nlogn的复杂度。

#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}

const int maxn=1e5;
ll a[maxn+5],sum[maxn<<2];
void pushup(int rt){sum[rt]=sum[rt<<1]+sum[rt<<1|1];return;}
void build(int l,int r,int rt) //区间左端点,区间右端点,根节点下标
{
    if(l==r) {sum[rt]=a[l];return;}
    int m=(l+r)>>1;
    build(l,m,rt<<1);build(m+1,r,rt<<1|1);
    pushup(rt);
}

ll query(int L,int R,int l,int r,int rt) // L,R所要操作的区间,l,r是你创造的树的总区间,rt是根节点。
{
    if(L<=l && r<=R) return sum[rt];
    int m=(l+r)>>1;
    ll ans=0;
    if(L<=m) ans+=query(L,R,l,m,rt<<1);
    if(R>m) ans+=query(L,R,m+1,r,rt<<1|1);
    return ans;
}

void update(int L,ll c,int l,int r,int rt) //L为所要添加值的节点,l,r是总区间,rt为根节点。
{
    if(l==r) {sum[rt]+=c; return;}
    int m=(l+r)>>1;
    if(L<=m) update(L,c,l,m,rt<<1);
    else update(L,c,m+1,r,rt<<1|1);
    pushup(rt);
}

int main()
{
    int n;
    while(cin>>n)
    {
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=n;i++)
            a[i]=read();
        build(1,n,1);

        int num;
        cin>>num;
        for(int i=1;i<=num;i++)
        {
            int node;
            node=read();
            if(node==1)
            {
                int p=read();
                ll v=read();
                update(p,v,1,n,1);
            }
            else
            {
                int l=read();
                int r=read();
                ll ans=query(l,r,1,n,1);
                cout<<ans<<endl;
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值