Description
Small hi and small ho are all fans, "Analog City" is a game they like very much, in this game they can incarnate God mode, buy and sell property.
In this game, will continue to happen as follows two kinds of events: one is the house spontaneous price increase or price reduction, and the other is the government departments for housing prices of hard regulation. Changes in house prices naturally affect the decisions of small hi and small ho, so they want to know the total price of all the houses in a street at any given moment-but unfortunately the game itself does not provide such calculations. But this is hard to do small hi and small ho, they have this problem abstracted, became the question:
Little Hi and Little Ho are concerned about the length of the street is N m, starting from one end every 1 meters there is a house, sequentially numbered 0..N, at the beginning of the game, each house has an initial price, where the number of the first house price for P_i, followed by the total occurrence of M events, All events occur for a number of consecutive houses, where the first event is a house spontaneous price increase or price reduction, it is described as ternary group (l_i, R_i, d_i), indicating that the number of houses in [L_i, R_i] The price of the increment (that is, positive numbers for price increases, negative numbers for the price) for the D_i , if the Government's relevant departments for the rigid regulation of housing prices, it is described as ternary group (l_i, R_i, v_i), said the number in the [L_i, R_i] The range of the price of the house into V_i. What little Hi and Little ho would like to know is the sum of the house prices of all the houses in the street after each event.
input
Each test point (input file) has and has only one set of test data.
The 1th behavior of each set of test data is two integers n, M, representing the length of the street and the total number of events that occurred.
The 2nd behavior of each set of test data n+1 an integer, with the first integer digit p_i, representing the initial price of the house numbered I.
The first 3-m+2 line of each set of test data, in chronological order, each line describes an event, if the event described by the row is "spontaneous price increase or price reduction", the behavior is 4 integers 0, l_i, R_i, d_i, meaning as described above; if the event described by the row is " Government departments for the rigid regulation of housing prices ", then the behavior of 4 integers 1, l_i, R_i, v_i, meaning as described above.
For 100% of the data, meet N<=10^5,1<=p_i, | d_i|, V_i<=10^4,0<=l_i<r_i<=n. <>
For 100% of the data, meet at any time, the price of any house is in [1, 10^4].
Output
For each set of test data, the output m row, which acts an integer ans_i, represents the total house price of all houses in the street after the first event.
Sample Input
6
3195 2202 4613 3744 2892 4858 619 5079 9478 7366 8942
0 1 6 886 1 0 2
9710
1 0 7980 0 4
9-759 4
0 2 8 1581
0 4 4-1010
Sample Output
58304
75652
87780
42216
53283
52273
Ideas for solving problems:
First, the value of the given values is used to construct a tree with two forks, each node in the tree representing the points of the intervals it maintains.
At this point, the root node of the tree is the sum of the house prices of all the houses in the street.
If it is the house spontaneous price increase or price reduction (add), then from the root node down, find just can maintain the interval of one or several nodes, marking its maintenance interval [(ADD+X) (sum+= (r-l+1) *x)]
If the house is a hard price adjustment (set), likewise, Mark [Set (x) (sum= (r-l+1) *x) in this one or several root nodes.
It is important to note that if the node that currently requires the set has an add, then the add is invalidated, and the node that currently needs add will not affect the result if there is a set.
In addition, if you look down the maintenance interval from the root node, you encounter a [set| | Add], it can be thought that we make changes to its descendants node, the current node will also change, so the simple way is to put the current node [add| | Set property to its child nodes, and then recalculate the values maintained by the nodes.
AC Code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> using namespace
Std
#define MAXX 110000 typedef long LL;
struct PO {ll add,set,sum;
void init (int sum=0,int add=0,int set=-1)//modify property, reset set to-1 {this->add=add; When add is 0 o'clock
this->sum=sum;
if (!add) this->set=set;
} void Addmax (int sum=0,int add=0)//property Additive {this->add+=add;
this->sum+=sum;
}} data[maxx<<2]; int lr=1; Width of two fork tree-1 void init (int n) {while (lr<n) lr<<=1;
Calculates the full two fork tree width based on n (int i=0; i<=2*lr; i++)//Initialize Data[i].init ();
lr--; } void Inse (int o,int k)//updates the lowest level o node of the two fork tree and updates the value of its parent node from the bottom {o+=lr;
LR is full two fork tree width-1, is also the bottom of the first element of the subscript data[o].sum=k;
while (o>1) {o/=2;
Data[o].sum=data[o*2].sum+data[o*2+1].sum; } void Update (int isset,int s,int l,int r,int ls,int lr,int o)//current interval [l,r], target interval [LS,LR], current sectionPoint o,s for [add| | The value of set] {if (l>lr| | r<ls| | R<L) return; If the two interval intersection is an empty if (R<=LR&&L>=LS)//If the target interval contains the current interval, execute [set| |
Add] {if (isset) Data[o].init (s* (r-l+1), 0,s); else {Data[o].addmax (s* (r-l+1), s);//Data[o].init (data[o].sum+s* (r-l+1), data[o].add+s);
Can't write this, there is a small very small bug (when data[o].add+s==0 will reset set)} return;
int m= (l+r)/2,lc=o*2,rc=lc+1; if (l!=r&& (data[o].set!=-1| | data[o].add!=0))///If not on leaf node and current node with [set| | Add] Property {if (data[o].set!=-1)///with [set] property {Data[lc].init (data[o].set* (m-l+1), 0,data[o] . set);
Transfer set to its two sub nodes Data[rc].init (data[o].set* (r-m), 0,data[o].set); Data[o].set=-1; Resets the current node set} if (data[o].add!=0)//with [Add] property {Data[lc].addmax (m-l+1) *data[o].ad D,data[o].add);
Transfer add to its two sub nodes Data[rc].addmax ((r-m) *data[o].add,data[o].add); DAta[o].add=0; Resets the current node add} update (ISSET,S,L,M,LS,LR,LC);
Recursive child node update (ISSET,S,M+1,R,LS,LR,RC); Data[o].sum=data[lc].sum+data[rc].sum;
The value of the parent node is evaluated at the end of recursion () int main () {int k,t,n;
scanf ("%d%d", &n,&t);
++n;
Init (n);
for (int i=1; i<=n; i++) {scanf ("%d", &k);
Inse (I,K);
while (t--) {int a,b,c;
scanf ("%d%d%d%d", &k,&a,&b,&c);
Update (k,c,0,lr,a,b,1);
printf ("%ld\n", data[1].sum);
return 0;
}