### ChairMan's blog

By ChairMan, history, 2 years ago,

Hi, i've been trying to solve this problem — problem

however it's still gives me wrong answer on test 3. Could someone please tell me what's wrong with my code ? — code.

 » 2 years ago, # | ← Rev. 2 →   +4 You don't need to add the 'lazy' into the subtrees in your push function(assignment part). Here is the modified code: Code//debug for @ChairMan #include #define int long long using namespace std; int maxN = 2e5; int n; struct seg { vector t = vector (maxN * 4); vector lazy = vector (maxN * 4); vector cur = vector (maxN * 4); vector mark = vector (maxN * 4); void push(int x, long long range1, long long range2) { if(mark[x]) { mark[2 * x] = 1; mark[2 * x + 1] = 1; lazy[2 * x + 1] = lazy[x]; lazy[2 * x] = lazy[x]; t[2 * x] = cur[x] * range1 + lazy[x] * range1; t[2 * x + 1] = cur[x] * range2 + lazy[x] * range2; mark[x] = 0; cur[2 * x] = cur[x]; cur[2 * x + 1] = cur[x]; lazy[x] = 0; } lazy[2 * x + 1] += lazy[x]; lazy[2 * x] += lazy[x]; t[2 * x] += lazy[x] * range1; t[2 * x + 1] += lazy[x] * range2; lazy[x] = 0; } void assign(int x, int lx, int rx, int l, int r, long long v) { if(lx >= l && rx <= r) { t[x] = (rx - lx + 1) * v; lazy[x] = 0; mark[x] = 1; cur[x] = v; return; } if(lx > r || rx < l) { return; } int m = (lx + rx) >> 1; push(x, (m - lx + 1), (rx - m)); assign(2 * x, lx, m, l, r, v); assign(2 * x + 1, m + 1, rx, l, r, v); t[x] = t[2 * x + 1] + t[2 * x]; } void assign(int l, int r, long long v) { assign(1, 0, n - 1, l, r, v); } void add(int x, int lx, int rx, int l, int r, long long v) { if(lx >= l && rx <= r) { t[x] += (rx - lx + 1) * v; lazy[x] += v; return; } if(lx > r || rx < l) { return; } int m = (lx + rx) >> 1; push(x, (m - lx + 1), (rx - m)); add(2 * x, lx, m, l, r, v); add(2 * x + 1, m + 1, rx, l, r, v); t[x] = t[2 * x + 1] + t[2 * x]; } void add(int l, int r, long long v) { add(1, 0, n - 1, l, r, v); } long long query(int x, int lx, int rx, int l, int r) { if(lx >= l && rx <= r) { return t[x]; } if(lx > r || rx < l) { return 0; } int m = (lx + rx) >> 1; push(x, (m - lx + 1), (rx - m)); return query(2 * x, lx, m, l, r) + query(2 * x + 1, m + 1, rx, l, r); } long long query(int l, int r) { return query(1, 0, n - 1, l, r); } }st; signed main() { ios::sync_with_stdio(false); cin.tie(0); int m; cin >> n >> m; while(m--) { int op; cin >> op; if(op == 1) { int l, r; long long v; cin >> l >> r >> v; st.assign(l, r - 1, v); } if(op == 2) { int l, r; long long v; cin >> l >> r >> v; st.add(l, r - 1, v); } if(op == 3) { int l, r; cin >> l >> r; cout << st.query(l, r - 1) << '\n'; } } return 0; }