BZOJ1798: [Ahoi2009]Seq 维护序列seq 线段树

Description

老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成。 有长为N的数列,不妨设为a1,a2,…,aN 。有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数全部加一个值; (3)询问数列中的一段数的和,由于答案可能很大,你只需输出这个数模P的值。

Input

第一行两个整数N和P(1≤P≤1000000000)。第二行含有N个非负整数,从左到右依次为a1,a2,…,aN, (0≤ai≤1000000000,1≤i≤N)。第三行有一个整数M,表示操作总数。从第四行开始每行描述一个操作,输入的操作有以下三种形式: 操作1:“1 t g c”(不含双引号)。表示把所有满足t≤i≤g的ai改为ai×c (1≤t≤g≤N,0≤c≤1000000000)。 操作2:“2 t g c”(不含双引号)。表示把所有满足t≤i≤g的ai改为ai+c (1≤t≤g≤N,0≤c≤1000000000)。 操作3:“3 t g”(不含双引号)。询问所有满足t≤i≤g的ai的和模P的值 (1≤t≤g≤N)。 同一行相邻两数之间用一个空格隔开,每行开头和末尾没有多余空格。

Output

对每个操作3,按照它在输入中出现的顺序,依次输出一行一个整数表示询问结果。

Sample Input

7 43
1 2 3 4 5 6 7
5
1 2 5 5
3 2 4
2 3 7 9
3 1 3
3 4 7

Sample Output

2
35
8

HINT

【样例说明】

初始时数列为(1,2,3,4,5,6,7)。
经过第1次操作后,数列为(1,10,15,20,25,6,7)。
对第2次操作,和为10+15+20=45,模43的结果是2。
经过第3次操作后,数列为(1,10,24,29,34,15,16}
对第4次操作,和为1+10+24=35,模43的结果是35。
对第5次操作,和为29+34+15+16=94,模43的结果是8。



测试数据规模如下表所示

数据编号 1 2 3 4 5 6 7 8 9 10
N= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000
M= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000

 

Solution

就是两个$tag$的线段树。 注意优先级就好。然后mul标记的初始值要赋值为1就好

#include <bits/stdc++.h>

#define ll long long
#define inf 0x3f3f3f3f
#define il inline

namespace io {

    #define in(a) a=read()
    #define out(a) write(a)
    #define outn(a) out(a),putchar('
')

    #define I_int ll
    inline I_int read() {
        I_int x = 0 , f = 1 ; char c = getchar() ;
        while( c < '0' || c > '9' ) { if( c == '-' ) f = -1 ; c = getchar() ; }
        while( c >= '0' && c <= '9' ) { x = x * 10 + c - '0' ; c = getchar() ; }
        return x * f ;
    }
    char F[ 200 ] ;
    inline void write( I_int x ) {
        if( x == 0 ) { putchar( '0' ) ; return ; }
        I_int tmp = x > 0 ? x : -x ;
        if( x < 0 ) putchar( '-' ) ;
        int cnt = 0 ;
        while( tmp > 0 ) {
            F[ cnt ++ ] = tmp % 10 + '0' ;
            tmp /= 10 ;
        }
        while( cnt > 0 ) putchar( F[ -- cnt ] ) ;
    }
    #undef I_int

}
using namespace io ;

using namespace std ;

#define N 100010
#define lc (rt<<1)
#define rc (rt<<1|1)

ll n , mod , a[ N ] , m ;
struct tree {
    int l , r ;
    ll mul , add , sum ;
} t[ N << 2 ] ;

void pushup( int rt ) { t[ rt ].sum = ( t[ lc ].sum + t[ rc ].sum ) % mod ; }

void pushdown( int ln , int rn , int rt ) {
    ll &x1 = t[ rt ].add , &x2 = t[ rt ].mul ;
    t[ lc ].sum = ( t[ lc ].sum * x2 + ln * x1 ) % mod ;
    t[ rc ].sum = ( t[ rc ].sum * x2 + rn * x1 ) % mod ;
    t[ lc ].mul = ( t[ lc ].mul * x2 ) % mod ;
    t[ rc ].mul = ( t[ rc ].mul * x2 ) % mod ;
    t[ lc ].add = ( t[ lc ].add * x2 + x1 ) % mod ;
    t[ rc ].add = ( t[ rc ].add * x2 + x1 ) % mod ;
    x1 = 0 ; x2 = 1 ;
}

void build( int l , int r , int rt ) {
    t[ rt ].l = l ; t[ rt ].r = r ;  t[ rt ].mul = 1 ;
    int mid = ( l + r ) >> 1 ;
    if( l == r ) { t[ rt ].sum = a[ l ] ; return ; }
    build( l , mid , lc ) ;
    build( mid + 1 , r , rc ) ;
    pushup( rt ) ;
}

void upd( int opt , int L , int R , int c , int rt ) {
    //1 - add , 2 - mul 
    int l = t[ rt ].l , r = t[ rt ].r , mid = ( l + r ) >> 1 ;
    if( L <= l && r <= R ) {
        if( opt == 1 ) {
            t[ rt ].sum = ( t[ rt ].sum + c * ( r - l + 1 ) ) % mod ;
            t[ rt ].add = ( t[ rt ].add + c ) % mod ;
        } 
        else { 
            t[ rt ].sum = ( t[ rt ].sum * c ) % mod ; 
            t[ rt ].add = ( t[ rt ].add * c ) % mod ;
            t[ rt ].mul = ( t[ rt ].mul * c ) % mod ; 
        }
        return ;
    }
    pushdown( mid - l + 1 , r - mid , rt ) ;
    if( L <= mid ) upd( opt , L , R , c , lc ) ;
    if( R > mid ) upd( opt , L , R , c , rc ) ;
    pushup( rt ) ;
} 

ll query( int L , int R , int rt ) {
    int l = t[ rt ].l , r = t[ rt ].r , mid = ( l + r ) >> 1 ;
    ll ans = 0 ;
    if( L <= l && r <= R ) return t[ rt ].sum % mod ;
    pushdown( mid - l + 1 , r - mid , rt ) ;
    if( L <= mid ) ans = ( ans + query( L , R , lc ) ) % mod ;
    if( R > mid ) ans = ( ans + query( L , R , rc ) ) % mod ;
    return ans % mod ;
}

int main() {
    n = read() , mod = read() ;
    for( int i = 1 ; i <= n ; i ++ ) a[ i ] = read() % mod ;
    build( 1 , n , 1 ) ; m = read() ;
    for( int i = 1 ; i <= m ; i ++ ) {
        ll opt = read() , t = read() , g = read() ;
        if( opt == 1 ) {
            ll c = read() % mod ;
            upd( 2 , t , g , c , 1 ) ;
        } else if( opt == 2 ) {
            ll c = read() % mod ;
            upd( 1 , t , g , c , 1 ) ;
        } else outn( query( t , g , 1 ) ) ;
    }
    return 0 ;
} 
原文地址:https://www.cnblogs.com/henry-1202/p/BZOJ1798.html