AcWing 1264. 动态求连续区间和(java)
原题链接
简单
作者:
文思涌
,
2021-03-08 17:27:59
,
所有人可见
,
阅读 407
private static int N=100010;
private static int n,m;
private static int[] arr=new int[N];
private static int[] tr=new int[N];//树状数组
public static int lowbit(int x) {
return x&-x;
}
public static void add(int a,int b) {//添加某个数操作
for(int i=a;i<=n;i+=lowbit(i))
tr[i]+=b;//若题意是说变成c则应表示:tr[i]+=c-tr[i]
}
public static int query(int x) {//求前缀和
int res=0;
for(int i=x;i>=1;i-=lowbit(i))
res+=tr[i];
return res;
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
n=scanner.nextInt();m=scanner.nextInt();
for(int i=1;i<=n;i++)arr[i]=scanner.nextInt();
for(int i=1;i<=n;i++)add(i, arr[i]);//搭建树状数组
for(int i=0;i<m;i++) {
int k=scanner.nextInt();
int a=scanner.nextInt();
int b=scanner.nextInt();
if(k==0) {
System.out.println(query(b)-query(a-1));
}else {
add(a, b);
}
}
}