数列分块入门 7
分块训练
两个tag,优先算乘法的tag。
暴力更新的时候要把乘法标记和加法标记都清空。
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 100005;
const int MOD = 10007;
ll a[N], add[N], mul[N];
int n, t, lt[N], rt[N], pos[N];
void adder(int l, int r, ll d){
d %= MOD;
int p = pos[l], q = pos[r];
if(p == q){
for(int i = lt[p]; i <= rt[p]; i ++)
a[i] = ((a[i] * mul[p]) % MOD + add[p]) % MOD;
for(int i = l; i <= r; i ++)
a[i] = (a[i] + d) % MOD;
add[p] = 0, mul[p] = 1;
}
else{
for(int i = p + 1; i <= q - 1; i ++)
add[i] = (add[i] + d) % MOD;
for(int i = lt[p]; i <= rt[p]; i ++)
a[i] = ((a[i] * mul[p]) % MOD + add[p]) % MOD;
for(int i = l; i <= rt[p]; i ++)
a[i] = (a[i] + d) % MOD;
for(int i = lt[q]; i <= rt[q]; i ++)
a[i] = ((a[i] * mul[q]) % MOD + add[q]) % MOD;
for(int i = lt[q]; i <= r; i ++)
a[i] = (a[i] + d) % MOD;
add[p] = add[q] = 0, mul[p] = mul[q] = 1;
}
}
void muler(int l, int r, ll d){
d %= MOD;
int p = pos[l], q = pos[r];
if(p == q){
for(int i = lt[p]; i <= rt[p]; i ++)
a[i] = ((a[i] * mul[p]) % MOD + add[p]) % MOD;
for(int i = l; i <= r; i ++)
a[i] = (a[i] * d) % MOD;
add[p] = 0, mul[p] = 1;
}
else{
for(int i = p + 1; i <= q - 1; i ++)
mul[i] = (mul[i] * d) % MOD, add[i] = (add[i] * d) % MOD;
for(int i = lt[p]; i <= rt[p]; i ++)
a[i] = ((a[i] * mul[p]) % MOD + add[p]) % MOD;
for(int i = l; i <= rt[p]; i ++)
a[i] = (a[i] * d) % MOD;
for(int i = lt[q]; i <= rt[q]; i ++)
a[i] = ((a[i] * mul[q]) % MOD + add[q]) % MOD;
for(int i = lt[q]; i <= r; i ++)
a[i] = (a[i] * d) % MOD;
add[p] = add[q] = 0, mul[p] = mul[q] = 1;
}
}
ll query(int k){
return (a[k] * mul[pos[k]] % MOD + add[pos[k]] % MOD) % MOD;
}
int main(){
//freopen("data.txt", "r", stdin);
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cin >> n;
for(int i = 1; i <= n; i ++) cin >> a[i];
t = (int)sqrt(n);
for(int i = 1; i <= t; i ++){
lt[i] = (i - 1) * t + 1;
rt[i] = i * t;
}
if(rt[t] < n) t ++, lt[t] = rt[t - 1] + 1, rt[t] = n;
for(int i = 1; i <= t; i ++){
mul[i] = 1, add[i] = 0;
for(int j = lt[i]; j <= rt[i]; j ++){
pos[j] = i;
}
}
for(int i = 1; i <= n; i ++){
int opt, l, r; ll c;
cin >> opt >> l >> r >> c;
if(opt == 0) adder(l, r, c);
else if(opt == 1) muler(l, r, c);
else cout << query(r) << endl;
}
return 0;
}