题目链接 : POJ3468
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 43012 | Accepted: 12552 | |
Case Time Limit: 2000MS |
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 4
Sample Output
4 55 9 15
Hint
Source
裸的线段树题 。不过也用splay写了一发
线段树 ver
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
struct p
{
int l,r;
long long dat,laz;
}tree[400050];
long long creat_tree(int i,int l,int r)
{
tree[i].laz=0;
tree[i].l=l;tree[i].r=r;
if(l==r) scanf("%lld",&tree[i].dat);
else
{
tree[i].dat=creat_tree(2*i+1,l,(l+r)/2);
tree[i].dat+=creat_tree(2*i+2,(l+r)/2+1,r);
}
return tree[i].dat;
}
long long query(int i,int l,int r)
{
if(tree[i].l==l && tree[i].r==r) return tree[i].dat+tree[i].laz*(r-l+1);
else
{
int mid=(tree[i].l+tree[i].r)/2;
if(l>mid) return query(2*i+2,l,r)+tree[i].laz*(r-l+1);
else if(r<=mid) return query(2*i+1,l,r)+tree[i].laz*(r-l+1);
else return query(2*i+1,l,mid)+query(2*i+2,mid+1,r)+tree[i].laz*(r-l+1);
}
}
void updat(int i,int l,int r,long long x)
{
if(tree[i].l==l && tree[i].r==r) tree[i].laz+=x;
else
{
tree[i].dat+=x*(r-l+1);
int mid=(tree[i].l+tree[i].r)/2;
if(l>mid) updat(2*i+2,l,r,x);
else if(r<=mid) updat(2*i+1,l,r,x);
else updat(2*i+1,l,mid,x),updat(2*i+2,mid+1,r,x);
}
}
int main()
{
int n,q,l,r;
char str[4];
long long x;
scanf("%d%d",&n,&q);
creat_tree(0,0,n-1);
for(int i=0;i<q;i++)
{
scanf("%s",str);
if(str[0]=='Q')
{
scanf("%d%d",&l,&r);
l--;r--;
printf("%lld\n",query(0,l,r));
}
else
{
scanf("%d%d%lld",&l,&r,&x);
l--;r--;
updat(0,l,r,x);
}
}
return 0;
}
splay ver
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
using namespace std;
int num[100005],val[100005],laz[100005];
long long sum[100005];
int siz[100005],ch[100005][2],pre[100005];
int root,top1;
void push_up(int x)
{
siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
sum[x]=val[x]+sum[ch[x][0]]+sum[ch[x][1]];
}
void push_down(int x)
{
if(laz[x])
{
val[x]+=laz[x];
laz[ch[x][0]]+=laz[x];
laz[ch[x][1]]+=laz[x];
sum[ch[x][0]]+=(long long)laz[x]*siz[ch[x][0]];
sum[ch[x][1]]+=(long long)laz[x]*siz[ch[x][1]];
laz[x]=0;
}
}
void rotate(int x,int f) //f=1 right
{
int y=pre[x];
push_down(y);
push_down(x);
ch[y][!f]=ch[x][f];
pre[ch[x][f]]=y;
pre[x]=pre[y];
if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x;
ch[x][f]=y;
pre[y]=x;
push_up(y);
}
void splay(int x,int to)
{
push_down(x);
while(pre[x]!=to)
{
if(pre[pre[x]]==to) rotate(x,ch[pre[x]][0]==x);
else
{
int f=(ch[pre[pre[x]]][0]==pre[x]);
if(ch[pre[x]][f]==x) rotate(x,!f),rotate(x,f);
else rotate(x,f),rotate(x,f);
}
}
push_up(x);
if(to==0) root=x;
}
void rotate_to(int k,int to)
{
int x=root;
push_down(x);
while(siz[ch[x][0]]!=k)
{
if(k<siz[ch[x][0]]) x=ch[x][0];
else {k-=siz[ch[x][0]]+1;x=ch[x][1];}
push_down(x);
}
splay(x,to);
}
void newnode(int &x,int c)
{
x=++top1;
laz[x]=ch[x][0]=ch[x][1]=pre[x]=0;
siz[x]=1;
val[x]=sum[x]=c;
}
void creat_tree(int &x,int l,int r,int f)
{
if(l>r) return ;
int mid=(l+r)>>1;
newnode(x,num[mid]);
pre[x]=f;
creat_tree(ch[x][0],l,mid-1,x);
creat_tree(ch[x][1],mid+1,r,x);
push_up(x);
}
void init(int n)
{
ch[0][0]=ch[0][1]=pre[0]=0;
siz[0]=laz[0]=sum[0]=0;
root=top1=0;
newnode(root,0);
newnode(ch[root][1],0);
pre[top1]=root;siz[root]=2;
for(int i=0;i<n;i++)
scanf("%d",num+i);
creat_tree(ch[ch[root][1]][0],0,n-1,ch[root][1]);
push_up(ch[root][1]);
push_up(root);
}
long long query(int l,int r)
{
rotate_to(l-1,0);
rotate_to(r+1,root);
return sum[ch[ch[root][1]][0]];
}
void updat(int l,int r,int c)
{
rotate_to(l-1,0);
rotate_to(r+1,root);
laz[ch[ch[root][1]][0]]+=c;
sum[ch[ch[root][1]][0]]+=(long long)c*siz[ch[ch[root][1]][0]];
}
int main()
{
int n,q;
char str[5];
scanf("%d%d",&n,&q);
init(n);
int l,r,c;
for(int i=0;i<q;i++)
{
scanf("%s",str);
if(str[0]=='Q')
{
scanf("%d%d",&l,&r);
printf("%lld\n",query(l,r));
}
else
{
scanf("%d%d%d",&l,&r,&c);
updat(l,r,c);
}
}
return 0;
}