Description

D | Rip Van Winkle's Code |
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 |
Problem Setter: Anindya Das, Special Thanks: Tanaeem M Moosa, Jane Alam Jan
A操作和B操作可以看成俩部分,一部分是+(1-st) 或者 +(nd-1) ,另一部分就是+ i或者-i,这部分可以看成+1或者-1来乘以这个区间的下标和,等差列。
C操作就是一般的区间更新了。
用了俩个lazy标记,俩lazy标记不能共存,
/*
*=====================
*File Name:a.cpp
*Author: qqspeed
*Date: 2014年 07月 10日 星期四 14:13:38 CST
*Source: http://vjudge.net/contest/view.action?cid=41967#problem/O
*=====================
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
typedef long long ll;
#define rep(i,s,t) for(int i=s;i<t;i++)
#define red(i,s,t) for(int i=s-1;i>=t;i--)
#define ree(i,now) for(int i=head[now];i!=-1;i=edge[i].next)
#define clr(a,v) memset(a,v,sizeof a)
#define L t<<1
#define R t<<1|1
#define MID int mid=(l+r)>>1
#define max(a,b) (a<b?b:a)
#define min(a,b) (a<b?a:b)
#define SQR(a) ((a)*(a))
inline int input(){
int ret=0;bool isN=0;char c=getchar();
while(c<'0' || c>'9'){
if(c=='-') isN=1;
c=getchar();
}
while(c>='0' && c<='9'){
ret=ret*10+c-'0';
c=getchar();
}
return isN?-ret:ret;
}
inline void output(ll x){
if(x<0){
putchar('-');x=-x;
}
int len=0,data[20];
while(x){
data[len++]=x%10;x/=10;
}
if(!len) data[len++]=0;
while(len--)
putchar(data[len]+48);
putchar('\n');
}
const int MAXN=250005;
int t,x,y,z;
char op[2];
int len[MAXN<<2],lazy_i[MAXN<<2],lazy_add[MAXN<<2],lazy[MAXN<<2];
ll pre_sum[MAXN<<2],data[MAXN<<2];
inline void pushup(int t){
data[t]=data[L]+data[R];
}
inline void pushdown(int t){
if(lazy[t]!=-MAXN){
lazy[L]=lazy[R]=lazy[t];
lazy_i[L]=lazy_i[R]=0;
lazy_add[L]=lazy_add[R]=0;
data[L]=len[L]*1LL*lazy[L];
data[R]=len[R]*1LL*lazy[R];
lazy[t]=-MAXN;
}
}
inline void push_down_i_add(int t){
if(len[t]!=1) pushdown(t);
if(lazy_i[t]!=0){
lazy_i[L]+=lazy_i[t];
lazy_i[R]+=lazy_i[t];
data[L]+=pre_sum[L]*1LL*lazy_i[t];
data[R]+=pre_sum[R]*1LL*lazy_i[t];
lazy_i[t]=0;
}
if(lazy_add[t]!=0){
lazy_add[L]+=lazy_add[t];
lazy_add[R]+=lazy_add[t];
data[L]+=len[L]*1LL*lazy_add[t];
data[R]+=len[R]*1LL*lazy_add[t];
lazy_add[t]=0;
}
}
inline void build(int t,int l,int r){
len[t]=r-l+1;
pre_sum[t]=(r+l)*1LL*len[t]>>1;
data[t]=lazy_add[t]=lazy_i[t]=0;
lazy[t]=-MAXN;
if(l!=r){
MID;
build(L,l,mid);
build(R,mid+1,r);
}
}
inline void modefiy_add(int t,int l,int r,int x,int y,int v,int vv){
if(l>=x && r<=y){
if(l!=r) pushdown(t);
lazy_i[t]+=vv;
lazy_add[t]+=v;
data[t]+=v*1LL*len[t];
data[t]+=vv*pre_sum[t];
}
else{
push_down_i_add(t);
pushdown(t);
MID;
if(y<=mid) modefiy_add(L,l,mid,x,y,v,vv);
else if(x>mid) modefiy_add(R,mid+1,r,x,y,v,vv);
else{
modefiy_add(L,l,mid,x,mid,v,vv);
modefiy_add(R,mid+1,r,mid+1,y,v,vv);
}
pushup(t);
}
}
inline void modefiy_eq(int t,int l,int r,int x,int y,int v){
if(l>=x && r<=y){
lazy_add[t]=lazy_i[t]=0;
lazy[t]=v;
data[t]=v*1LL*len[t];
}
else{
push_down_i_add(t);
pushdown(t);
MID;
if(y<=mid) modefiy_eq(L,l,mid,x,y,v);
else if(x>mid) modefiy_eq(R,mid+1,r,x,y,v);
else{
modefiy_eq(L,l,mid,x,mid,v);
modefiy_eq(R,mid+1,r,mid+1,y,v);
}
pushup(t);
}
}
inline ll query(int t,int l,int r,int x,int y){
if(l>=x && r<=y){
return data[t];
}
push_down_i_add(t);
pushdown(t);
MID;
if(y<=mid) return query(L,l,mid,x,y);
else if(x>mid) return query(R,mid+1,r,x,y);
else return query(L,l,mid,x,mid)+query(R,mid+1,r,mid+1,y);
}
int main(){
while(~scanf("%d",&t)){
build(1,1,250000);
while(t--){
scanf("%s",op);
if(op[0]=='A'){
x=input(),y=input();
modefiy_add(1,1,250000,x,y,1-x,1);
}
else if(op[0]=='B'){
x=input(),y=input();
modefiy_add(1,1,250000,x,y,1+y,-1);
}
else if(op[0]=='C'){
x=input(),y=input(),z=input();
modefiy_eq(1,1,250000,x,y,z);
}
else{
x=input(),y=input();
output(query(1,1,250000,x,y));
}
}
}
return 0;
}