forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandling-sum-queries-after-update.cpp
138 lines (130 loc) · 4.08 KB
/
handling-sum-queries-after-update.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
// Time: O(nlogn + m + qlogn)
// Space: O(n)
// segment tree
class Solution {
public:
vector<long long> handleQuery(vector<int>& nums1, vector<int>& nums2, vector<vector<int>>& queries) {
const auto& build = [&](const auto& i) {
return vector<int>{nums1[i], nums1[i] ^ 1};
};
const auto& query = [](const auto& x, const auto& y) {
if (empty(x)) {
return y;
}
return vector<int>{x[0] + y[0], x[1] + y[1]};
};
const auto& update = [](const auto& x, const auto& y) {
if (empty(x)) {
return y;
}
return y == vector<int>{1, 0} ? vector<int>{x[1], x[0]} : vector<int>{x[0], x[1]};
};
SegmentTree<vector<int>> st(size(nums1), build, query, update);
vector<long long> result;
int64_t total = accumulate(cbegin(nums2), cend(nums2), 0LL);
for (const auto& q : queries) {
if (q[0] == 1) {
st.update(q[1], q[2], vector<int>{1, 0});
} else if (q[0] == 2) {
total += static_cast<int64_t>(st.query(0, size(nums1) - 1)[0]) * q[1];
} else if (q[0] == 3) {
result.emplace_back(total);
}
}
return result;
}
private:
template <typename T>
class SegmentTree {
public:
explicit SegmentTree(
int N,
const function<T(const int&)>& build_fn,
const function<T(const T&, const T&)>& query_fn,
const function<T(const T&, const T&)>& update_fn)
: base_(N),
tree_(2 * N),
lazy_(2 * N),
build_fn_(build_fn),
query_fn_(query_fn),
update_fn_(update_fn) {
H_ = 1;
while ((1 << H_) < N) {
++H_;
}
for (int i = base_; i < base_ + N; ++i) {
tree_[i] = build_fn_(i - base_);
}
for (int i = base_ - 1; i >= 1; --i) {
tree_[i] = query_fn_(tree_[2 * i], tree_[2 * i + 1]);
}
}
void update(int L, int R, const T& val) {
L += base_;
R += base_;
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);
}
T query(int L, int R) {
T result{};
if (L > R) {
return result;
}
L += base_;
R += base_;
push(L);
push(R);
for (; L <= R; L >>= 1, R >>= 1) {
if ((L & 1) == 1) {
result = query_fn_(result, tree_[L++]);
}
if ((R & 1) == 0) {
result = query_fn_(result, tree_[R--]);
}
}
return result;
}
private:
void apply(int x, const T& val) {
tree_[x] = update_fn_(tree_[x], val);
if (x < base_) {
lazy_[x] = update_fn_(lazy_[x], val);
}
}
void pull(int x) {
while (x > 1) {
x >>= 1;
tree_[x] = query_fn_(tree_[x * 2], tree_[x * 2 + 1]);
if (!empty(lazy_[x])) {
tree_[x] = update_fn_(tree_[x], lazy_[x]);
}
}
}
void push(int x) {
for (int h = H_; h > 0; --h) {
int y = x >> h;
if (!empty(lazy_[y])) {
apply(y * 2, lazy_[y]);
apply(y * 2 + 1, lazy_[y]);
lazy_[y].clear();
}
}
}
int base_;
int H_;
vector<T> tree_;
vector<T> lazy_;
const function<T(const int&)> build_fn_;
const function<T(const T&, const T&)> query_fn_;
const function<T(const T&, const T&)> update_fn_;
};
};