题目
题目链接:https://www.luogu.com.cn/problem/P3372
代码
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<ctime>
#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<iomanip>
#include<list>
#include<bitset>
#include<sstream>
#include<fstream>
#include<complex>
#include<algorithm>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif
#define ll long long
#define lowbit(x) (-x)&x
#define int long long
using namespace std;
const int INF = 0x3f3f3f3f;
int n,m;
int bit[500010],bit2[500010];
void edit(int pos,int val){
for(int i=pos;i<=n;i+=lowbit(i)){
bit[i]+=val;
bit2[i]+=val*pos;
}
}
void range_edit(int l,int r,int val){
edit(l,val);
edit(r+1,-val);
}
int query(int x){
int res=0;
for(int i=x;i>0;i-=lowbit(i)){
res+=bit[i]*(x+1)-bit2[i];
}
return res;
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
int t;
cin>>t;
edit(i,t);
edit(i+1,-t);
}
int st;
for(int i=1;i<=m;i++){
cin>>st;
if(st==1){
int a,b,c;
cin>>a>>b>>c;
range_edit(a,b,c);
}
else {
int a,b;
cin>>a>>b;
cout<<query(b)-query(a-1)<<endl;
}
}
return 0;
}
来源:CSDN
作者:kosf_
链接:https://blog.csdn.net/kosf_/article/details/104320091