Under construction

Still being built. Things may change

Coffee Shop Game 2

Interview

Time limit2sMemory limit256 MB

Summary
Given an array, answer Q online queries each asking a range sum (with swapped bounds allowed) followed by a point update.
Level

Medium4 of 10

Topics
Segment tree, Prefix sum, Array
Solved
No attempts yet

Problem

A game is played in a coffee shop.

Initially, there are NN integers arranged in a row. Each turn has two steps.

  1. Given two positions xx and yy, find the sum of the current array from the xx-th number through the yy-th number.
  2. Given a position aa and a value bb, replace the aa-th number with bb.

Write a program that prints the required range sum for every turn.

Input

The first line contains the number of integers NN and the number of turns QQ. (1≤N,Q≤100,000)(1 \le N, Q \le 100,000)

The second line contains the initial NN integers in the array.

Each of the next QQ lines contains four integers xx, yy, aa, and bb, describing one turn. First find the sum from the xx-th number through the yy-th number in the current array, then replace the aa-th number with bb.

Every integer in the input is between −231-2^{31} and 231−12^{31}-1, inclusive.

Output

For each turn, output the computed range sum on its own line.

Hint

Normally, x∼yx \sim y means from the xx-th number through the yy-th number. In this problem, if x>yx > y, compute the sum from the yy-th number through the xx-th number instead.

Examples1

  1. Example 1

    Input
    5 2
    1 2 3 4 5
    2 3 3 1
    3 5 4 1
    
    Expected output
    5
    10