读入可以用快读
by dream_dad @ 2024-08-30 12:26:35
@[eason_jiang](/user/1155909)
by dream_dad @ 2024-08-30 12:28:10
@[eason_jiang](/user/1155909) ??树状数组的板子,你写前缀和。。。差分不知道能不能水过去,你可以试下。。 互关。。
树状数组代码:
```cpp
#include<bits/stdc++.h>
#define N 1000005
using namespace std;
int bl,n,m,ans,tmp,x,y,tra[N];
int lowbit(int x){
return x&(-x);
}
void update(int idx,int data){
for(int i=idx;i<=n;i+=lowbit(i)){
tra[i]+=data;
}
}
int ask(int idx){
ans=0;
for(int i=idx;i>=1;i-=lowbit(i))ans+=tra[i];
return ans;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i){
scanf("%d",&tmp);
update(i,tmp);
}
while(m--){
scanf("%d%d%d",&bl,&x,&y);
switch(bl){
case 2:
printf("%d\n",ask(y)-ask(x-1));
break;
case 1:
update(x,y);
break;
}
}
}
```
线段树代码:
```cpp
#include<bits/stdc++.h>
const int N=5e5;
using namespace std;
int n,m,x,y,arr[N];
char opt,F[20];
typedef struct TreeNode{
int l,r;
long long sum;
}TreeNode;
TreeNode tree[4*N];
void add(int root,int p,int x){
tree[root].sum+=x;
if(tree[root].l==tree[root].r)return ;
if(p<=tree[root<<1].r)add(root<<1,p,x);
if(p>=tree[root<<1|1].l)add(root<<1|1,p,x);
}
int query(int p,int l,int r){
if(tree[p].l>=l&&tree[p].r<=r)return tree[p].sum;
int mid=(tree[p].l+tree[p].r)>>1;
long long sum=0;
if(l<=mid)sum+=query(p<<1,l,r);
if(r>mid)sum+=query(p<<1|1,l,r);
return sum;
}
void pushup(int p){
tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum;
}
void build(int p,int l,int r){
tree[p].l=l;
tree[p].r=r;
if(l==r){
tree[p].sum=arr[l];
return ;
}
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
pushup(p);
}
int qr(){
int res=0,f=1;
char ch=getchar();
while(ch>'9'||ch<'0'){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
res=(res<<3)+(res<<1)+ch-'0';
ch=getchar();
}
return res*f;
}
void write(int x){
int p=0,i;
if(x<0){
putchar('-');
x=-x;
}
do{
F[p++]=x%10;
x/=10;
}while(x);
i=p-1;
while(i>=0){
putchar(F[i]+'0');
--i;
}
putchar('\n');
}
int main(){
n=qr();
m=qr();
for(int i=1;i<=n;++i){
arr[i]=qr();
}
build(1,1,n);
while(m--){
// opt=getchar();
// getchar();
cin>>opt;
x=qr();
y=qr();
switch(opt){
case '1':
add(1,x,y);
break;
case '2':
write(query(1,x,y));
// cout<<query(1,x,y)<<endl;
break;
}
}
}
```
by liuhaoyan0323 @ 2024-08-30 12:34:20
@[eason_jiang](/user/1155909) 因为两份代码隔了3个月,本人码风改了,所以可能有点怪。
by liuhaoyan0323 @ 2024-08-30 12:35:48
这两份代码都能过,谢谢大佬,已关
by jiangyixuan_eason @ 2024-08-30 12:49:45
@[dream_dad](/user/775831) @[liuhaoyan0323](/user/921177) 太谢谢了,终于过了。全部已关,求互关。
by jiangyixuan_eason @ 2024-08-30 12:51:52