Construct a segment tree over the range and initially set all values to zero. Suppose a trader sold hats in villages [l_i; r_i], starting with a price of p_i. Divide the segment [l_i; r_i] into fundamental segments [lx_1; ry_1], [lx_2; ry_2], ..., [lx_k; ry_k]. Then, for the node corresponding to the segment [lx_q; ry_q]~(1 \le q \le k), store the value
For example, let n = 5, and the first trade covers the cities numbered from to , starting with a price of . Then, the segment will be split into fundamental segments: [2; 2], [3; 3], [4; 5]. After processing the first operation, the segment tree will look as follows:
If the fundamental segment contains the value , it means that:
The trader sold hats in all cities from to .
In city , the hat was sold for the price , in city a + 1 for the price p + 1, and so on, up to city , where the price was p + b - a.
Consider the procedure for processing the query [left; right] (the segment where the trader is trading) over the range [lpos; rpos], if the initial price of the hat is .
In the left subsegment, the merchant will sell hats in len = min(mid, right) - left + 1 cities, starting at the price . In the right subsegment, the first price of the hat in the city where the merchant arrives will be x + len. Thus, in the segment with indices [max(left, mid + 1), right], the trade will start at the price x + len. However, if len < 0 (which is possible if the entire query segment lies within the right subsegment), set len = 0.
Consider the segment , which has two subsegments: and [m + 1; b]. If the trader sold a hat for price in city , then in city m + 1, the price will be p + m + 1 - a.
During the query processing, implement value propagation that maintains the maximum.
To print the answer, traverse the tree, propagate all values to the leaves, and then print them.
Example
Consider the execution of the two queries given in the example. Split the query segments into fundamental segments:
[1; 3] = [1; 3];
[2; 4] = [2; 2] + [3; 3] + [4; 4].
Algorithm implementation
Declare an array SegTree to store the segment tree.
#define MAX 300010
long long SegTree[4*MAX];The Push function propagates the value from node to its children.
void Push(int v, int lpos, int mid, int rpos)
{
if (SegTree[v])
{
SegTree[2 * v] = max(SegTree[2 * v], SegTree[v]);
SegTree[2 * v + 1] = max(SegTree[2 * v + 1],
SegTree[v] + mid - lpos + 1);
SegTree[v] = 0;
}
}The Update function performs the sale of hats over the segment [left; right], starting with the price .
void Update(int v, int lpos, int rpos, int left, int right,
long long x)
{
if (left > right) return;
if ((lpos == left) && (rpos == right))
SegTree[v] = max(SegTree[v], x);
else
{
int mid = (lpos + rpos) / 2;
Push(v, lpos, mid, rpos);
int len = min(mid, right) - left + 1;
if (len < 0) len = 0;
Update(2 * v, lpos, mid, left, min(mid, right), x);
Update(2 * v + 1, mid + 1, rpos, max(left, mid + 1),
right, x + len);
}
}The PrintResult function propagates the values from internal nodes to the leaves and prints the final state of the array.
void PrintResult(int v, int lpos, int rpos)
{
if (lpos == rpos)
printf("%lld ", SegTree[v]);
else
{
int mid = (lpos + rpos) / 2;
Push(v, lpos, mid, rpos);
PrintResult(2 * v, lpos, mid);
PrintResult(2 * v + 1, mid + 1, rpos);
}
}The main part of the program. Read the input data.
scanf("%d %d", &n, &m);
memset(SegTree,0,sizeof(SegTree));Process queries.
for(i = 0; i < m; i++)
{
scanf("%d %d %lld", &l, &r, &x);
Update(1,1,n,l,r,x);
}Print the result.
PrintResult(1,1,n);
printf("\n");