1 条题解
-
1louhancheng LV 7 MOD @ 2018-08-12 09:16:38
Loj #6280. 数列分块入门 4 传送门
My Blog
对每一块记录总和s[i],(注意:这里的s[i]不包括p[i]的值,当然,也可以写成包括的形式),区间加时将不完整块暴力,对于完整块(不包括两边界,两边界即使完整也是用暴力)加上p[i] * m + s[i],再取mod就可以了。详情请见代码:#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] ){//l、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;//左边界(不一定不完整)每个数加c for ( int i = r; p[r] == p[i]; --i ) a[i] += c, s[p[i]] += c;//右边界(也不一定完整)每个数加c for ( int i = p[l] + 1; i < p[r]; ++i ) v[i] += c;//中间的完整块加c } LL query( int l, int r, LL c ){ if ( p[l] == p[r] ){//l、r在同一块中直接暴力 LL ans(0); for ( int i = l; i <= r; ++i ) ans += ( a[i] + v[p[i]] ) % c, ans %= c; return ans; } LL 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;//中间完整块加上v[i] * m(完整块的大小都为m)与区间和s[i] 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];//分成√n块,每一块s[i]加上初始值 for ( int i = 1; i <= n; ++i ){ scanf( "%lld%lld%lld%lld", &opt, &l, &r, &c ); if ( opt ) printf( "%lld\n", query( l, r, c + 1 ) ); else Add( l, r, c ); } return 0; }
- 1