fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. int main(){
  6. ios::sync_with_stdio(false);
  7. cin.tie(nullptr);
  8.  
  9. int n;
  10. cin >> n;
  11.  
  12. // Tìm biên trên của ngày cần xét
  13. const int MAXD = 100000 + 5;
  14. static long long diff[MAXD+2] = {0};
  15. int minDay = MAXD, maxDay = 0;
  16.  
  17. for(int i = 0; i < n; ++i){
  18. long long x, d, k;
  19. cin >> x >> d >> k;
  20. // Bắt đầu tăng k tại x, giảm k tại x+d
  21. diff[x] += k;
  22. diff[x + d] -= k;
  23. if (x < minDay) minDay = x;
  24. if (x + d - 1 > maxDay) maxDay = x + d - 1;
  25. }
  26.  
  27. long long prev = 0, curr = 0;
  28. bool firstOutput = true;
  29.  
  30. // Duyệt từ minDay đến maxDay
  31. for(int day = minDay; day <= maxDay; ++day){
  32. curr += diff[day];
  33. if (curr != prev) {
  34. if (!firstOutput) cout << ' ';
  35. cout << curr;
  36. firstOutput = false;
  37. prev = curr;
  38. }
  39. }
  40. cout << '\n';
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0.01s 5292KB
stdin
3
6 2 15
3 6 9
4 5 7
stdout
9 16 31 16