`{"id":"295928ac-3c68-456e-b5f8-986198faa6a6","name":"Sum Of Interval","description":"You are given an array(of integers) of length n.\r\nYou are required to answer q queries.\r\n\r\nQueries can be of two types\r\n0. 0 pos val : In this you have to update arr[pos] to val.\r\n1. 1 l r: In this query u have to find the sum of all elements in this interval.\r\n\r\nSum of elements in interval [l, r] means sum of all arr[i] for which i is in range [l, r].\r\n\r\nTo do the above task u have to create a datastructure as follows :-\r\n\r\nImplement the SegmentTree class:\r\n1. SegmentTree(int arr[]): Initializes the SegmentTree object with an array,\r\n2. void update(int pos, int val): updates the arr[pos] to val,\r\n3. int query(int l, int r): return sum of all element's in interval [l, r].","inputFormat":"A number n\r\nn1\r\nn2\r\n.. n number of elements\r\nA number q\r\nfollowing q lines contains queries of format either of two \r\n0 pos val,\r\n1 l r","outputFormat":"for each query print a single integer in seperate line","constraints":"1. 1 &lt;= n, q &lt;= 10^5\r\n2. 0 &lt;= l &lt;= r &lt; n\r\n3. 10^4 &lt;= arr[i] &lt;= 10^4.","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class SegmentTree {\r\n\r\n SegmentTree(int arr[]) {\r\n\r\n }\r\n\r\n void update(int pos, int val) {\r\n\r\n }\r\n\r\n int query(int l, int r) {\r\n\r\n }\r\n\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(read.readLine());\r\n int arr[] = new int[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = Integer.parseInt(read.readLine());\r\n }\r\n\r\n SegmentTree obj = new SegmentTree(arr);\r\n\r\n int q = Integer.parseInt(read.readLine());\r\n\r\n StringBuilder out = new StringBuilder();\r\n while (q-- > 0) {\r\n String[]inp = read.readLine().split(\" \");\r\n\r\n int t = Integer.parseInt(inp[0]);\r\n int l = Integer.parseInt(inp[1]);\r\n int r = Integer.parseInt(inp[2]);\r\n\r\n if (t == 0) {\r\n obj.update(l, r);\r\n } else {\r\n int ans = obj.query(l, r);\r\n out.append(ans);\r\n out.append(\"\\n\");\r\n }\r\n }\r\n\r\n System.out.println(out);\r\n }\r\n\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"4\r\n1\r\n2\r\n3\r\n4\r\n9\r\n1 0 3\r\n0 1 3\r\n1 0 3\r\n1 1 2\r\n0 2 5\r\n1 2 3\r\n1 0 1\r\n1 0 2\r\n1 1 3\r\n","sampleOutput":"10\r\n11\r\n6\r\n9\r\n4\r\n9\r\n12\r\n\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"9b3d7d76-b2ca-458f-80a8-4b37d548024a","name":"Segment Tree For Experts","slug":"segment-tree-for-experts-953","type":0},{"id":"1761e131-94fb-4631-8e27-e7bf0e70621a","name":"Sum Of Interval","slug":"sum-of-interval","type":1}],"next":{"id":"5c055372-d5a6-4fce-ba24-91184cb9ac98","name":"Max In A Interval - Range Query Point Update","type":1,"slug":"max-in-a-interval-range-query-point-update"},"prev":{"id":"6a2a82cc-7b41-487b-8d34-a25bc9250b10","name":"Max In An Interval - Range Query","type":1,"slug":"max-in-an-interval-range-query"}}}`

Sum Of Interval

You are given an array(of integers) of length n. You are required to answer q queries. Queries can be of two types 0. 0 pos val : In this you have to update arr[pos] to val. 1. 1 l r: In this query u have to find the sum of all elements in this interval. Sum of elements in interval [l, r] means sum of all arr[i] for which i is in range [l, r]. To do the above task u have to create a datastructure as follows :- Implement the SegmentTree class: 1. SegmentTree(int arr[]): Initializes the SegmentTree object with an array, 2. void update(int pos, int val): updates the arr[pos] to val, 3. int query(int l, int r): return sum of all element's in interval [l, r].

`{"id":"295928ac-3c68-456e-b5f8-986198faa6a6","name":"Sum Of Interval","description":"You are given an array(of integers) of length n.\r\nYou are required to answer q queries.\r\n\r\nQueries can be of two types\r\n0. 0 pos val : In this you have to update arr[pos] to val.\r\n1. 1 l r: In this query u have to find the sum of all elements in this interval.\r\n\r\nSum of elements in interval [l, r] means sum of all arr[i] for which i is in range [l, r].\r\n\r\nTo do the above task u have to create a datastructure as follows :-\r\n\r\nImplement the SegmentTree class:\r\n1. SegmentTree(int arr[]): Initializes the SegmentTree object with an array,\r\n2. void update(int pos, int val): updates the arr[pos] to val,\r\n3. int query(int l, int r): return sum of all element's in interval [l, r].","inputFormat":"A number n\r\nn1\r\nn2\r\n.. n number of elements\r\nA number q\r\nfollowing q lines contains queries of format either of two \r\n0 pos val,\r\n1 l r","outputFormat":"for each query print a single integer in seperate line","constraints":"1. 1 &lt;= n, q &lt;= 10^5\r\n2. 0 &lt;= l &lt;= r &lt; n\r\n3. 10^4 &lt;= arr[i] &lt;= 10^4.","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class SegmentTree {\r\n\r\n SegmentTree(int arr[]) {\r\n\r\n }\r\n\r\n void update(int pos, int val) {\r\n\r\n }\r\n\r\n int query(int l, int r) {\r\n\r\n }\r\n\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(read.readLine());\r\n int arr[] = new int[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = Integer.parseInt(read.readLine());\r\n }\r\n\r\n SegmentTree obj = new SegmentTree(arr);\r\n\r\n int q = Integer.parseInt(read.readLine());\r\n\r\n StringBuilder out = new StringBuilder();\r\n while (q-- > 0) {\r\n String[]inp = read.readLine().split(\" \");\r\n\r\n int t = Integer.parseInt(inp[0]);\r\n int l = Integer.parseInt(inp[1]);\r\n int r = Integer.parseInt(inp[2]);\r\n\r\n if (t == 0) {\r\n obj.update(l, r);\r\n } else {\r\n int ans = obj.query(l, r);\r\n out.append(ans);\r\n out.append(\"\\n\");\r\n }\r\n }\r\n\r\n System.out.println(out);\r\n }\r\n\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"4\r\n1\r\n2\r\n3\r\n4\r\n9\r\n1 0 3\r\n0 1 3\r\n1 0 3\r\n1 1 2\r\n0 2 5\r\n1 2 3\r\n1 0 1\r\n1 0 2\r\n1 1 3\r\n","sampleOutput":"10\r\n11\r\n6\r\n9\r\n4\r\n9\r\n12\r\n\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"9b3d7d76-b2ca-458f-80a8-4b37d548024a","name":"Segment Tree For Experts","slug":"segment-tree-for-experts-953","type":0},{"id":"1761e131-94fb-4631-8e27-e7bf0e70621a","name":"Sum Of Interval","slug":"sum-of-interval","type":1}],"next":{"id":"5c055372-d5a6-4fce-ba24-91184cb9ac98","name":"Max In A Interval - Range Query Point Update","type":1,"slug":"max-in-a-interval-range-query-point-update"},"prev":{"id":"6a2a82cc-7b41-487b-8d34-a25bc9250b10","name":"Max In An Interval - Range Query","type":1,"slug":"max-in-an-interval-range-query"}}}`

Editor

Sum Of Interval

medium

You are given an array(of integers) of length n. You are required to answer q queries. Queries can be of two types 0. 0 pos val : In this you have to update arr[pos] to val. 1. 1 l r: In this query u have to find the sum of all elements in this interval. Sum of elements in interval [l, r] means sum of all arr[i] for which i is in range [l, r]. To do the above task u have to create a datastructure as follows :- Implement the SegmentTree class: 1. SegmentTree(int arr[]): Initializes the SegmentTree object with an array, 2. void update(int pos, int val): updates the arr[pos] to val, 3. int query(int l, int r): return sum of all element's in interval [l, r].

Constraints

1. 1 <= n, q <= 10^5 2. 0 <= l <= r < n 3. 10^4 <= arr[i] <= 10^4.

Format

Input

A number n n1 n2 .. n number of elements A number q following q lines contains queries of format either of two 0 pos val, 1 l r

Output

for each query print a single integer in seperate line

Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}4 1 2 3 4 9 1 0 3 0 1 3 1 0 3 1 1 2 0 2 5 1 2 3 1 0 1 1 0 2 1 1 3 ```

Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}10 11 6 9 4 9 12 ```

Discussions

Show Discussion

Related Resources