跳到正文

个人记录

树状数组

Refined_heart约 2 分钟
原文
文章信息与版本

文章信息

发布
2019年1月19日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 2 分钟
获赞
2

版本

  1. v1当前

记录一下码风不太好的模板:

C++
#include<cstdio>
#include<iostream>
using namespace std;
int tree[100000],n,m;
inline int read(){int s=0,w=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){s=s*10+ch-48;ch=getchar();}return s*w;}
inline int lowbit(int x){return x&(-x);}
inline void revise(int x,int k){while(x<=n){tree[x]+=k;x+=lowbit(x);}return;}
inline int sum(int k){int ans=0;while(k!=0){ans+=tree[k];k-=lowbit(k);}return ans;}
inline int search(int x){int ans=0;while(x!=0){ans+=tree[x];x-=lowbit(x);}return ans;}
int main(){n=read(),m=read();for(register int i=1;i<=n;i++){int xx=read();revise(i,xx);}
	for(register int i=1;i<=m;i++){int opt,l,r;opt=read(),l=read(),r=read();if(opt==1){revise(l,r);}
	else{int ans_1=sum(r);int ans_2=sum(l-1);int ans_3(ans_1ans_2);cout<<ans_3<<endl;}}return 0;}

恐怖的压行操作……

讲解照常,以后补

原文