-
Notifications
You must be signed in to change notification settings - Fork 12
/
1135-count-multiples-of-3.cpp
195 lines (131 loc) · 3.5 KB
/
1135-count-multiples-of-3.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <bits/stdc++.h>
#define ll long long
#define MAX 100100
using namespace std;
ll arr[100100];
vector <pair <ll,ll> > v;
struct sam {
int lazy;
int yo;
int arr[3];
};
struct sam nodes[500100];
void initilize(int node)
{
nodes[node].lazy = 0;
nodes[node].yo = 0;
for(int i=0;i < 3;i++)
nodes[node].arr[i] = 0;
nodes[node].arr[0]= 1;
}
void buildtree(int node,int start,int end,int val)
{
if(start > end)
return;
if(start == end)
{
initilize(node);
return;
}
int mid = start + end;
mid /= 2;
buildtree(node*2+1,start ,mid,val);
buildtree(node*2+2,mid+1,end,val);
initilize(node);
nodes[node].arr[0] = end-start+1;
return;
}
void update(int node,int start,int end,int qstr,int qend,int val)
{
if(nodes[node].lazy != 0) {
int temp = nodes[node].lazy ;
temp %= 3;
if(start != end)
nodes[node*2+1].lazy += temp, nodes[node*2+2].lazy +=temp;
int zeroes = nodes[node].arr[0], ones = nodes[node].arr[1],twos = nodes[node].arr[2];
nodes[node].arr[temp%3] = zeroes;
nodes[node].arr[(1+temp)% 3] = ones;
nodes[node].arr[(2+temp)%3] = twos;
nodes[node].lazy = 0;
}
if(start > end)
return;
if(qend < start || qstr > end)
return;
if(qstr <= start and qend >= end) {
int temp = val;
temp %= 3;
if(start != end)
nodes[node*2+1].lazy += temp, nodes[node*2+2].lazy +=temp;
int zeroes = nodes[node].arr[0], ones = nodes[node].arr[1],twos = nodes[node].arr[2];
nodes[node].arr[temp%3] = zeroes;
nodes[node].arr[(1+temp)% 3] = ones;
nodes[node].arr[(2+temp)%3] = twos;
nodes[node].lazy = 0;
return;
}
int mid =start + end;
mid /= 2;
update(node*2 +1,start,mid,qstr,qend,val);
update(node*2+2,mid+1,end,qstr,qend,val);
for(int i=0;i < 3;i++)
nodes[node].arr[i] = nodes[2*node+1].arr[i] + nodes[2*node+2].arr[i];
}
ll query(int node,int start,int end,int qstr,int qend)
{
if(nodes[node].lazy != 0) {
int temp = nodes[node].lazy ;
temp %= 3;
if(start != end)
nodes[node*2+1].lazy += temp, nodes[node*2+2].lazy +=temp;
int zeroes = nodes[node].arr[0], ones = nodes[node].arr[1],twos = nodes[node].arr[2];
nodes[node].arr[temp%3] = zeroes;
nodes[node].arr[(1+temp)% 3] = ones;
nodes[node].arr[(2+temp)%3] = twos;
nodes[node].lazy = 0;
}
if(start > end)
return 0;
if(qend < start || qstr > end)
return 0;
if(qstr <= start and qend >= end) {
return nodes[node].arr[0];
}
int mid =start + end;
mid /= 2;
int left = query(node*2 +1,start,mid,qstr,qend);
int right = query(node*2+2,mid+1,end,qstr,qend);
return left + right;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/sameer/Documents/sameer/input.sam","r",stdin);
#endif
//ios_base::sync_with_stdio(false);
ll i,j,k,n,m,t,cont,a,b;
//cin >> t;
scanf("%lld",&t);
ll cases = t;
while(t--){
//cin >> n >> m;
scanf("%lld %lld",&n,&m);
memset(nodes,0,sizeof nodes);
// cout << "Case " << cases-t << ":" << endl;
printf("Case %lld:\n",cases-t );
buildtree(0,0,n-1,0);
//cout << "after bldng :" << nodes[1].arr[0] << " " << nodes[2].arr[0] << " "<< nodes[0].arr[2] << endl;
for(i =0;i < m;i++) {
//cin >> k;
// cin >> a >> b;
scanf("%lld %lld %lld",&k,&a,&b);
if(k == 0){
update(0,0,n-1,a,b,1);
}else {
ll ans = query(0,0,n-1,a,b) ;
printf("%lld\n",ans);
}
}
}
return 0;
}