區間加法,區間求和。html
這題。。。也與分塊1一模一樣。。。數組
當有修改時,對於完整的塊,直接維護一個數組v記錄整個塊加過的數(每塊共同的加數)與s記錄每一個塊的和(不算共同加數),不完整的就直接暴力在原數組a上直接加,而且別忘了給s也加上。詢問時,對於不完整的塊,直接暴力加(別忘了共同加數),完整的塊對於每一個區間ans加上 (區間和 + 共同加數 * 大小)就能夠了。spa
#include<cstdio> #include<cmath> using namespace std; #define MAXN 50005 #define LL long long int n, t, m; LL a[MAXN], p[MAXN], v[500], s[500]; LL opt, l, r, c; void Add( int l, int r, int c ){ if ( p[l] == p[r] ){ for ( int i = l; i <= r; ++i ) a[i] += c, s[p[i]] += c; return; } for ( int i = l; p[l] == p[i]; ++i ) a[i] += c, s[p[i]] += c; for ( int i = r; p[r] == p[i]; --i ) a[i] += c, s[p[i]] += c; for ( int i = p[l] + 1; i < p[r]; ++i ) v[i] += c; } LL query( int l, int r, LL c ){ if ( p[l] == p[r] ){ LL ans(0); for ( int i = l; i <= r; ++i ) ans += ( a[i] + v[p[i]] ) % c, ans %= c; return ans; } int ans(0); for ( int i = l; p[i] == p[l]; ++i ) ans += ( a[i] + v[p[i]] ) % c, ans %= c; for ( int i = r; p[i] == p[r]; --i ) ans += ( a[i] + v[p[i]] ) % c, ans %= c; for ( int i = p[l] + 1; i < p[r]; ++i ) ans += ( s[i] + v[i] * m ) % c, ans %= c; return ans % c; } int main(){ scanf( "%d", &n ); m = (int)sqrt(n); for ( int i = 1; i <= n; ++i ) scanf( "%lld", &a[i] ), p[i] = ( i - 1 ) / m + 1, s[p[i]] += a[i]; for ( int i = 1; i <= n; ++i ){ scanf( "%d%lld%lld%lld", &opt, &l, &r, &c ); if ( opt ) printf( "%lld\n", query( l, r, c + 1 ) ); else Add( l, r, c ); } }
數列分塊入門1code
數列分塊入門2htm
數列分塊入門3blog
數列分塊入門4 <-get
數列分塊入門5io
數列分塊入門6入門
數列分塊入門7class