Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int maxN = 2e5;
- int n;
- struct seg {
- vector<long long> t = vector<long long> (maxN * 4);
- vector<long long> lazy = vector<long long> (maxN * 4);
- vector<long long> cur = vector<long long> (maxN * 4);
- vector<bool> mark = vector<bool> (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);
- }
- };
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- int m;
- cin >> n >> m;
- seg st;
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement