文章目录
H题【树状数组】
Ryuji doesn’t want to study
蚌埠住了,add函数里面要ll val;二维树状数组的板子题,有公式推导的部分
#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
const int N = 1e6 + 1000;
const int mod = 11092019;
const int inf = 0x7f7f7f7f;
const int eps = 1e-8;
ll read()
{
ll x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while ('0' <= c && c <= '9')
x = x * 10 + c - '0', c = getchar();
return x * f;
}
ll n, q, res;
ll a[N], c[3][N];
ll lowbit(ll x) { return x & -x; }
void add(int x, ll val, int id)//就nm离谱
{
for (; x <= n; x += lowbit(x))
c[id][x] += val;
}
ll ask(int x, int id)
{
ll ans = 0;
for (; x; x -= lowbit(x))
ans += c[id][x];
return ans;
}
int main()
{
// IOS;
cin >> n >> q;
for (int i = 1; i <= n; i++)
{
scanf("%lld", &a[i]);
add(i, a[i], 0);
add(i, i * a[i], 1);
}
res = 0;
while (q--)
{
ll x, l, r;
scanf("%lld%lld%lld", &x, &l, &r);
if (x == 1)
{
if (l > r)
{
puts("0");
continue;
}
res = (r + 1) * (ask(r, 0) - ask(l - 1, 0));
res -= ask(r, 1) - ask(l - 1, 1);
printf("%lld\n", res);
}
else
{
// add(l, r - (ask(l, 0) - ask(l - 1, 0)), 0);//这种实际上就是每次修改树状数组里面的
// add(l, l * r - (ask(l, 1) - ask(l - 1, 1)), 1);
add(l, r - a[l], 0);
add(l, l * (r - a[l]), 1);
a[l] = r;//注意每次要更新a[]数组
}
}
return 0;
}
/**
【Code is far away from bug with the animal protecting!!!】
休息一下~休息一下~
 ̄ ̄ ̄ ̄ ̄\/ ̄ ̄ ̄ ̄
∧_∧
;;(??ω?)
_旦_(っ(,,■)__
|l ̄l|| ̄し?し? ̄|i
**/