P1438 无聊的数列
线段树用于维护等差数列区间修改量,另开一数组维护原数组
区间修改是利用前缀和的思想
#include <bits/stdc++.h>
#define inf 0x7fffffff
#define ll long long
#define int long long
//#define double long double
//#define double long long
#define re int
//#define void inline void
#define eps 1e-8
//#define mod 1e9+7
#define ls(p) p<<1
#define rs(p) p<<1|1
#define pi acos(-1.0)
#define pb push_back
#define mk make_pair
#define P pair < int , int >
using namespace std;
//const int mod=9901;
//const int inf=1e18;
const int M=1e8;
const int N=4e6+5;//??????.???? 4e8
struct node
{
int l,r,sum,add;
}e[N];
int n,m;
int a[N];
void pushup(int p)
{
e[p].sum=e[ls(p)].sum+e[rs(p)].sum;
}
void spread(int p)
{
if(e[p].add)
{
e[ls(p)].sum+=e[p].add*(e[ls(p)].r-e[ls(p)].l+1);
e[rs(p)].sum+=e[p].add*(e[rs(p)].r-e[rs(p)].l+1);
e[ls(p)].add+=e[p].add;
e[rs(p)].add+=e[p].add;
e[p].add=0;
}
}
void bulid(int p,int l,int r)
{
e[p].l=l,e[p].r=r;
if(l==r)
{
scanf("%lld",&a[l]);//
return;
}
int mid=(l+r)>>1;
bulid(ls(p),l,mid);
bulid(rs(p),mid+1,r);
pushup(p);
}
void change(int p,int l,int r,int d)
{
if(l<=e[p].l&&e[p].r<=r)
{
e[p].add+=d;
e[p].sum+=d*(e[p].r-e[p].l+1);
return;
}
spread(p);
int mid=(e[p].l+e[p].r)>>1;
if(l<=mid) change(ls(p),l,r,d);
if(mid<r) change(rs(p),l,r,d);
pushup(p);
}
int ask(int p,int l,int r)
{
if(l<=e[p].l&&e[p].r<=r) return e[p].sum;
spread(p);
int mid=(e[p].l+e[p].r)>>1;
int ans=0;
if(l<=mid) ans+=ask(ls(p),l,r);
if(mid<r) ans+=ask(rs(p),l,r);
return ans;
}
void solve()
{
cin>>n>>m;
bulid(1,1,n);
while(m--)
{
int op,x,y,k,d;
scanf("%lld%lld",&op,&x);
if(op==1)
{
scanf("%lld%lld%lld",&y,&k,&d);
change(1,x,x,k);
if(x<y) change(1,x+1,y,d);
int len=y-x+1;
if(y!=n) change(1,y+1,y+1,-(k+d*(len-1)));
}
if(op==2) printf("%lld\n",ask(1,1,x)+a[x]);
}
}
signed main()
{
int T=1;
// cin>>T;
for(int index=1;index<=T;index++)
{
solve();
// puts("");
}
return 0;
}
/*
*/