We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Array Manipulation
Array Manipulation
Sort by
recency
|
2491 Discussions
|
Please Login in order to post a comment
I have used in Prefix method
long [] arr= new long[n+2];
}
import sys queries=sys.stdin.read().strip().split() it=iter(queries) n=int(next(it)) q=int(next(it)) arr = [0] * (n+2)
for i in range(q): a = int(next(it)) b = int(next(it)) k = int(next(it)) arr[ a ] += k arr[ b +1] -= k
max_value = 0 current = 0 for val in arr: current += val max_value = max(max_value, current)
print(max_value)
import java.io.; import java.util.;
class Result {
}
public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
}
You cannot directly sum each item in range because you will get a Time Exceeded Exception.
Instead, use prefix sum technique:
tricky. Not explained very well.. KEY : prefix sum algorthim