区间修改(懒标记),区间查询
struct data {int tad;int sum;
} t[N<<2];void push_up(int u){t[u].sum=t[u << 1].sum+t[u << 1|1].sum;
}void push_add(int u, int l, int r, int v) {t[u].sum += (r - l + 1ll) * v;t[u].tad += v;
}void pushdown(int u, int l, int r) {const int lu = u << 1, ru = u << 1 | 1, mid = (l + r) >> 1;if (t[u].tad)push_add(lu, l, mid, t[u].tad), push_add(ru, mid + 1, r, t[u].tad);t[u].tad = 0;
}void build(int u = 1, int l = 1, int r = n) {t[u].tad=0;if (l == r) {t[u].sum = a[l];return;}int mid = (l + r) >> 1;build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);push_up(u);
}void add(int L, int R, int v, int u = 1, int l = 1, int r = n) {if (R < l || r < L) return;if (L <= l && r <= R) return push_add(u, l, r, v);int mid = (l + r) >> 1;pushdown(u, l, r);add(L, R, v, u << 1, l, mid), add(L, R, v, u << 1 | 1, mid + 1, r);push_up(u);
}int qsum(int L, int R, int u = 1, int l = 1, int r = n) {if (R < l || r < L) return 0;if (L <= l && r <= R) return t[u].sum;int mid = (l + r) >> 1;pushdown(u, l, r);return qsum(L, R, u << 1, l, mid) + qsum(L, R, u << 1 | 1, mid + 1, r);
}
单点修改,区间查询
int pos[N];
struct data {int tad;int sum;
} t[N<<2];void push_up(int u){t[u].sum=t[u << 1].sum+t[u << 1|1].sum;
}void build(int u = 1, int l = 1, int r = n) {t[u].tad=0;if (l == r) {pos[l]=u;t[u].sum = a[l];return;}int mid = (l + r) >> 1;build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);push_up(u);
}void add(int p, int v) {int u=pos[p];t[u].sum+=v;while(u>>=1)push_up(u);
}int qsum(int L, int R, int u = 1, int l = 1, int r = n) {if (R < l || r < L) return 0;if (L <= l && r <= R) return t[u].sum;int mid = (l + r) >> 1;return qsum(L, R, u << 1, l, mid) + qsum(L, R, u << 1 | 1, mid + 1, r);
}