原题链接:SaikrVj | 【团队赛组】2021-2022年度第三届全国大学生算法设计与编程挑战赛(秋季赛)——热身赛
树状数组+差分维护前缀和
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int,int>pii;
const int mod = 1e9+7 , INF = 0x3f3f3f3f , N = 2e5 + 10;
LL tr[N];
int n,m;
int lowbit(int x)
{
return x & -x;
}
void add(int x,int c)
{
for (int i = x ; i <= n ; i += lowbit(i))
tr[i] += c;
}
LL query(int x)
{
LL res = 0;
for (int i = x ; i ; i -= lowbit(i))
res += tr[i];
return res;
}
int main()
{
cin >> n >> m;
LL sum = 0;
while (m -- )
{
int type;
cin >> type;
if (type)
{
int x;
cin >> x;
cout << sum - query(x) << endl;
}
else
{
int l,r,c;
cin >> l >> r >> c;
sum += (r - l + 1) * c;
add(l,(r - l + 1) * c);
add(r + 1,-(r - l + 1) * c);
}
}
}