-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathmaximize-count-of-distinct-primes-after-split.cpp
More file actions
148 lines (136 loc) · 4 KB
/
maximize-count-of-distinct-primes-after-split.cpp
File metadata and controls
148 lines (136 loc) · 4 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
137
138
139
140
141
142
143
144
145
146
147
148
// Time: O(r + nlogn + qlogn), r = max(nums)
// Space: O(r + n)
// number theory, bst, segment tree
vector<int> linear_sieve_of_eratosthenes(int n) { // Time: O(n), Space: O(n)
vector<int> spf(n + 1, -1);
vector<int> primes;
for (int i = 2; i <= n; ++i) {
if (spf[i] == -1) {
spf[i] = i;
primes.emplace_back(i);
}
for (const auto& p : primes) {
if (i * p > n || p > spf[i]) {
break;
}
spf[i * p] = p;
}
}
return spf;
}
const int MAX_N = 1e5;
const auto& SPF = linear_sieve_of_eratosthenes(MAX_N);
class Solution {
public:
vector<int> maximumCount(vector<int>& nums, vector<vector<int>>& queries) {
unordered_map<int, set<int>> lookup;
SegmentTree st(size(nums) - 1);
const auto& add = [&](int i, int d) {
const auto& x = nums[i];
if (SPF[x] != x) {
return;
}
if (d == 1) {
lookup[x].emplace(i);
}
if (size(lookup[x]) == 1) {
st.update(0, size(nums) - 2, d);
} else if (i == *begin(lookup[x])) {
st.update(i, *next(begin(lookup[x])) - 1, d);
} else if (i == *rbegin(lookup[x])) {
st.update(*next(rbegin(lookup[x])), i - 1, d);
}
if (d == -1) {
lookup[x].erase(i);
}
};
for (int i = 0; i < size(nums); ++i) {
add(i, +1);
}
vector<int> result(size(queries));
for (int i = 0; i < size(queries); ++i) {
const int idx = queries[i][0], x = queries[i][1];
if (nums[idx] != x) {
add(idx, -1);
nums[idx] = x;
add(idx, +1);
}
result[i] = st.tree[1]; // st.query(0, size(nums) - 2);
}
return result;
}
private:
class SegmentTree {
public:
explicit SegmentTree(int N)
: base_(N > 1 ? 1 << (__lg(N - 1) + 1) : 1),
lazy_(base_),
tree(N > 1 ? 1 << (__lg(N - 1) + 2) : 2) {
}
void update(int L, int R, const int val) {
L += base_;
R += base_;
// push(L); // enable if range assignment
// push(R); // enable if range assignment
int L0 = L, R0 = R;
for (; L <= R; L >>= 1, R >>= 1) {
if ((L & 1) == 1) {
apply(L++, val);
}
if ((R & 1) == 0) {
apply(R--, val);
}
}
pull(L0);
pull(R0);
}
int query(int L, int R) {
if (L > R) {
return 0;
}
L += base_;
R += base_;
push(L);
push(R);
int left = 0, right = 0;
for (; L <= R; L >>= 1, R >>= 1) {
if ((L & 1) == 1) {
left = max(left, tree[L++]);
}
if ((R & 1) == 0) {
right = max(tree[R--], right);
}
}
return max(left, right);
}
vector<int> tree;
private:
void apply(int x, const int val) {
tree[x] += val;
if (x < base_) {
lazy_[x] += val;
}
}
void pull(int x) {
while (x > 1) {
x >>= 1;
tree[x] = max(tree[x << 1], tree[(x << 1) + 1]);
if (lazy_[x]) {
tree[x] += lazy_[x];
}
}
}
void push(int x) {
for (int h = __lg(x) - 1; h > 0; --h) {
int y = x >> h;
if (lazy_[y]) {
apply(y << 1, lazy_[y]);
apply((y << 1) + 1, lazy_[y]);
lazy_[y] = 0;
}
}
}
int base_;
vector<int> lazy_;
};
};