Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4Sample Output
4 55 9 15这道题真的是用什么树都可以做,伸展树的每个节点维护add,sum,key信息,分别表示以该节点为根节点的整棵树的增加值,和,以及本节点的值.注意add标记在x号节点上时,x节点的sum和key就立即更新,这个add只是表示x节点的儿子节点们没有更新.
AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<iomanip>
#include<math.h>
#include<sstream>
using namespace std;
typedef long long ll;
typedef double ld;
using namespace std;
const int maxn=100000+100;
int a[maxn];
int n,q;
struct SplayTree
{
#define Key_Value ch[ch[root][1]][0]
int ch[maxn][2],pre[maxn],size[maxn],root,tot1;
int key[maxn];
int add[maxn];
ll sum[maxn];
void NewNode(int &r,int fa,int k)
{
r =++tot1;
pre[r]=fa;
add[r]=sum[r]=ch[r][0]=ch[r][1]=0;
size[r]=1;
key[r]=k;
}
void Update_Add(int r,int ADD)
{
if(r==0)return ;
key[r]+=ADD;
add[r]+=ADD;
sum[r]+=(long long)ADD*size[r];
}
void Push_Up(int r)
{
size[r]=1+size[ch[r][0]]+size[ch[r][1]];
sum[r] = sum[ch[r][0]]+sum[ch[r][1]]+key[r];
}
void Push_Down(int r)
{
if(add[r])
{
Update_Add(ch[r][0],add[r]);
Update_Add(ch[r][1],add[r]);
add[r]=0;
}
}
void Build(int &x,int l,int r,int fa)
{
if(l>r) return ;
int mid=(l+r)>>1;
NewNode(x,fa,a[mid]);
Build(ch[x][0],l,mid-1,x);
Build(ch[x][1],mid+1,r,x);
Push_Up(x);
}
void init()
{
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
root=tot1=0;
ch[root][0]=ch[root][1]=pre[root]=size[root]=key[root]=sum[root]=add[root]=0;
NewNode(root,0,-1);
NewNode(ch[root][1],root,-1);
Build(Key_Value,1,n,ch[root][1]);
Push_Up(ch[root][1]);
Push_Up(root);
}
void Rotate(int x,int kind)
{
int y=pre[x];
Push_Down(y);
Push_Down(x);
ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x;
pre[x]=pre[y];
ch[x][kind]=y;
pre[y]=x;
Push_Up(y);
}
void Splay(int r,int goal)
{
Push_Down(r);
while(pre[r]!=goal)
{
if(pre[pre[r]]==goal)
Rotate(r,ch[pre[r]][0]==r);
else
{
int y=pre[r];
int kind= ch[pre[y]][0]==y;
if(ch[y][kind]==r)
{
Rotate(r,!kind);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
Push_Up(r);
if(goal==0) root=r;
}
int Get_Kth(int r,int k)
{
Push_Down(r);
int t=size[ch[r][0]]+1;
if(k==t) return r;
else if(k<t) return Get_Kth(ch[r][0],k);
else return Get_Kth(ch[r][1],k-t);
}
void ADD(int l,int r,int D)
{
Splay(Get_Kth(root,l),0);
Splay(Get_Kth(root,r+2),root);
Update_Add(Key_Value,D);
Push_Up(ch[root][1]);
Push_Up(root);
}
long long Query(int l,int r)
{
Splay(Get_Kth(root,l),0);
Splay(Get_Kth(root,r+2),root);
return sum[Key_Value];
}
}st;
int main()
{
while(scanf("%d%d",&n,&q)==2)
{
st.init();
while(q--)
{
char op[20];
int x,y,z;
scanf("%s",op);
if(op[0]=='C')
{
scanf("%d%d%d",&x,&y,&z);
st.ADD(x,y,z);
}
else if(op[0]=='Q')
{
scanf("%d%d",&x,&y);
printf("%I64d\n",st.Query(x,y));
}
}
}
return 0;
}