-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathderevootrezkov
82 lines (67 loc) · 1.13 KB
/
derevootrezkov
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
// Example program
#include <iostream>
#include <string>
#include<vector>
using namespace std;
vector<long long> t;
long long sum(int v,int vl,int vr,int l,int r){
if (r<vl || vr<l)
return 0;
if(vl>=l && r>=vr)
return t[v];
int vs=vl+(vr-vl)/2;
long long d=sum(2*v+1,vl,vs,l,r);
long long m=sum(2*v+2,vs+1,vr,l,r);
return d+m;
}
void b(int v,int vl,int vr,vector<int> a){
if(vl==vr){
t[v]=a[vl];
}
else{
int vs=vl+(vr-vl)/2;
b(2*v+1,vl,vs,a);
b(2*v+2,vs+1,vr,a);
t[v]=t[2*v+1]+t[2*v+2];
}
}
void modify(int v,int vl,int vr,int pos,int val){
if(vl==vr){
t[v]=val;
return;
}
int vm=vl+(vr-vl)/2;
if(pos<=vm){
modify(2*v+1,vl,vm,pos,val);
}
else{
modify(2*v+2,vm+1,vr,pos,val);
}
t[v]=t[2*v+1]+t[2*v+2];
}
int main(){
int n,k;
cin>>n;
char query;
vector<int>a(n,0);
for(int i=0;i<n;i++){
cin>>a[i];
}
t.resize(4*n);
b(0,0,n-1,a);
cin>>k;
vector<int>c;
for(int i=0;i<k;i++){
int l,r;
cin>>query;
cin>>l>>r;
l--;
r--;
if(query=='s'){
cout<<sum(0,0,n-1,l,r)<<" ";
}
else{
modify(0,0,n-1,l,r+1);
}
}
}