To simplify the problem, you are given an array A of length N and a magic number P. Initially, all the elements in this array are 0.
Now, you have to perform a sequence of operation. Each operation is represented as (a, b, c), which means: For each A[i] (a <= i <= b), if A[i] < P, then A[i] will be A[i] + c, else A[i] will be A[i] + c * 2.
Compute all the elements in this array when all the operations finish.
The first line contains three integers n, m, P (1 <= n, m, P <= 200000), denoting the size of the array, the number of operations and the magic number.
Next m lines represent the operations. Each operation consists of three integers a; b and c (1 <= a <= b <= n, 1 <= c <= 20).
3 2 1 1 2 1 2 3 1
1 3 1
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node
{
int left,right;
int _max,_min,lnc;//当前区间的最大值和最小值,当前区间需要加上的数(区间)
};
int n,m,p;
const int maxn=200000;
Node tree[maxn*4];
void buildtree(int id,int l,int r)
{
tree[id].left=l,tree[id].right=r;
tree[id]._max=tree[id]._min=tree[id].lnc=0;
if(l!=r)
{
int mid=(l+r)>>1;
buildtree(id<<1,l,mid);
buildtree(id<<1|1,mid+1,r);
}
}
void update(int id,int l,int r,int d)
{
if(tree[id].left==l&&tree[id].right==r)
{
if(tree[id]._max<p)
{
tree[id].lnc+=d;
tree[id]._max+=d;
tree[id]._min+=d;
return ;
}
if(tree[id]._min>=p)
{
tree[id].lnc+=d<<1;
tree[id]._max+=d<<1;
tree[id]._min+=d<<1;
return ;
}
}
if(tree[id].lnc)
{
tree[id<<1].lnc+=tree[id].lnc;
tree[id<<1]._max+=tree[id].lnc;
tree[id<<1]._min+=tree[id].lnc;
tree[id<<1|1].lnc+=tree[id].lnc;
tree[id<<1|1]._max+=tree[id].lnc;
tree[id<<1|1]._min+=tree[id].lnc;
tree[id].lnc=0;
}
int mid=(tree[id].left+tree[id].right)>>1;
if(r<=mid) update(id<<1,l,r,d);
else if(l>=mid+1) update(id<<1|1,l,r,d);
else
{
update(id<<1,l,mid,d);
update(id<<1|1,mid+1,r,d);
}
tree[id]._max=max(tree[id<<1]._max,tree[id<<1|1]._max);
tree[id]._min=min(tree[id<<1]._min,tree[id<<1|1]._min);
}
int a[maxn];
void query(int id)
{
if(tree[id].left==tree[id].right)
{
a[tree[id].left]=tree[id].lnc;
return ;
}
if(tree[id].lnc)
{
tree[id<<1].lnc+=tree[id].lnc;
tree[id<<1|1].lnc+=tree[id].lnc;
tree[id].lnc=0;
}
query(id<<1);
query(id<<1|1);
}
int main()
{
while(scanf("%d%d%d",&n,&m,&p)==3)
{
buildtree(1,1,n);
while(m--)
{
int s,t,d;scanf("%d%d%d",&s,&t,&d);
update(1,s,t,d);
}
query(1);
printf("%d",a[1]);
for(int i=2;i<=n;i++) printf(" %d",a[i]);printf("\n");
}
return 0;
}