-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathmain.cpp
176 lines (137 loc) · 3.4 KB
/
main.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
#include <bits/stdc++.h>
using namespace std; // NOLINT
constexpr bool TESTS = false;
// Maximum length of the original string.
constexpr int MAX_S = 100000;
// Maximum length of each infection string. Determines how many offsets and
// periods we need to store in T.
constexpr int MAX_E = 10;
// For each character E[i] we want to ask: How many times does E[i] occur in
// S[l...r] with offset (l + i) % |E| and period |E|?
//
// To make this possible we make T[c][o][p] a bit mask where T[c][o][p][e] is 1
// if S[i] = c and S[i] has offset, period = o, p. Otherwise the entry is 0.
// This bit mask can be represented with a Binary Index Tree for O(log MAX_S)
// point updates and range sums.
int T[4][MAX_E][MAX_E + 1][MAX_S + 1] = {0};
// Original string.
string S;
int chr(char c) {
switch (c) {
case 'A': return 0;
case 'T': return 1;
case 'G': return 2;
case 'C': return 3;
default: assert(false);
}
}
//
// Updates index i in BIT T[c][o][p] by x.
//
void update(int c, int o, int p, int i, int x) {
assert(i >= 0 && i < MAX_S);
i++;
while (i <= MAX_S) {
T[c][o][p][i] += x;
i += (i & -i);
}
}
//
// Updates all BIT's for S[i] by x in O(log MAX_S) time.
//
void update(int i, int x) {
int c = chr(S[i]);
for (int p = 1; p <= MAX_E; p++) {
for (int o = 0; o < p && o <= i; o++) {
if ((i - o) % p == 0) {
update(c, o, p, i, x);
}
}
}
}
//
// Prefix Sum Query on first i elements of T[c][o][p].
//
int psq(int c, int o, int p, int i) {
assert(o >= 0 && o < p && p <= MAX_E);
assert(i >= 0 && i <= MAX_S);
int s = 0;
while (i > 0) {
s += T[c][o][p][i];
i -= (i & -i);
}
return s;
}
//
// Range Sum Query on range [l, r] of T[c][o][p].
//
int rsq(int c, int o, int p, int l, int r) {
return psq(c, o, p, r + 1) - psq(c, o, p, l);
}
//
// Populates T to reflect the initial state of string S.
//
void preprocess() {
assert(!S.empty());
assert(S.size() <= MAX_S);
memset(T, 0, sizeof(T));
int n = S.size();
for (int i = 0; i < n; i++) {
update(i, 1);
}
}
void test() {
cout << "Begin basic tests..." << endl;
S = "ATGCATGC";
preprocess();
assert(rsq(chr('A'), 0, 1, 0, 7) == 2);
assert(rsq(chr('T'), 0, 1, 0, 7) == 2);
assert(rsq(chr('G'), 0, 1, 0, 7) == 2);
assert(rsq(chr('C'), 0, 1, 0, 7) == 2);
assert(rsq(chr('A'), 0, 4, 0, 7) == 2);
assert(rsq(chr('T'), 1, 4, 0, 7) == 2);
assert(rsq(chr('G'), 2, 4, 0, 7) == 2);
assert(rsq(chr('C'), 3, 4, 0, 7) == 2);
assert(rsq(chr('A'), 1, 3, 0, 2) == 0);
assert(rsq(chr('A'), 1, 3, 3, 7) == 1);
assert(rsq(chr('A'), 1, 3, 0, 7) == 1);
assert(rsq(chr('T'), 1, 2, 0, 7) == 2);
update(3, -1);
S[3] = 'T';
update(3, 1);
assert(rsq(chr('A'), 0, 2, 1, 5) == 1);
assert(rsq(chr('T'), 1, 2, 1, 5) == 3);
cout << "Tests pass!" << endl;
}
int main() {
ios::sync_with_stdio(false);
if (TESTS) test();
cin >> S;
preprocess();
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int t;
cin >> t;
if (t == 1) {
int x;
char c;
cin >> x >> c;
update(x - 1, -1);
S[x - 1] = c;
update(x - 1, 1);
} else if (t == 2) {
int l, r;
string E;
cin >> l >> r >> E;
int n = E.size(), v = 0;
for (int j = 0; j < n; j++) {
int k = chr(E[j]);
int o = (l + j - 1) % n;
v += rsq(k, o, n, l - 1, r - 1);
}
cout << v << endl;
}
}
return 0;
}