一个问题 P3374
  • 板块题目总版
  • 楼主jessie2011
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/8/17 16:10
  • 上次更新2023/11/3 03:07:41
查看原帖
一个问题 P3374
915065
jessie2011楼主2023/8/17 16:10

为何输出如此奇特?WA 0分求调


这是程序:

#include<bits/stdc++.h>
using namespace std;
int n,m,c[500005];
int lowbit(int x){
	return x&(-x);
}
int sum(int x){
	int s=0;
	while(x>0){
		s+=c[x];
		x-=lowbit(x);
	}
	return s;
}
void change(int x,int y){
	while(x<=n){
		c[x]+=y;
		x+=lowbit(x);
	}
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		int t1;
		cin>>t1;
	    change(i,t1);
	}
	for(int i=1;i<=m;i++){
		int t1,t2,t3;
		cin>>t1>>t2>>t3;
		if(t1==1)  change(t2,t3);
		if(t1==2)  cout<<sum(t2)-sum(t3-1)<<endl;
	}
	return 0;
}

这是输入:

5 5
1 5 4 2 3
1 1 3
2 2 5
1 3 -1
1 4 2
2 1 4

这是输出:

-6 -8

Dalao们帮着看看呗。

2023/8/17 16:10
加载中...