Problem 1. setmod
Input file: setmod.in
Output file: setmod.out
Time limit: 2 seconds
Memory limit: 256 MB
给你一个序列:a1 a2 a3 : : : an,有m 个操作,操作如下:
• modify l r x 将区间[l; r] 中的每个数修改为x
• change l r x 将区间[l; r] 中的每个数加上x
• query l r 询问区间[l; r] 中的和
Input
第1 行2 个整数:n m,表示序列长的和操作数.
第2 行n 个整数:a1 a2 a3 : : : an,表示初始序列.
接下来m 行,每行是上面三种操作中的一种.
Output
对于每个询问操作,输出其结果.
Sample
setmod.in
3 3
1 2 3
change 1 3 2
modify 3 3 3
query 1 3
setmod.out
10
Note
• 对于30% 的数据,1 n;m 103
• 对于100% 的数据,1 n;m 105,1 ai; x n,1 l r n
需要维护的:
sum:区间的和
type:现在的状态(有增量或赋值后)
delta:增量存储态,存增量
value:赋值大小
讨论秩序:
所有状态加赋值均变成赋值态
增量加增量仍为增量
#include<cstdio>
using namespace std;
typedef long long ll;
const int N = 1000000 + 5;
inline int read() {
int x = 0, f = 1;char ch = getchar();
while( ch < '0' || ch > '9'){if(ch == '-')f=-1;ch=getchar();}
while(ch>='0' && ch <='9'){x = x*10+ch-'0';ch = getchar();}
return x*f;
}
struct Node;
void modify( Node *nd, int lf, int rg, int L, int R, ll value );
void change( Node *nd, int lf, int rg, int L, int R, ll delta );
struct Node{
ll sum;
ll value,delta;
Node *ls, *rs;
int type;
void update(){ sum = ls->sum + rs->sum; }
void pushdown( int lf, int rg ){
if( type == 0 ) return;
int mid = (lf+rg) >> 1;
if( type == 1 ){
change( ls, lf, mid, lf, mid, delta );
change( rs, mid+1, rg, mid+1, rg, delta );
type = 0;
} else{
modify( ls, lf, mid, lf, mid, value );
modify( rs, mid+1, rg, mid+1, rg, value );
type = 0;
}
}
}pool[N*2],*tail = pool,*root;
int m,n,aa[N];
Node *build( int lf, int rg ){
Node *nd = ++tail;
if( lf == rg ){
nd->sum = aa[lf];
nd->type = 0;
} else{
int mid = (lf+rg) >> 1;
nd->ls = build( lf, mid );
nd->rs = build( mid+1, rg);
nd->type = 0;
nd->update();
}
return nd;
}
void modify( Node *nd, int lf, int rg, int L, int R, ll value ){
if( lf >= L && rg <= R ){
nd->sum = (ll)(rg-lf+1)*value;
nd->type = 2; nd->value = value; return ;
}
int mid = (lf+rg) >> 1;
nd->pushdown(lf,rg);
if( L <= mid ) modify( nd->ls, lf, mid, L, R, value );
if( mid < R ) modify( nd->rs, mid+1, rg, L, R, value );
nd->update();
}
void change( Node *nd, int lf, int rg, int L, int R, ll delta ){
if( L <= lf && rg <= R ){
nd->sum += (ll)(rg-lf+1)*delta;
if( nd->type == 0 ){
nd->type = 1;nd->delta = delta;
} else if( nd->type == 1 ) nd->delta += delta;
else if( nd->type == 2 ) nd->value += delta;
return ;
}
int mid = (lf+rg) >> 1;
nd->pushdown(lf,rg);
if( L <= mid ) change( nd->ls, lf, mid, L, R, delta );
if( mid < R ) change( nd->rs, mid+1, rg, L, R, delta );
nd->update();
}
ll query( Node *nd, int lf, int rg,int L, int R ){
if( L <= lf && rg <= R )
return nd->sum;
int mid = (lf+rg) >> 1;
nd->pushdown(lf,rg);
ll rt = 0;
if( mid >= L ) rt += query( nd->ls, lf, mid, L, R );
if( mid < R ) rt+= query( nd->rs, mid+1, rg, L, R );
return rt;
}
int main() {
freopen("setmod.in", "r", stdin);
freopen("setmod.out", "w", stdout);
scanf( "%d%d", &n, &m );
for( int i = 1; i <= n; i++ )
scanf( "%d", aa + i );
root = build( 1, n );
while( m-- ) {
char ss[10];
int l, r, delta, value;
scanf( "%s", ss );
if( ss[0] == 'c' ) {
l = read(); r = read(); delta = read();
change( root, 1, n, l, r, delta );
} else if( ss[0] == 'm' ) {
l = read(); r = read(); value = read();
modify( root, 1, n, l, r, value );
} else {
l = read(); r = read();
printf( "%I64d\n", query( root, 1, n, l, r ) );
}
}
return 0;
}