-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsegmenttree.cpp
More file actions
137 lines (130 loc) · 2.03 KB
/
segmenttree.cpp
File metadata and controls
137 lines (130 loc) · 2.03 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
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
129
130
131
132
133
134
135
136
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define rt return 0
#define endln "\n"
#define for1(i,a,b) for(long long int i=a;i<b;i++)
#define for2(i,a,b) for(long long int i=a;i>b;i--)
// cout << fixed << setprecision(0) << pi <<" "<<npi<<endl;
typedef long long int int1;
vector<int1>v;
int seg[400005];
int build(int pos,int low,int high)
{
if(low>high)
{
return 0;
}
else if(low == high)
{
if(v[low]%2 == 0)
seg[pos] = 1;
else
seg[pos]=0;
return 0;
}
else
{
int mid = (low+high)/2;
build(2*pos,low,mid);
build(2*pos+1,mid+1,high);
seg[pos] = (seg[2*pos] + seg[2*pos+1]) ;
return 0;
}
return 0;
}
// }
int update(int pos , int low , int high,int x ,int y)
{
// cout << pos << endl;
if(low > x || high < x)
{
return 0;
}
else if( low == high)
{
if(y%2 == 0)
{
seg[pos]=1;
}
else
seg[pos]=0;
}
else
{
int mid = (low+high)/2;
if(mid >= x)
{
update(2*pos,low,mid,x,y);
seg[pos] = seg[2*pos] + seg[2*pos+1];
}
else
{
update(2*pos+1,mid+1,high,x,y);
seg[pos] = seg[2*pos] + seg[2*pos+1];
}
}
return 0;
}
int query(int pos,int low,int high,int l,int h)
{
// cout << pos << endl;
if(l>high || h<low)
{
return 0;
}
else if(l <=low && h>=high)
{
return seg[pos];
}
else
{
int k=0,p=0;
int mid =(low+high)/2;
if( mid >=l)
{
k = query(2*pos,low,mid,l,h);
}
if(h>=mid+1 )
{
p = query(2*pos+1,mid+1,high,l,h);
}
// cout << k<<p<<endl;
int a = (k+p);
return a;
}
}
int main()
{
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
int1 x,y,n,k,m,a,b;
scanf("%lld",&n);
v.pb(0);
for1(i,0,n)
{
scanf("%lld",&k);
v.pb(k);
}
build(1,1,n);
// cin >> m;
scanf("%lld",&m);
for1(i,0,m)
{
// cin>>a>>x>>y;
scanf("%lld %lld %lld",&a,&x,&y);
if(a==0)
{
update(1,1,n,x,y);
}
else
{
int1 p = query(1,1,n,x,y);
if(a==1)
printf("%lld\n",p);
else
printf("%lld\n",y-x+1-p);
}
}
}