### #1080 : 更为复杂的买卖房屋姿势

#### 输出

• 样例输入

• ```10 6
3195 2202 4613 3744 2892 4858 619 5079 9478 7366 8942
0 1 6 886
1 0 2 9710
1 0 10 7980
0 4 9 -7594
0 2 8 1581
0 4 4 -1010```
• 样例输出

• ```58304
75652
87780
42216
53283
52273```

#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<algorithm>

#include<vector>

#include<list>

#include<iterator>

#include<string>

#include<stack>

using namespace std;

const int MAX = 1000001;

struct NODE {

int value, left, right, sum, add;

}node[MAX];

void BuildTree(int n, int left, int right) {

node[n].left = left;

node[n].right = right;

node[n].value = 0;

if (left == right)

{

scanf_s("%d", &node[n].sum);

return;

}

int mid = (left + right) >> 1;

BuildTree(n << 1, left, mid);

BuildTree((n << 1) | 1, mid + 1, right);

node[n].sum = node[n << 1].sum + node[(n << 1) | 1].sum;

}

void PushDown(int n) {

if (node[n].value!=0){

node[n << 1].value = node[n << 1 | 1].value = node[n].value;

node[n << 1].sum = (node[n << 1].right - node[n << 1].left + 1)*node[n].value;

node[n << 1 | 1].sum = (node[n << 1 | 1].right - node[n << 1 | 1].left + 1)*node[n].value;

node[n].value = 0;

}

{

node[n << 1].sum += (node[n << 1].right - node[n << 1].left + 1)*node[n].add;

node[n << 1 | 1].sum += (node[n << 1 | 1].right - node[n << 1 | 1].left + 1)*node[n].add;

}

}

int FindTree(int n, int begin, int end) {

int p1 = 0, p2 = 0;

if (node[n].left >= begin&&node[n].right <= end)

return node[n].sum;

PushDown(n);

if (begin <= node[n << 1].right)

p1 = FindTree(n << 1, begin, end);

if (end >= node[(n << 1) | 1].left)

p2 = FindTree((n << 1) | 1, begin, end);

node[n].sum = node[n << 1].sum + node[n << 1 | 1].sum;

return p1 + p2;

}

void UpdateTree(int n, int left, int right, int val,int flag) {

if (node[n].left >= left && node[n].right <= right)

{

if (flag == 1)

{

node[n].sum = (node[n].right - node[n].left + 1)*val;

node[n].value = val;

}

if (flag == 0)

{

node[n].sum += (node[n].right - node[n].left + 1)*val;

}

return;

}

PushDown(n);

if (left <= node[n << 1].right)

UpdateTree(n << 1, left, right, val,flag);

if (right >= node[(n << 1) | 1].left)

UpdateTree((n << 1) | 1, left, right, val,flag);

node[n].sum = node[n << 1].sum + node[(n << 1) | 1].sum;

}

int main()

{

int N;

int m;

int s, l, r, v;

while (scanf_s("%d %d", &N, &m) != EOF){

BuildTree(1, 1, N+1);

for (int i = 0; i < m; i++)

{

scanf_s("%d %d %d %d", &s, &l, &r, &v);

UpdateTree(1, l+1, r+1, v, s);

printf("%d\n", FindTree(1, 1, N+1));

}

}

return 0;

}