fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX 100001
  5.  
  6. int main() {
  7. int N, X, Y, L, R;
  8. int diff[MAX] = {0}; // This will be used for the difference array approach
  9.  
  10. // Reading input
  11. scanf("%d", &N);
  12. scanf("%d %d", &X, &Y);
  13.  
  14. // Reading each segment and updating the difference array
  15. for (int i = 0; i < N; i++) {
  16. scanf("%d %d", &L, &R);
  17.  
  18. if (L < MAX) {
  19. diff[L]++;
  20. }
  21. if (R + 1 < MAX) {
  22. diff[R + 1]--;
  23. }
  24. }
  25.  
  26. // Calculating the prefix sum array to find the coverage of each point
  27. int score = 0;
  28. int current_coverage = 0;
  29.  
  30. for (int i = X; i <= Y; i++) {
  31. current_coverage += diff[i]; // The coverage of point i
  32. score += current_coverage; // Add the score of point i to the total score
  33. }
  34.  
  35. // Printing the result
  36. printf("%d\n", score);
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
0