Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.regex.*;
  8.  
  9. public class Solution {
  10.  
  11. // Complete the arrayManipulation function below.
  12. static long arrayManipulation(int n, int[][] queries) {
  13. long a[] = new long[n + 1];
  14. for (int i = 0; i < queries.length; i++) {
  15. int start = queries[i][0];
  16. int end = queries[i][1];
  17. int value = queries[i][2];
  18. a[start] += value;
  19. if (end + 1 <= n)
  20. a[end + 1] -= value;
  21. }
  22. long result = 0;
  23. long max = -1;
  24. for (int i = 1; i <= n; i++) {
  25. result += a[i];
  26. if (result > max)
  27. max = result;
  28. }
  29. return max;
  30.  
  31. }
  32.  
  33. private static final Scanner scanner = new Scanner(System.in);
  34.  
  35. public static void main(String[] args) throws IOException {
  36. BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
  37.  
  38. String[] nm = scanner.nextLine().split(" ");
  39.  
  40. int n = Integer.parseInt(nm[0]);
  41.  
  42. int m = Integer.parseInt(nm[1]);
  43.  
  44. int[][] queries = new int[m][3];
  45.  
  46. for (int i = 0; i < m; i++) {
  47. String[] queriesRowItems = scanner.nextLine().split(" ");
  48. scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  49.  
  50. for (int j = 0; j < 3; j++) {
  51. int queriesItem = Integer.parseInt(queriesRowItems[j]);
  52. queries[i][j] = queriesItem;
  53. }
  54. }
  55.  
  56. long result = arrayManipulation(n, queries);
  57.  
  58. bufferedWriter.write(String.valueOf(result));
  59. bufferedWriter.newLine();
  60.  
  61. bufferedWriter.close();
  62.  
  63. scanner.close();
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement