题目链接:点击进入
题目
题意
n 个数,q 次询问:
C a b c 区间 [ a , b ] 都加上 c
Q a b 求 [ a , b ] 的和
思路
树状数组实现 -> 区间修改 + 区间查询
( 线段树也可做 )
代码
// Problem: A Simple Problem with Integers
// Contest: Virtual Judge - POJ
// URL: https://vjudge.net/problem/POJ-3468
// Memory Limit: 131 MB
// Time Limit: 5000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize(3)//O3
//#pragma GCC optimize(2)//O2
#include<iostream>
#include<string>
#include<map>
#include<set>
//#include<unordered_map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<fstream>
#define X first
#define Y second
#define best 131
#define INF 0x3f3f3f3f3f3f3f3f
#define pii pair<int,int>
#define lowbit(x) x & -x
#define inf 0x3f3f3f3f
#define int long long
//#define double long double
//#define rep(i,x,y) for(register int i = x; i <= y;++i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pai=acos(-1.0);
const int maxn=1e6+10;
const int mod=998244353;
const double eps=1e-9;
const int N=5e3+10;
/*--------------------------------------------*/
inline int read()
{
int k = 0, f = 1 ;
char c = getchar() ;
while(!isdigit(c)){if(c == '-') f = -1 ;c = getchar() ;}
while(isdigit(c)) k = (k << 1) + (k << 3) + c - 48 ,c = getchar() ;
return k * f ;
}
/*--------------------------------------------*/
int n,q,a[maxn],c1[maxn],c2[maxn];
void add(int x,int val)
{
for(int i=x;i<=n;i+=lowbit(i))
c1[i]+=val,c2[i]+=val*(x-1);
}
int getsum(int x)
{
int sum=0;
for(int i=x;i>=1;i-=lowbit(i))
sum+=x*c1[i]-c2[i];
return sum;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>q;
a[0]=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
add(i,a[i]-a[i-1]);
}
while(q--)
{
char op;
int x,y,z;
cin>>op>>x>>y;
if(op=='C')
cin>>z,add(x,z),add(y+1,-z);
else
cout<<getsum(y)-getsum(x-1)<<endl;
}
return 0;
}