蒟蒻求助树状数组
查看原帖
蒟蒻求助树状数组
409265
YJY0807qwq楼主2021/12/12 12:20

rt,样例第一个输出是4,第二个没问题

#include <bits/stdc++.h>
using namespace std;

#ifdef ONLINE_JUDGE
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<22], *p1(buf), *p2(buf);
#endif
template<typename F>
inline void read(register F&x) {x = 0; short f(1); register char ch(getchar()); while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();} while(ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();} x *= f;}
template<typename F>
inline void write(register F x) {if(x < 0) putchar('-'), x = -x; if(x > 9) write(x / 10); putchar(x % 10 + '0');}
template<typename T,typename... Args>
inline void read(T& t, Args&... args){read(t);read(args...);}

constexpr int MAXN = 500001;
int a[MAXN], c[MAXN], n, m;
inline int lowbit(const register int x)
{
	return x & -x;
}

inline void add(const int x, const int y, const int k)
{
	register int i;
	for (i = x; i <= n; i += lowbit(i))
	{
		c[i] += k;
	}
	for (i = y + 1; i <= n; i += lowbit(i))
	{
		c[i] -= k;
	}
	return;
}

inline int ask(const int k)
{
	return a[k] + c[k];
}

int main()
{
	register int i, tmp, op, x, y, k;
	read(n, m);
	for (i = 1; i <= n; i++)
	{
		read(a[i]);
	}
	while (m--)
	{
		read(op);
		if(op == 1)
		{
			read(x, y, k);
			add(x, y, k);
		}
		else
		{
			read(k);
			write(ask(k));
			putchar(10);
		}
	}
}
2021/12/12 12:20
加载中...