标签:
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 (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 nintegers 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
{1, 2, 3, 4} → {1, 4, 6, 7}.
这绝对是个水题,但同时又是个不折不扣的大坑题,一开始没注意ri < li+1 ,差点用线段树做。
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 long long num[600100],n,m,l,r,add;//坑!开100005不够,因为工资一开始最高为100000,但后面可以涨工资。 6 int main() 7 { 8 9 while (cin>>n>>m) 10 { 11 memset(num,0,sizeof(num)); 12 for (int i=1;i<=n;i++) 13 { 14 cin>>add; 15 num[add]++; 16 } 17 for (int i=1;i<=m;i++) 18 { 19 cin>>l>>r>>add; 20 for (int j=r;j>=l;j--)//坑!应该从大到小计算,不然会对后面产生影响。 21 { 22 if (num[j]) 23 { 24 num[j+add]+=num[j]; 25 num[j]=0; 26 } 27 } 28 } 29 add=0; 30 for (int i=1;i<600100;i++)//坑!一开始只从1算到100000,wrong了好几次,后来发现工资可能超过100000. 31 add=add+num[i]*i; 32 cout <<add<<endl; 33 } 34 return 0; 35 }
#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;}
标签:
原文地址:http://www.cnblogs.com/arno-my-boke/p/4747683.html