例题:https://www.luogu.org/problem/show?pid=3374

程序:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,x,y,ch,f[600000],a[600000];
int lowbit(int x)
{
    return (x&-x);
}

//单点修改
void update(int x,int k)
{
    while (x<=n) 
    {
        f[x]+=k;
        x+=lowbit(x);
    }
}

//单点查询
int query(int x)
{
    int ans;
    ans=0;
    while (x>0) 
    {
       ans+=f[x];
       x-=lowbit(x);
    }
    return ans;
}
int main()
{
    cin>>n>>m;
    for (int i=1;i<=n;i++) 
    {
      cin>>a[i];
      update(i,a[i]);
    }
    for (int i=1;i<=m;i++){
      cin>>ch>>x>>y;
      if (ch==1) update(x,y);
      else cout<<query(y)-query(x-1)<<endl;
    }  
}

好啦好啦。。。

posted on 2017-02-05 15:14  nhc2014  阅读(201)  评论(0编辑  收藏  举报