-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
alternating-groups-iii.cpp
98 lines (91 loc) · 2.81 KB
/
alternating-groups-iii.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
// Time: O(nlogn + qlogn)
// Space: O(n)
// sorted list, freq table, bit, fenwick tree
class Solution {
private:
class BIT {
public:
BIT(int n) : bit_(n + 1) { // 0-indexed
}
void add(int i, int val) {
++i;
for (; i < size(bit_); i += lower_bit(i)) {
bit_[i] += val;
}
}
int query(int i) const {
++i;
int total = 0;
for (; i > 0; i -= lower_bit(i)) {
total += bit_[i];
}
return total;
}
private:
int lower_bit(int i) const {
return i & -i;
}
vector<int> bit_;
};
public:
vector<int> numberOfAlternatingGroups(vector<int>& colors, vector<vector<int>>& queries) {
const int n = size(colors);
set<int> bst;
BIT bit1(n + 1), bit2(n + 1);
const auto& update = [&](int i, int d) {
if (d == +1) {
bst.emplace(i);
if (size(bst) == 1) {
bit1.add(n, +1);
bit2.add(n, +n);
}
}
auto curr = bst.find(i);
auto prv = prev(curr != begin(bst) ? curr : end(bst));
auto nxt = next(curr);
if (nxt == end(bst)) {
nxt = begin(bst);
}
if (size(bst) != 1) {
int l = ((*nxt) - (*prv) + (n - 1)) % n + 1;
bit1.add(l, d * -1);
bit2.add(l, d * -l);
l = ((*curr) - (*prv) + n) % n;
bit1.add(l, d * +1);
bit2.add(l, d * +l);
l = ((*nxt) - (*curr) + n) % n;
bit1.add(l, d * +1);
bit2.add(l, d * +l);
}
if (d == -1) {
if (size(bst) == 1) {
bit1.add(n, -1);
bit2.add(n, -n);
}
bst.erase(curr);
}
};
vector<int> result;
for (int i = 0; i < n; ++i) {
if (colors[i] == colors[(i + 1) % n]) {
update(i, +1);
}
}
for (const auto &q : queries) {
if (q[0] == 1) {
const int l = q[1];
result.emplace_back(!empty(bst) ? (bit2.query(n) - bit2.query(l - 1)) -
(l - 1) * (bit1.query(n) - bit1.query(l - 1)) : n);
continue;
}
const int i = q[1];
if (colors[i] == q[2]) {
continue;
}
colors[i] = q[2];
update((i - 1 + n) % n, colors[i] == colors[(i - 1 + n) % n] ? +1 : -1);
update(i, colors[i] == colors[(i + 1) % n] ? +1 : -1);
}
return result;
}
};