https://www.luogu.com.cn/problem/P2068
题目描述
给定一个长度为n(n<=100000),初始值都为0的序列,x(x<=10000)次的修改某些位置上的数字,每次加上一个数,然后提出y (y<=10000)个问题,求每段区间的和。时间限制1秒。
输入格式
第一行1个数,表示序列的长度n
第二行1个数,表示操作的次数w
后面依次是w行,分别表示加入和询问操作
其中,加入用x表示,询问用y表示
x的格式为”x a b” 表示在序列a的位置加上b
y的格式为”y a b” 表示询问a到b区间的加和
输出格式
每行一个数,分别是每次询问的结果
输入输出样例
输入 #1复制
5 4 x 3 8 y 1 3 x 4 9 y 3 4
输出 #1复制
8 17
树状数组模板二题目(双倍经验)
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#define lowbit(x) x&(-x)
using namespace std;
const int maxn=100010;
typedef long long LL;
LL n,x;
LL tree[maxn];
LL add(LL x,LL d)
{
while(x<=n)
{
tree[x]+=d;
x+=lowbit(x);
}
}
LL ask(LL x)
{
LL sum=0;
while(x>0)
{
sum+=tree[x];
x-=lowbit(x);
}
return sum;
}
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
cin>>n>>x;
while(x--)
{
char op;LL x;LL y;
cin>>op>>x>>y;
if(op=='x')
{
add(x,y);
}
else
{
cout<<ask(y)-ask(x-1)<<endl;
}
}
return 0;
}