-
Notifications
You must be signed in to change notification settings - Fork 9
/
GSS4 - Can you answer these queries IV.cpp
75 lines (75 loc) · 1.82 KB
/
GSS4 - Can you answer these queries IV.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
70
71
72
73
74
75
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mx 100005
ll arr[mx];
ll tree[mx*4];
ll mtree[mx*4];
void build(int pos, int b, int e)
{
if(b>e) return;
if(b==e){
tree[pos] = arr[b];
mtree[pos] = arr[b];
return ;
}
int mid = (b+e)/2;
int left = pos*2;
int right = left+1;
build(left, b, mid);
build(right,mid+1,e);
tree[pos] = tree[left] + tree[right];
mtree[pos] = max(mtree[left], mtree[right]);
}
void update(int pos, int b, int e, int i, int j)
{
if(b>e || b>j || e<i) return;
if(mtree[pos]==1) return;
if(b>=i && e<=j && b==e){
tree[pos] = sqrt(tree[pos]);
mtree[pos] = tree[pos];
return;
}
int mid = (b+e)/2;
int left = pos*2;
int right = left+1;
update(left,b,mid,i,j);
update(right,mid+1,e,i,j);
tree[pos] = tree[left] + tree[right];
mtree[pos] = max(mtree[left], mtree[right]);
}
ll query(int pos, int b, int e, int i, int j)
{
if(b>e || b>j || e<i) return 0;
if(b>=i && e<=j)return tree[pos];
int mid = (b+e)/2;
int left = pos*2;
int right = left+1;
return (query(left,b,mid,i,j)+query(right,mid+1,e,i,j));
}
int main()
{
int n,l,r,q,c;
int a;
int tc=1;
while(scanf("%d",&n)!=EOF){
memset(tree,0,sizeof tree);
memset(mtree,0,sizeof mtree);
for(int i=1; i<=n; i++){
scanf("%lld",&arr[i]);
}
build(1,1,n);
scanf("%d",&q);
printf("Case #%d:\n",tc++);
while(q--){
scanf("%d%d%d",&a, &l,&r);
if(l>r) swap(l,r);
if(a==0)
update(1,1,n,l,r);
else
printf("%lld\n",query(1,1,n,l,r));
}
printf("\n");
}
return 0;
}