`{"id":"73646009-d6ed-4cf7-b3a7-bb1a5ca87df0","name":"What's At Idx - Point Query Range Update","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 ind : In this you have to tell the value of arr[ind].\r\n1. 1 l r val: In this query u have to increase all elements in this interval [l, r] by val.\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 l, int r, int val): increase all elements in this interval [l, r] by val,\r\n3. int query(int ind): return arr[ind].","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 ind,\r\n1 l r val","outputFormat":"for each query of type 0 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], val &lt;= 10^4.\r\n4. 0 &lt;= ind &lt; n","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 l, int r, int val) {\r\n\r\n }\r\n\r\n int query(int ind) {\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\r\n if (t == 0) {\r\n int ind = Integer.parseInt(inp[1]);\r\n long ans = obj.query(ind);\r\n out.append(ans + \"\\n\");\r\n } else {\r\n int l = Integer.parseInt(inp[1]);\r\n int r = Integer.parseInt(inp[2]);\r\n int val = Integer.parseInt(inp[3]);\r\n obj.update(l, r, val);\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":"8\r\n4\r\n3\r\n9\r\n3\r\n9\r\n8\r\n7\r\n2\r\n5\r\n1 4 6 0\r\n0 7\r\n1 0 3 4\r\n0 4\r\n0 5\r\n","sampleOutput":"2\r\n9\r\n8\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":"059f9009-7e44-4ad2-9a06-fd536d21bea1","name":"What's At Idx - Point Query Range Update","slug":"what-s-at-idx-point-query-range-update","type":1}],"next":{"id":"049f89aa-c540-40f2-b90a-57097e32045c","name":"Sum Of Range - Range Query Range Update","type":1,"slug":"sum-of-range-range-query-range-update"},"prev":{"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"}}}`

# What's At Idx - Point Query Range Update

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 ind : In this you have to tell the value of arr[ind]. 1. 1 l r val: In this query u have to increase all elements in this interval [l, r] by val. 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 l, int r, int val): increase all elements in this interval [l, r] by val, 3. int query(int ind): return arr[ind].

`{"id":"73646009-d6ed-4cf7-b3a7-bb1a5ca87df0","name":"What's At Idx - Point Query Range Update","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 ind : In this you have to tell the value of arr[ind].\r\n1. 1 l r val: In this query u have to increase all elements in this interval [l, r] by val.\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 l, int r, int val): increase all elements in this interval [l, r] by val,\r\n3. int query(int ind): return arr[ind].","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 ind,\r\n1 l r val","outputFormat":"for each query of type 0 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], val &lt;= 10^4.\r\n4. 0 &lt;= ind &lt; n","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 l, int r, int val) {\r\n\r\n }\r\n\r\n int query(int ind) {\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\r\n if (t == 0) {\r\n int ind = Integer.parseInt(inp[1]);\r\n long ans = obj.query(ind);\r\n out.append(ans + \"\\n\");\r\n } else {\r\n int l = Integer.parseInt(inp[1]);\r\n int r = Integer.parseInt(inp[2]);\r\n int val = Integer.parseInt(inp[3]);\r\n obj.update(l, r, val);\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":"8\r\n4\r\n3\r\n9\r\n3\r\n9\r\n8\r\n7\r\n2\r\n5\r\n1 4 6 0\r\n0 7\r\n1 0 3 4\r\n0 4\r\n0 5\r\n","sampleOutput":"2\r\n9\r\n8\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":"059f9009-7e44-4ad2-9a06-fd536d21bea1","name":"What's At Idx - Point Query Range Update","slug":"what-s-at-idx-point-query-range-update","type":1}],"next":{"id":"049f89aa-c540-40f2-b90a-57097e32045c","name":"Sum Of Range - Range Query Range Update","type":1,"slug":"sum-of-range-range-query-range-update"},"prev":{"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"}}}`

Editor

# What's At Idx - Point Query Range Update

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 ind : In this you have to tell the value of arr[ind]. 1. 1 l r val: In this query u have to increase all elements in this interval [l, r] by val. 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 l, int r, int val): increase all elements in this interval [l, r] by val, 3. int query(int ind): return arr[ind].

## Constraints

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

## 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 ind, 1 l r val

### Output

for each query of type 0 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;}8 4 3 9 3 9 8 7 2 5 1 4 6 0 0 7 1 0 3 4 0 4 0 5 ```

### 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;}2 9 8 ```

Discussions

Show Discussion

Related Resources