forked from MohmedIkram/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sumpariwise.cpp
69 lines (61 loc) · 1.79 KB
/
Sumpariwise.cpp
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
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;
int main()
{
int querry;
cin>>querry;
int size;
cin>>size;
int A[size];
for(int i=0; i<size; i++)
cin>>A[i];
vector<int> pre_sum(size,A[0]), suf_sum(size,A[size-1]);
long long ans=0;
for(int i=1; i<size; i++)
pre_sum[i]=pre_sum[i-1]+A[i];
for(int i=size-2; i>=0; i--)
{
suf_sum[i]=suf_sum[i+1]+A[i];
ans=ans+suf_sum[i+1]*A[i];
}
vector<long long> result;
while(querry)
{
int a,b,c;
cin>>a>>b>>c;
if(a==0)
{
long long temp=ans;
for(int i=0; i<b-1; i++)
temp=temp-suf_sum[i+1]*A[i];
for(int i=c; i<size; i++)
{
temp=temp-pre_sum[i-1]*A[i];
if(b-2>=0)
temp=temp+pre_sum[b-2]*A[i];
}
cout<<temp<<endl;
result.push_back(temp);
}
else
{
int ab=A[b-1];
for(int i=b-1; i<size; i++)
pre_sum[i]=pre_sum[i]-ab+c;
for(int i=b-1; i>=0; i--)
suf_sum[i]=suf_sum[i]-ab+c;
A[b-1]=c;
ans=0;
for(int i=size-2; i>=0; i--)
ans=ans+suf_sum[i+1]*A[i];
cout<<ans<<endl;
}
querry--;
}
return 0;
}