Rip Van Winkle was fed up with everything except programming. One day he found a problem which required to perform three types of update operations (A, B, C), and one query operation S over an array data[]. Initially all elements of data are equal to 0. Though Rip Van Winkle is going to sleep for 20 years, and his code is also super slow, you need to perform the same update operations and output the result for the query operation S in an efficient way.
long long data[250001];
void A( int st, int nd ) {
for( int i = st; i <= nd; i++ ) data[i] = data[i] + (i - st + 1);
}
void B( int st, int nd ) {
for( int i = st; i <= nd; i++ ) data[i] = data[i] + (nd - i + 1);
}
void C( int st, int nd, int x ) {
for( int i = st; i <= nd; i++ ) data[i] = x;
}
long long S( int st, int nd ) {
long long res = 0;
for( int i = st; i <= nd; i++ ) res += data[i];
return res;
}
Input
The first line of input will contain T (≤ 4*105) denoting the number of operations. Each of the next T lines starts with a character ('A', 'B', 'C' or 'S'), which indicates the type of operation. Character 'A', 'B' or 'S' will be followed by two integers, st and nd in the same line. Character 'C' is followed by three integers, st, nd and x. It's assumed that, 1 ≤ st ≤ nd ≤ 250000 and -105 ≤ x ≤ 105. The meanings of these integers are explained by the code of Rip Van Winkle.
Output
For each line starting with the character 'S', print S( st, nd ) as defined in the code. Dataset is huge, so use faster I/O methods.
Sample Input | Output for Sample Input |
7 A 1 4 B 2 3 S 1 3 C 3 4 -2 S 2 4 B 1 3 S 2 4 | 9 0 3 |
题意:三种操作,A是从l到r增加一个从1、2、3...B是从l到r增加r-l+1,r-l,r-l-1...C是从l到r赋值为x,S询问l到r的和
思路:在一个节点处,记录这个节点中到l到r的等差数列的第一个数,和等差,因为等差数列家伙说那个等差数列还是等差数列。然后赋值的操作优先于等差的操作,每次向下更新时,如果一个节点的子节点有赋值,先处理子节点的赋值,避免出现先赋值后加上等差这样的混乱情况。
AC代码如下:
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
struct node
{
int l,r;
ll a,b,c,sum;
bool flag1,flag2;
}tree[2000010];
char s[10];
void build(int o,int l,int r)
{
tree[o].l=l;
tree[o].r=r;
tree[o].flag1=tree[o].flag2=0;
if(l==r)
return;
int mi=(l+r)/2;
build(o<<1,l,mi);
build(o<<1|1,mi+1,r);
}
void down(int o)
{
//printf("%d %d %d %d %d %d %d %d\n",o,tree[o].l,tree[o].r,tree[o].flag1,tree[o].flag2,tree[o].a,tree[o].b,tree[o].c);
if(tree[o].l==tree[o].r)
{
tree[o].flag1=tree[o].flag2=0;
return;
}
if(tree[o].flag2)
{
tree[o].flag1=tree[o].flag2=0;
tree[o<<1].flag2=tree[o<<1|1].flag2=1;
tree[o<<1].a=tree[o<<1|1].a=0;
tree[o<<1].b=tree[o<<1|1].b=0;
tree[o<<1].c=tree[o<<1|1].c=tree[o].c;
tree[o<<1].sum=tree[o].c*(tree[o<<1].r-tree[o<<1].l+1);
tree[o<<1|1].sum=tree[o].c*(tree[o<<1|1].r-tree[o<<1|1].l+1);
}
else if(tree[o].flag1)
{
if(tree[o<<1].flag2)
down(o<<1);
if(tree[o<<1|1].flag2)
down(o<<1|1);
tree[o].flag1=0;
tree[o<<1].flag1=tree[o<<1|1].flag1=1;
tree[o<<1].a+=tree[o].a; tree[o<<1].b+=tree[o].b;
tree[o<<1|1].a+=tree[o].a+(tree[o<<1|1].l-tree[o<<1].l)*tree[o].b; tree[o<<1|1].b+=tree[o].b;
tree[o<<1].sum+=(tree[o].a*2+(tree[o<<1].r-tree[o<<1].l)*tree[o].b)*(tree[o<<1].r-tree[o<<1].l+1)/2;
tree[o<<1|1].sum+=((tree[o].a+(tree[o<<1|1].l-tree[o<<1].l)*tree[o].b)*2+(tree[o<<1|1].r-tree[o<<1|1].l)*tree[o].b)*(tree[o<<1|1].r-tree[o<<1|1].l+1)/2;
tree[o].a=tree[o].b=0;
}
}
void update1(int o,int l,int r,ll val,ll fen)
{
//printf("%d %d %d %d %d\n",o,l,r,tree[o].l,tree[o].r);
down(o);
if(tree[o].l==l && tree[o].r==r)
{
tree[o].a+=val;
tree[o].b+=fen;
tree[o].sum+=(val*2+(r-l)*fen)*(r-l+1)/2;
tree[o].flag1=1;
return;
}
int mi=(tree[o].l+tree[o].r)/2;
if(r<=mi)
update1(o<<1,l,r,val,fen);
else if(l>mi)
update1(o<<1|1,l,r,val,fen);
else
{
update1(o<<1,l,mi,val,fen);
update1(o<<1|1,mi+1,r,val+(mi+1-l)*fen,fen);
}
tree[o].sum=tree[o<<1].sum+tree[o<<1|1].sum;
}
void update2(int o,int l,int r,ll val)
{
down(o);
if(tree[o].l==l && tree[o].r==r)
{
tree[o].c=val;
tree[o].flag2=1;
tree[o].sum=val*(r-l+1);
return;
}
int mi=(tree[o].l+tree[o].r)/2;
if(r<=mi)
update2(o<<1,l,r,val);
else if(l>mi)
update2(o<<1|1,l,r,val);
else
{
update2(o<<1,l,mi,val);
update2(o<<1|1,mi+1,r,val);
}
tree[o].sum=tree[o<<1].sum+tree[o<<1|1].sum;
}
ll query(int o,int l,int r)
{
down(o);
if(tree[o].l==l && tree[o].r==r)
return tree[o].sum;
int mi=(tree[o].l+tree[o].r)/2;
if(r<=mi)
return query(o<<1,l,r);
else if(l>mi)
return query(o<<1|1,l,r);
else
return query(o<<1,l,mi)+query(o<<1|1,mi+1,r);
}
int main()
{
int n,m,i,j,k,l,r;
ll val;
build(1,1,250000);
scanf("%d",&n);
while(n--)
{
scanf("%s%d%d",s,&l,&r);
if(s[0]=='A')
update1(1,l,r,1,1);
else if(s[0]=='B')
update1(1,l,r,r-l+1,-1);
else if(s[0]=='C')
{
scanf("%lld",&val);
update2(1,l,r,val);
}
else
printf("%lld\n",query(1,l,r));
//for(i=1;i<=4;i++)
// printf("%lld ",query(1,i,i));
//printf("\n");
}
}