P3374 【模板】树状数组 1

题目链接
P3374 【模板】树状数组 1
提交
69.12k
通过
38.17k
时间限制
1.00s
内存限制
125.00MB
提交答案
加入题单
题目提供者
HansBug
难度
普及/提高-
历史分数
100
提交记录 查看题解
标签
高性能
进入讨论版
相关讨论
推荐题目
展开
题目描述
如题,已知一个数列,你需要进行下面两种操作:

将某一个数加上 xx

求出某区间每一个数的和

输入格式
第一行包含两个正整数 n,mn,m,分别表示该数列数字的个数和操作的总个数。

第二行包含 nn 个用空格分隔的整数,其中第 ii 个数字表示数列第 ii 项的初始值。

接下来 mm 行每行包含 33 个整数,表示一个操作,具体如下:

1 x k 含义:将第 xx 个数加上 kk

2 x y 含义:输出区间 [x,y][x,y] 内每个数的和

输出格式
输出包含若干行整数,即为所有操作 22 的结果。

输入输出样例
输入 #1复制
5 5
1 5 4 2 3
1 1 3
2 2 5
1 3 -1
1 4 2
2 1 4
输出 #1复制
14
16
说明/提示
【数据范围】

对于 30%30% 的数据,1 \le n \le 81≤n≤8,1\le m \le 101≤m≤10;
对于 70%70% 的数据,1\le n,m \le 10^41≤n,m≤10
4

对于 100%100% 的数据,1\le n,m \le 5\times 10^51≤n,m≤5×10
5

样例说明:

故输出结果14、16

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int maxn = 500010;
int d[maxn];
int n,m;
inline int lowbit(int x){return -x&x;}

int ask(int r){
    int res=0;
    while(r) res += d[r], r -= lowbit(r);
    return res;
}

void updata(int x,int y){
    while(x <= n)d[x] += y,x += lowbit(x);
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i = 1,a;i <= n;i++){
    cin>>a;
    updata(i,a);
    }
    for(int i=1;i<=m;i++){
        int tp,x,k;
        cin>>tp>>x>>k;
        if(tp == 1){
            updata(x,k);
        }else {
            cout<<ask(k)-ask(x-1)<<'\n';
        }
    }
    return 0;
}

感觉这个东西和线段树解决的方式比较像。两个都能跑。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值