萌新求条树状数组模板
  • 板块灌水区
  • 楼主BK小鹿
  • 当前回复4
  • 已保存回复4
  • 发布时间2024/8/27 10:21
  • 上次更新2024/8/27 12:38:51
查看原帖
萌新求条树状数组模板
1345783
BK小鹿楼主2024/8/27 10:21

rt,好久没写了,今天连模板都写挂了。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
char *p1,*p2,buf[1<<20+5];
inline int read(){
unsigned int x=0;
char c=gc();
while(!isdigit(c)){
c=gc();
}while(isdigit(c)){
x=x*10+(c^48);
c=gc();
}return x;
}

typedef long long LL;

const int N = 5e5 + 5;

int n, m;
int a[N], c[N];

inline int lowbit(int x)
{
	return x & (-x);
}

inline void upd(int x, int d)
{
	for (int i = x; i <= n; i += lowbit(i)) c[i] += d;
}

inline LL sum(int x)
{
	LL cnt = 0;
	for (int i = x; i > 0; i -= lowbit(i)) cnt += c[i];
	return cnt;
}

int main()
{
	n = read(), m = read();
	for (int i = 1; i <= n; i ++ ) a[i] = read(), upd(i, a[i]);
	
	int opt, x, y, d;
	while (m -- )
	{
		opt = read();
		if (opt == 1)
		{
			x = read(), d = read();
			//a[x] += d;
			upd(x, d);
		}
		if (opt == 2)
		{
			x = read(), y = read();
			LL res = sum(y) - sum(x - 1);
			printf("%lld\n", res);
		}
	}
	return 0;
}
2024/8/27 10:21
加载中...