#include <iostream>
#include <vector>
using namespace std;
class SegmentTree {
public :
vector< int > tree;
int n;
SegmentTree( int size) {
n = size;
tree.resize ( 4 * n, 0 ) ; // Initialize the segment tree with 0
}
// Build the segment tree from the array
void build( const vector< int > & arr, int node, int start, int end) {
if ( start == end) {
tree[ node] = arr[ start] ; // Leaf node stores array value
} else {
int mid = ( start + end) / 2 ;
build( arr, 2 * node + 1 , start, mid) ; // Build left subtree
build( arr, 2 * node + 2 , mid + 1 , end) ; // Build right subtree
tree[ node] = tree[ 2 * node + 1 ] + tree[ 2 * node + 2 ] ; // Internal node
}
}
// Query the sum of elements in range [L, R]
int query( int node, int start, int end, int L, int R) {
if ( R < start || end < L) {
return 0 ; // No overlap
}
if ( L <= start && end <= R) {
return tree[ node] ; // Total overlap
}
int mid = ( start + end) / 2 ;
int left_query = query( 2 * node + 1 , start, mid, L, R) ; // Query left subtree
int right_query = query( 2 * node + 2 , mid + 1 , end, L, R) ; // Query right subtree
return left_query + right_query; // Combine the results
}
// Update the value at a specific index
void update( int node, int start, int end, int idx, int value) {
if ( start == end) {
tree[ node] = value; // Update leaf node
} else {
int mid = ( start + end) / 2 ;
if ( start <= idx && idx <= mid) {
update( 2 * node + 1 , start, mid, idx, value) ; // Update left subtree
} else {
update( 2 * node + 2 , mid + 1 , end, idx, value) ; // Update right subtree
}
tree[ node] = tree[ 2 * node + 1 ] + tree[ 2 * node + 2 ] ; // Update internal node
}
}
} ;
int main( ) {
int n;
cin >> n; // Input the size of the array
vector< int > arr( n) ;
// Input the array elements
for ( int i = 0 ; i < n; i++ ) {
cin >> arr[ i] ;
}
SegmentTree segment_tree( n) ;
segment_tree.build ( arr, 0 , 0 , n - 1 ) ; // Build the segment tree
int L, R;
cin >> L >> R; // Input the range for the query
cout << segment_tree.query ( 0 , 0 , n - 1 , L, R) << endl; // Output the sum in the range
int idx, value;
cin >> idx >> value; // Input the index and new value for update
segment_tree.update ( 0 , 0 , n - 1 , idx, value) ; // Update the segment tree
cin >> L >> R; // Input the new range for the query
cout << segment_tree.query ( 0 , 0 , n - 1 , L, R) << endl; // Output the updated sum in the range
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2xhc3MgU2VnbWVudFRyZWUgewpwdWJsaWM6CiAgICB2ZWN0b3I8aW50PiB0cmVlOwogICAgaW50IG47CgogICAgU2VnbWVudFRyZWUoaW50IHNpemUpIHsKICAgICAgICBuID0gc2l6ZTsKICAgICAgICB0cmVlLnJlc2l6ZSg0ICogbiwgMCk7ICAvLyBJbml0aWFsaXplIHRoZSBzZWdtZW50IHRyZWUgd2l0aCAwCiAgICB9CgogICAgLy8gQnVpbGQgdGhlIHNlZ21lbnQgdHJlZSBmcm9tIHRoZSBhcnJheQogICAgdm9pZCBidWlsZChjb25zdCB2ZWN0b3I8aW50PiYgYXJyLCBpbnQgbm9kZSwgaW50IHN0YXJ0LCBpbnQgZW5kKSB7CiAgICAgICAgaWYgKHN0YXJ0ID09IGVuZCkgewogICAgICAgICAgICB0cmVlW25vZGVdID0gYXJyW3N0YXJ0XTsgIC8vIExlYWYgbm9kZSBzdG9yZXMgYXJyYXkgdmFsdWUKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBpbnQgbWlkID0gKHN0YXJ0ICsgZW5kKSAvIDI7CiAgICAgICAgICAgIGJ1aWxkKGFyciwgMiAqIG5vZGUgKyAxLCBzdGFydCwgbWlkKTsgIC8vIEJ1aWxkIGxlZnQgc3VidHJlZQogICAgICAgICAgICBidWlsZChhcnIsIDIgKiBub2RlICsgMiwgbWlkICsgMSwgZW5kKTsgIC8vIEJ1aWxkIHJpZ2h0IHN1YnRyZWUKICAgICAgICAgICAgdHJlZVtub2RlXSA9IHRyZWVbMiAqIG5vZGUgKyAxXSArIHRyZWVbMiAqIG5vZGUgKyAyXTsgIC8vIEludGVybmFsIG5vZGUKICAgICAgICB9CiAgICB9CgogICAgLy8gUXVlc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