Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- const int maxn = 1e5 + 10;
- int a[maxn];
- int segment_tree[3 * maxn];
- int lazy_propagation[3 * maxn];
- void build_tree(int L, int R, int position) {
- if(L == R) {
- segment_tree[position] = a[L];
- }
- else {
- int middle = (L + R) / 2;
- build_tree(L, middle, 2 * position);
- build_tree(middle + 1, R, 2 * position + 1);
- segment_tree[position] = segment_tree[2 * position] + segment_tree[2 * position + 1];
- }
- }
- void update_range(int L, int R, int position, int i, int j, int new_value) {
- if(lazy_propagation[position] > 0) {
- segment_tree[position] += lazy_propagation[position] * (R - L + 1);
- if(L != R) {
- lazy_propagation[2 * position] += lazy_propagation[position];
- lazy_propagation[2 * position + 1] += lazy_propagation[position];
- }
- lazy_propagation[position] = 0;
- }
- // L R i L R j L R
- if(R < i or j < L) {
- return;
- }
- if(i <= L and R <= j) {
- segment_tree[position] += new_value * (R - L + 1);
- if(L != R) {
- lazy_propagation[2 * position] += new_value;
- lazy_propagation[2 * position + 1] += new_value;
- }
- return;
- }
- int middle = (L + R) / 2;
- update_range(L, middle, 2 * position, i, j, new_value);
- update_range(middle + 1, R, 2 * position + 1, i, j, new_value);
- segment_tree[position] = segment_tree[2 * position] + segment_tree[2 * position + 1];
- }
- int query(int L, int R, int position, int i, int j) {
- if(lazy_propagation[position] > 0) {
- segment_tree[position] += lazy_propagation[position] * (R - L + 1);
- if(L != R) {
- lazy_propagation[2 * position] += lazy_propagation[position];
- lazy_propagation[2 * position + 1] += lazy_propagation[position];
- }
- lazy_propagation[position] = 0;
- }
- // L R i L R j L R
- if(i <= L and R <= j) {
- return segment_tree[position];
- }
- if(R < i or j < L) {
- return 0;
- }
- int middle = (L + R) / 2;
- return query(L, middle, 2 * position, i, j) + query(middle + 1, R, 2 * position + 1, i, j);
- }
- int main() {
- ios_base::sync_with_stdio(false);
- int n;
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> a[i];
- }
- build_tree(0, n - 1, 1);
- memset(lazy_propagation, 0, sizeof lazy_propagation);
- int q;
- cin >> q;
- for(int i = 0; i < q; i++) {
- char c;
- cin >> c;
- if(c == 'U') {
- int a, b, x;
- cin >> a >> b >> x;
- update_range(0, n - 1, 1, a, b, x);
- for(int j = 0; j < n; j++) {
- cout << query(0, n - 1, 1, j, j) << " ";
- }
- cout << endl;
- }
- else {
- int a, b;
- cin >> a >> b;
- cout << query(0, n - 1, 1, a, b) << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement