`{"id":"30b2c8a8-e496-472a-868c-618477592a7f","name":"Sum Of Squares","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 l r : In this you have to return sum of squares of all elements int range [l, r].\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. long query(int l, int r): return sum of squares of all elements arr[i] for which i is in range [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\r\n0 l r\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.","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 long 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);\r\n int l = Integer.parseInt(inp);\r\n int r = Integer.parseInt(inp);\r\n\r\n if (t == 0) {\r\n long ans = obj.query(l, r);\r\n out.append(ans + \"\\n\");\r\n } else {\r\n int val = Integer.parseInt(inp);\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":"hard","sampleInput":"8\r\n0\r\n10\r\n10\r\n-1\r\n5\r\n8\r\n10\r\n2\r\n5\r\n0 7 7\r\n1 4 6 1\r\n0 2 4\r\n1 5 5 7\r\n0 3 7\r\n","sampleOutput":"4\r\n137\r\n418\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":"ae20c9d9-8ab3-41f2-8b1e-72f4092fc46f","name":"Sum Of Squares","slug":"sum-of-squares","type":1}],"next":{"id":"5eccc617-64f0-4ca6-8282-6acb4e1c5160","name":"2d Segment Tree","type":1,"slug":"2d-segment-tree"},"prev":{"id":"9031427d-cb38-4373-8193-f988b61bdeed","name":"Maximum Sum","type":1,"slug":"maximum-sum"}}}`

# Sum Of Squares

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 l r : In this you have to return sum of squares of all elements int range [l, r]. 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. long query(int l, int r): return sum of squares of all elements arr[i] for which i is in range [l, r].

`{"id":"30b2c8a8-e496-472a-868c-618477592a7f","name":"Sum Of Squares","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 l r : In this you have to return sum of squares of all elements int range [l, r].\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. long query(int l, int r): return sum of squares of all elements arr[i] for which i is in range [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\r\n0 l r\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.","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 long 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);\r\n int l = Integer.parseInt(inp);\r\n int r = Integer.parseInt(inp);\r\n\r\n if (t == 0) {\r\n long ans = obj.query(l, r);\r\n out.append(ans + \"\\n\");\r\n } else {\r\n int val = Integer.parseInt(inp);\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":"hard","sampleInput":"8\r\n0\r\n10\r\n10\r\n-1\r\n5\r\n8\r\n10\r\n2\r\n5\r\n0 7 7\r\n1 4 6 1\r\n0 2 4\r\n1 5 5 7\r\n0 3 7\r\n","sampleOutput":"4\r\n137\r\n418\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":"ae20c9d9-8ab3-41f2-8b1e-72f4092fc46f","name":"Sum Of Squares","slug":"sum-of-squares","type":1}],"next":{"id":"5eccc617-64f0-4ca6-8282-6acb4e1c5160","name":"2d Segment Tree","type":1,"slug":"2d-segment-tree"},"prev":{"id":"9031427d-cb38-4373-8193-f988b61bdeed","name":"Maximum Sum","type":1,"slug":"maximum-sum"}}}` Editor

# Sum Of Squares

hard

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 l r : In this you have to return sum of squares of all elements int range [l, r]. 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. long query(int l, int r): return sum of squares of all elements arr[i] for which i is in range [l, r].

## Constraints

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

## Format

### Input

A number n n1 n2 .. n number of elements A number q following q lines contains queries of format 0 l r 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 0 10 10 -1 5 8 10 2 5 0 7 7 1 4 6 1 0 2 4 1 5 5 7 0 3 7 ```

### 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;}4 137 418 ```

Discussions

Show Discussion

Related Resources 