ZOJ 3745 Salary Increasing

时间:2021-06-12 21:57:04

Description

Edward has established a company with n staffs. He is such a kind man that he did Q times salary increasing for his staffs. Each salary increasing was described by three integers (lrc). That means Edward will add c units money for the staff whose salaxy is in range [lr] now. Edward wants to know the amount of total money he should pay to staffs after Q times salary increasing.

Input

The input file contains multiple test cases.

Each case begin with two integers : n -- which indicate the amount of staff; Q -- which indicate Q times salary increasing. The following nintegers each describes the initial salary of a staff(mark as ai). After that, there are Q triples of integers (lirici) (i=1..Q) which describe the salary increasing in chronological.

1 ≤ n ≤ 105 , 1 ≤ Q ≤ 105 , 1 ≤ ai ≤ 105 , 1 ≤ li ≤ ri ≤ 105 , 1 ≤ ci ≤ 105 , ri < li+1

Process to the End Of File.

Output

Output the total salary in a line for each case.

Sample Input

4 1
1 2 3 4
2 3 4

Sample Output

18

Hint

{1, 2, 3, 4} → {1, 4, 6, 7}.

这绝对是个水题,但同时又是个不折不扣的大坑题,一开始没注意ri < li+1 ,差点用线段树做。

 #include <iostream>
#include <cstring>
using namespace std; long long num[],n,m,l,r,add;//坑!开100005不够,因为工资一开始最高为100000,但后面可以涨工资。
int main()
{ while (cin>>n>>m)
{
memset(num,,sizeof(num));
for (int i=;i<=n;i++)
{
cin>>add;
num[add]++;
}
for (int i=;i<=m;i++)
{
cin>>l>>r>>add;
for (int j=r;j>=l;j--)//坑!应该从大到小计算,不然会对后面产生影响。
{
if (num[j])
{
num[j+add]+=num[j];
num[j]=;
}
}
}
add=;
for (int i=;i<;i++)//坑!一开始只从1算到100000,wrong了好几次,后来发现工资可能超过100000.
add=add+num[i]*i;
cout <<add<<endl;
}
return ;
}

#include <iostream>#include <cstring>using namespace std;
long long num[600100],n,m,l,r,add;//坑!开100005不够,因为工资一开始最高为100000,但后面可以涨工资。 int main(){        while (cin>>n>>m)    {          memset(num,0,sizeof(num));          for (int i=1;i<=n;i++)          {              cin>>add;              num[add]++;          }          for (int i=1;i<=m;i++)          {              cin>>l>>r>>add;              for (int j=r;j>=l;j--)//坑!应该从大到小计算,不然会对后面产生影响。               {                  if (num[j])                  {                             num[j+add]+=num[j];                             num[j]=0;                  }              }          }          add=0;          for (int i=1;i<600100;i++)//坑!一开始只从1算到100000,wrong了好几次,后来发现工资可能超过100000.               add=add+num[i]*i;          cout <<add<<endl;    }    return 0;}