import sys
import threading
def main():
import sys
data = sys.stdin
line = data.readline().split()
n, q = int(line[0]), int(line[1])
a = list(map(int, data.readline().split()))
# If there's only one checkpoint, scenic score is always 0
if n == 1:
for _ in range(q):
data.readline()
print(0)
return
# Build difference array d[i] = a[i+1] - a[i]
d = [a[i+1] - a[i] for i in range(n-1)]
# Initial total scenic score = sum of absolute differences
total = sum(abs(v) for v in d)
out = []
for _ in range(q):
l, r, x = map(int, data.readline().split())
# Convert to 0-based
l -= 1
r -= 1
# Update the difference at l-1 if l > 0
if l > 0:
idx = l - 1
old = d[idx]
new = old + x
total += abs(new) - abs(old)
d[idx] = new
# Update the difference at r if r < n-1
if r < n - 1:
idx = r
old = d[idx]
new = old - x
total += abs(new) - abs(old)
d[idx] = new
out.append(str(total))
sys.stdout.write("\n".join(out))
if __name__ == "__main__":
main()
Compilation message (stdout)
Compiling 'Main.py'...
=======
adding: __main__.pyc (deflated 39%)
=======
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |