-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.cpp
319 lines (291 loc) · 6.98 KB
/
4.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include "bits/stdc++.h"
using namespace std;
#define FOR_INC(i, start, end) for (int i = start; i < end; ++i)
#define FOR_DEC(i, start, end) for (int i = start; i > end; --i)
#define FOR_INC_EQUAL(i, start, end) for (int i = start; i <= end; ++i)
#define FOR_DEC_EQUAL(i, start, end) for (int i = start; i >= end; --i)
#define int long long
void getNums(string s)
{
string t;
for (char ch : s)
{
if (ch == '[')
{
t += '{';
}
else if (ch == ']')
{
t += '}';
}
else
{
t += ch;
}
}
cout << t << endl;
}
class Fenwick
{
public:
vector<int> f;
Fenwick(int n) : f(n + 1)
{
for (int i = 0; i < n; i++)
{
add(i + 1, f[i]);
}
}
static int lowBit(int n)
{
return n & -n;
}
void add(int i, int val)
{
for (; i < f.size(); i += lowBit(i))
{
f[i] += val;
}
}
int query(int i)
{
int res = 0;
for (; i > 0; i -= lowBit(i))
{
res += f[i];
}
return res;
}
int get(int index)
{
return f[index];
}
};
class UnionFind
{
std::vector<int> root;
std::vector<int> rank;
public:
explicit UnionFind(int size)
{
root.resize(size);
rank.resize(size);
for (int i = 0; i < size; ++i)
{
root[i] = rank[i] = i;
}
}
int find(int x)
{
if (x == root[x])
return x;
return root[x] = find(root[x]);
}
void connect(int x, int y)
{
int rootX = find(x);
int rootY = find(y);
if (rootX != rootY)
{
if (rank[rootX] > rank[rootY])
{
root[rootY] = rootX;
}
else if (rank[rootX] < rank[rootY])
{
root[rootX] = rootY;
}
else
{
root[rootY] = rootX;
rank[rootX] += 1;
}
}
}
bool isConnected(int x, int y)
{
return find(x) == find(y);
}
};
class NumArray
{
private:
vector<int> nums_;
Fenwick *fenwick_;
public:
NumArray(vector<int> &nums) : nums_(nums)
{
fenwick_ = new Fenwick(nums.size() + 1);
for (int i = 1; i <= nums.size(); i++)
{
fenwick_->add(i, nums[i - 1]);
}
}
void update(int index, int val)
{
fenwick_->add(index + 1, val - fenwick_->f[index]);
fenwick_->f[index] = val;
}
int sumRange(int left, int right)
{
return fenwick_->query(right + 1) - fenwick_->query(left);
}
};
class KMP
{
private:
vector<vector<int>> dp;
string pat_;
int size = 128;
public:
KMP(const string &pat)
{
this->pat_ = pat;
int n = pat.length();
dp.resize(n, vector<int>(size, 0));
dp[0][pat[0]] = 1;
int pre = 0;
for (int j = 1; j < n; j++)
{
for (int c = 0; c < size; c++)
{
dp[j][c] = dp[pre][c];
}
dp[j][pat[j]] = j + 1;
pre = dp[pre][pat[j]];
}
}
int search(const string &txt)
{
int M = pat_.size();
int N = txt.size();
int j = 0;
for (int i = 0; i < N; i++)
{
j = dp[j][txt[i]];
if (j == M)
{
return i - M + 1;
}
}
return -1;
}
};
struct ListNode
{
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(nullptr) {}
};
class Solution
{
public:
};
bool isPrime(int n)
{
if (n <= 2)
{
return true;
}
if (n % 2 == 0)
{
return false;
}
for (int i = 3; i <= sqrt(n); i += 2)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
class SegmentTree
{
public:
SegmentTree(vector<int> &data)
{
n = data.size();
tree.resize(2 * n);
build(data);
}
void update(int index, int value)
{
int pos = index + n; // 将原数组的索引映射到线段树的叶子节点位置
tree[pos] += value; // 更新对应位置的叶子节点(这里是增加操作)
while (pos > 1)
{ // 向上更新线段树的父节点
pos /= 2; // 移动到父节点
tree[pos] = max(tree[pos * 2], tree[pos * 2 + 1]); // 更新父节点的值为子节点的最大值
}
}
int query(int left, int right)
{
left += n; // 将查询区间的左端点映射到线段树
right += n + 1; // 将查询区间的右端点映射到线段树(右端点是开区间)
int max_value = INT_MIN; // 用来存储查询结果的变量
while (left < right)
{
if (left & 1)
{ // 如果左端点是奇数,说明左端点是一个右子节点
max_value = max(max_value, tree[left]); // 将左端点的值与当前最大值比较
left++; // 左端点向右移动
}
if (right & 1)
{ // 如果右端点是奇数,说明右端点是一个左子节点
right--; // 右端点向左移动
max_value = max(max_value, tree[right]); // 将右端点的值与当前最大值比较
}
left /= 2; // 向上移动到父节点
right /= 2; // 向上移动到父节点
}
return max_value; // 返回区间的最大值
}
private:
int n;
vector<int> tree;
void build(vector<int> &data)
{
for (int i = 0; i < n; i++)
{
tree[n + i] = data[i]; // 将数据填充到线段树的叶子节点
}
for (int i = n - 1; i > 0; i--)
{
tree[i] = max(tree[i * 2], tree[i * 2 + 1]); // 合并左右子树的最大值
}
}
};
// 线段树 https://leetcode.cn/problems/longest-increasing-subsequence-ii/solutions/1816920/by-lfool-f6vs/
signed main()
{
// Solution s;
// vector<vector<int>> grid{{0,1},{1,2},{2,0},{3,4},{4,5},{5,6},{6,3}};
// vector<int> nums{1,2,3,4,3,2,5};
// s.nonSpecialCount(1,2);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int> arr(n, 0);
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
SegmentTree segmentTree(arr);
int a, b, c;
for (int i = 0; i < m; i++)
{
cin >> a >> b >> c;
if (a == 1)
{
segmentTree.update(b - 1, c);
}
else
{
cout << segmentTree.query(b - 1, c - 1) << endl;
}
}
return 0;
}