fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int N;
  6. cin >> N;
  7.  
  8. vector<int> A(N + 2, 0); // N+2 so that A[r+1] is always valid
  9.  
  10. int Q;
  11. cin >> Q;
  12.  
  13. while (Q--) {
  14. int l, r;
  15. cin >> l >> r;
  16.  
  17. A[l] += 1; // Start incrementing from l
  18. A[r + 1] -= 1; // Stop incrementing after r
  19. }
  20.  
  21. // Convert difference array into actual array
  22. for (int i = 1; i <= N; i++) {
  23. A[i] += A[i - 1];
  24. }
  25.  
  26. // Print final array
  27. for (int i = 1; i <= N; i++) {
  28. cout << A[i] << " ";
  29. }
  30. cout << "\n";
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0s 5316KB
stdin
6
4
1 2
1 4
2 4
1 6
stdout
3 4 3 3 1 1