-
Notifications
You must be signed in to change notification settings - Fork 0
/
Segment tree.cpp
128 lines (81 loc) · 1.92 KB
/
Segment tree.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include<stdio.h>
#include<string.h>
using namespace std;
const int N = 50005;
long long a[N], tree[4*N], MOD = 1000000007LL;
void construct(int node, int lo, int hi)
{
if(lo==hi)
{
tree[node] = a[lo];
return;
}
int Left = 2*node;
int Right = Left + 1;
int mid = (lo+hi)>>1;
construct(Left, lo, mid);
construct(Right, mid+1, hi);
tree[node] = tree[Left] * tree[Right];
tree[node] %= MOD;
}
long long query(int node, int lo, int hi, int i, int j)
{
if(i>hi || j<lo) return 1;
if(i<=lo && j>=hi) return tree[node];
int left = 2*node;
int right = left + 1;
int mid = (lo+hi)>>1;
long long x = 1, y = 1;
x = query(left, lo, mid, i, j);
y = query(right, mid+1, hi, i, j);
return (x*y)%MOD;
}
void update(int node, int lo, int hi, int i, long long val)
{
if(i>hi || i<lo) return;
if(lo==hi)
{
tree[node] = val;
return;
}
int left = 2*node;
int right = left + 1;
int mid = (lo+hi)>>1;
update(left, lo, mid, i, val);
update(right, mid+1, hi, i, val);
tree[node] = tree[left] * tree[right];
tree[node] %= MOD;
}
int main()
{
int T, C;
scanf("%d", &T);
for(C=1; C<=T; C++)
{
memset(a, 0, sizeof a);
memset(tree, 0, sizeof tree);
int n, q, t, x, y, i;
scanf("%d", &n);
for(i=1; i<=n; i++) scanf("%lld", &a[i]);
construct(1, 1, n);
scanf("%d", &q);
while(q--)
{
scanf("%d", &t);
if(t==1)
{
int k;
long long p;
scanf("%d", &k);
scanf("%lld", &p);
update(1, 1, n, k, p);
}
else
{
scanf("%d %d", &x, &y);
printf("%lld\n", query(1, 1, n, x, y));
}
}
}
return 0;
}