不要想复杂了,只需要用个flag标记区间是否全部相同就可以了。 注意查询时不是区间相同且flag=1时才计算,而是在区间之内。WA好几次
#include#include #include #include #include #include #include #include #include #include #include #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) #define mem(a) memset(a, 0, sizeof(a)) #define eps 1e-5 #define INF 0x1f1f1f1f #define M 100005 using namespace std; int Case = 1; int t, n, m; struct Node { int l, r, mid, f, v; }l[M<<2]; void build(int a, int b, int r) { l[r].l = a, l[r].r = b, l[r].mid = (a+b)>>1; l[r].f = 1, l[r].v = 0; if(a == b) { return ; } build(a, l[r].mid, r<<1); build(l[r].mid+1, b, r<<1|1); } int ff, mod = 10007, z; void solve(int r) { if(ff == 1) { l[r].v = (l[r].v + z) % mod; } else if(ff == 2) { l[r].v = (l[r].v * z) % mod; } else if(ff == 3) { l[r].v = z % mod; } } void update(int a, int b, int r) { if(l[r].l == l[r].r) { solve(r); return ; } if(l[r].l == a && l[r].r == b) { if(l[r].f == 1) { solve(r); } else { if(ff == 3) { solve(r); l[r].f = 1; } else { update(a, l[r].mid, r<<1); update(l[r].mid+1, b, r<<1|1); } } return ; } if(l[r].f == 1) { l[r].f = 0; l[r<<1].v = l[r].v; l[r<<1|1].v = l[r].v; l[r<<1].f = l[r<<1|1].f = 1; } if(a > l[r].mid) { update(a, b, r<<1|1); } else if(b <= l[r].mid) { update(a, b, r<<1); } else { update(a, l[r].mid, r<<1); update(l[r].mid+1, b, r<<1|1); } } int query(int a, int b, int r) { int res = 0; if(l[r].l == l[r].r) { int tmp = 1; for(int i = 0; i < z; i++) { tmp = (tmp * l[r].v) % mod; } return tmp; } if(a >= l[r].l && b <= l[r].r) { if(l[r].f == 1) { int tmp = 1; int tot = b-a + 1; for(int i = 0; i < z; i++) { tmp = (tmp * l[r].v) % mod; } return (tot*tmp) % mod; } } if(a > l[r].mid) { res = query(a, b, r<<1|1); } else if(b <= l[r].mid) { res = query(a, b, r<<1); } else { res = (query(a, l[r].mid, r<<1) + query(l[r].mid+1, b, r<<1|1)) % mod; } return res; } int main() { while(~scanf("%d%d", &n, &m)) { if(n == 0 && m == 0) { break; } build(1, n, 1); while(m--) { int x, y; scanf("%d%d%d%d", &ff, &x, &y, &z); if(ff < 4) { update(x, y, 1); } else { int ans = query(x, y, 1); printf("%d\n", ans); } } } return 0; }