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 (l, r, c). That means Edward will add c units money for the staff whose salaxy is in range [l, r] 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 n integers each describes the initial salary of a staff(mark as ai). After that, there are Q triples of integers (li, ri, ci) (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}.
题目的大意是说给出N个员工的初始工资,并给出Q次加薪机会,然后满足在L R 这个闭区间内的工资的员工可以得到C的工资加成,问最后N个人的总工资为多少钱。
额这道题一开始我跟鑫神看理解成了线段树的区间更新然后求输出最顶端的数,然后TLE了- - 大湿说直接暴力可以过,结果我就傻傻的去不做任何处理的去写了个暴力结果又T了,后来在大湿的指点下发现这道题类似于暑假老学长跟我说过的一个筛法的问题,这里是把人的工资用一个初始全为0的数组进行计数,然后碰到了第K个人的信息则A[K]++进行计数,然后这样一来就可以求当前的工资总和sum+=a[k]*c,然后注意每次从L到R注意每次要把后面的a[i+c]更新为a[i],而a[i]本身注意清零,然后用于后面的统计,然后就这样,暴力的过了。。。好吧复习期末的过程中没做题。。手又生了,,考完了寒假要努力刷点题了- -!
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a[100005];
int n,q;
long long m;
while(cin>>n>>q)
{ m=0;
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
{ int k;
cin>>k;
a[k]++;
m+=k;
}
int l,r,c;
while(q--)
{
cin>>l>>r>>c;
for(int i=l;i<=r;i++ )
{
if(a[i]!=0)
{
m+=c*a[i];
}
}
for(int i=r;i>=l;i--)
{
a[i+c]+=a[i];
a[i]=0;
}
}
cout<<m<<endl;
}
}