-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy patharray insert
More file actions
82 lines (61 loc) · 1.83 KB
/
array insert
File metadata and controls
82 lines (61 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Gary likes to solve problems of array, but he doesn't like problems that involve queries. His school teacher gave him an assignment full of problems but one of them involve queries. Can you help Gary in solving that problem so that he can go and play with his friends? The problem is :
Given an Array A consisting of N positive integers, you have to answer Q queries on it. Queries can be of the two types: * 1 X Y - Update X at location Y in the array. * 2 L R - Print the sum of range [L, R], i.e. Both L and R are inclusive.
Note:- Array indexing is from 0.
Input:
The first line contains two space separated integers N(Length of Array) and Q(Queries).
Next Line contains N space separated integers denoting array A.
Next Q Line contains 3 space separated integers for each line, as described above
Output: Answer to the each query of type 2 in a new line, only when range is valid, otherwise ouput `-1`
Constraints: 1 <= N <= 10^9 1 <= Q <= 10^5 1 <= A[i], X, Y, L, R <= 10^5
SAMPLE INPUT
5 5
2 3 4 8 9
1 0 3
2 0 1
2 0 4
1 2 5
2 0 3
SAMPLE OUTPUT
6
27
19
Explanation
After First query:
Array becomes 3 3 4 8 9
After Second query:
Sum of range [0, 1] i.e. A[0]+A[1] is 6
After Third query
Sum of range [0, 4] is 27
After Fourth query
Array becomes 3 3 5 8 9
After Fifth query
Sum of range [0, 3] is 19
*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long n,a[n],q,i;
cin>>n>>q;
for(i=0;i<n;i++) cin>>a[i];
//for(int k=0;k<q;k++)
while(q--)
{
int t,a1,b;
cin>>t>>a1>>b;
if(t==1)
{
a[a1]=b;
}
else if(t==2)
{
long long sum=0;
for(int i=a1; i<=b;i++)
{
sum+=a[i];
}
cout<<sum<<endl;
}
}
}