暴力求解是不行的
注意到差分后的一些性质
1 , 6 , 8 , 5 , 10 {1,6,8,5,10} 1,6,8,5,10,对应差分数组 1 , 5 , 2 , − 3 , 5 {1,5,2,-3,5} 1,5,2,−3,5
区间[2,4] + 2
1 , 8 , 10 , 7 , 10 {1,8,10,7,10} 1,8,10,7,10,对应差分数组 1 , 7 , 2 , − 3 , 3 {1,7,2,-3,3} 1,7,2,−3,3
就是 a [ 2 ] + 2 a[2]+2 a[2]+2 , a [ 5 ] − 2 a[5]-2 a[5]−2
#include <iostream>
using namespace std;
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
int n,m;
const int N = 5e5+10;
int c[N];
int lowbit(int x) {return x&-x;}
void update(int i, int x)
{
while (i<=n){
c[i] += x;
i += lowbit(i);
}
}
int getsum(int i)
{
int sum = 0;
while (i)
{
sum += c[i];
i -= lowbit(i);
}
return sum;
}
int main()
{
ios;
cin >> n >> m;
int last = 0, now;
for(int i = 1; i<= n; i++){
cin >> now;
update(i, now-last);
last = now;
}
while (m--)
{
int op;cin >> op;
if(op==1){
int x, y, k; cin >> x >> y >> k;
update(x,k);
update(y+1,-k);
}else{
int x; cin >> x;
cout << getsum(x) << endl;
}
}
}