Big Water Problem

简介: Big Water Problem

Big Water Problem


给一个数列,会有多次询问,对于每一次询问,会有两种操作:

1:给定两个整数x, y, 然后在原数组的第x位置上加y;

2:给定两个整数l,r,然后输出数组从第l位加到第r位数字的和并换行


输入描述:

第一行有两个整数n, m(1 <= n, m <= 100000)代表数列的长度和询问的次数

第二行n个数字,对于第i个数字a[i],(0<=a[i]<=100000)。

接下来m行,每一行有三个整数f, x, y。第一个整数f是1或者是2,代表操作类型,如果是1,接下来两个数x,y代表第x的位置上加y,如果是2,则求x到y的和,保证数据合法。

输出描述:

输出每次求和的结果并换行

输入

10 2

1 2 3 4 5 6 7 8 9 10

1 1 9

2 1 10

输出

64

思路:单点修改和求区间和,利用树状数组求解


树状数组的优点是实现简单,效率高省空间。主要用于查询前缀和、区间和及点更新,对点查询、 区间更新效率较低。

#include<bits/stdc++.h>
using namespace std;
int a[101010];
long long m,n;
long long lowbit(int x)
{
    return x&-x;
}
void add(long long i,long long x)
{
    while(i<=n)
    {
        a[i]+=x;
        i+=lowbit(i);
    }
}
long long int sum(long long i)
{
    long long ans=0;
    while(i>0)
    {
        ans+=a[i];
        i-=lowbit(i);
    }
    return ans;
}
int main()
{
    long long int f,x,y;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        add(i,x);
    }
    while(m--)
    {
        cin>>f>>x>>y;
        if(f==1)
        {
            add(x,y);
        }
        else
        {
            cout<<sum(y)-sum(x-1)<<endl;
        }
    }
    return 0;
}

目录
相关文章
|
机器学习/深度学习 编解码 人工智能
Reading Notes: Human-Computer Interaction System: A Survey of Talking-Head Generation
由于人工智能的快速发展,虚拟人被广泛应用于各种行业,包括个人辅助、智能客户服务和在线教育。拟人化的数字人可以快速与人接触,并在人机交互中增强用户体验。因此,我们设计了人机交互系统框架,包括语音识别、文本到语音、对话系统和虚拟人生成。接下来,我们通过虚拟人深度生成框架对Talking-Head Generation视频生成模型进行了分类。同时,我们系统地回顾了过去五年来在有声头部视频生成方面的技术进步和趋势,强调了关键工作并总结了数据集。 对于有关于Talking-Head Generation的方法,这是一篇比较好的综述,我想着整理一下里面比较重要的部分,大概了解近几年对虚拟人工作的一些发展和
|
8月前
NC15173 The Biggest Water Problem
NC15173 The Biggest Water Problem
Bear and Big Brother
Bear and Big Brother
69 0
Bear and Big Brother
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
116 0
PAT (Advanced Level) Practice - 1087 All Roads Lead to Rome(30 分)
PAT (Advanced Level) Practice - 1087 All Roads Lead to Rome(30 分)
76 0
PAT (Advanced Level) Practice - 1004 Counting Leaves(30 分)
PAT (Advanced Level) Practice - 1004 Counting Leaves(30 分)
87 0
Performance problem during saving - activating big form
Performance problem during saving - activating big form
81 0
|
计算机视觉 Python
Improving the quality of the output
There are a variety of reasons you might not get good quality output from Tesseract. It's important to note that unless you're using a very unusual fo...
1047 0
The Rising Smart Logistics Industry: How to Use Big Data to Improve Efficiency and Save Costs
This whitepaper will examine Alibaba Cloud’s Cainiao smart logistics cloud and Big Data powered platform and the underlying strategies used to optimiz.
1503 0
The Rising Smart Logistics Industry: How to Use Big Data to Improve Efficiency and Save Costs
lecture 2.2 problem set 1 and 2
1 COUNTING VOWELS   (10/10 分数) Assume s is a string of lower case characters.
1016 0