#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef pair<int,int> pii;
struct SegmentTree{
vector<int> tree, lazy;
void init(int n) {
tree.resize(n*4+1);
fill(tree.begin(), tree.end(), 0);
}
void update(int node, int s, int e, int tar, int val) {
if (s > tar || tar > e) return;
tree[node] += val;
if (s == e) return;
int mid = s + e >> 1;
update(node*2, s, mid, tar, val);
update(node*2+1, mid+1, e, tar, val);
}
int query(int node, int s, int e, int l, int r) {
if (l > e || s > r) return 0;
if (l <= s && e <= r) return tree[node];
int mid = s + e >> 1;
return query(node*2, s, mid, l, r) + query(node*2+1, mid+1, e, l, r);
}
} seg;
struct Query{
int k, a, b;
};
int N, M, Q;
int a[200010];
int w[200010];
Query q[200010];
void Update(int i) {
int cnt = 0;
if (i > 1 && a[i-1] > a[i]) cnt ++;
if (i < N && a[i+1] >= a[i]) cnt ++;
if (cnt == 0) seg.update(1, 1, M, a[i], 1-w[i]), w[i]=1;
else if (cnt == 1) seg.update(1, 1, M, a[i], -w[i]), w[i]=0;
else seg.update(1, 1, M, a[i], -1-w[i]), w[i]=-1; // cnt == 2
}
int main() {
scanf("%d %d", &N, &Q);
vector<int> t;
for (int i=1; i<=N; i++) {
scanf("%d", &a[i]);
t.push_back(a[i]);
}
for (int i=1; i<=Q; i++) {
scanf("%d", &q[i].k);
if (q[i].k == 1) {
scanf("%d", &q[i].a);
q[i].b = 0;
t.push_back(q[i].a);
}
else {
scanf("%d %d", &q[i].a, &q[i].b);
t.push_back(q[i].b);
}
}
sort(t.begin(), t.end());
t.erase(unique(t.begin(), t.end()), t.end());
for (int i=1; i<=N; i++) {
a[i] = lower_bound(t.begin(), t.end(), a[i]) - t.begin() + 1;
}
for (int i=1; i<=Q; i++) {
if (q[i].k == 1)
q[i].a = lower_bound(t.begin(), t.end(), q[i].a) - t.begin() + 1;
else
q[i].b = lower_bound(t.begin(), t.end(), q[i].b) - t.begin() + 1;
}
M = (int) t.size();
seg.init(M);
for (int i=1; i<=N; i++) Update(i);
for (int i=1; i<=Q; i++) {
if (q[i].k == 1) {
printf("%d\n", seg.query(1, 1, M, q[i].a, M));
}
else {
seg.update(1, 1, M, a[q[i].a], -w[q[i].a]);
w[q[i].a] = 0;
a[q[i].a] = q[i].b;
Update(q[i].a);
if (q[i].a > 1) Update(q[i].a - 1);
if (q[i].a < N) Update(q[i].a + 1);
}
}
return 0;
}
Compilation message
employment.cpp: In member function 'void SegmentTree::update(int, int, int, int, int)':
employment.cpp:19:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
19 | int mid = s + e >> 1;
| ~~^~~
employment.cpp: In member function 'int SegmentTree::query(int, int, int, int, int)':
employment.cpp:26:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | int mid = s + e >> 1;
| ~~^~~
employment.cpp: In function 'int main()':
employment.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d %d", &N, &Q);
| ~~~~~^~~~~~~~~~~~~~~~~
employment.cpp:53:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
employment.cpp:57:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%d", &q[i].k);
| ~~~~~^~~~~~~~~~~~~~~
employment.cpp:59:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%d", &q[i].a);
| ~~~~~^~~~~~~~~~~~~~~
employment.cpp:64:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | scanf("%d %d", &q[i].a, &q[i].b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
452 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
15 ms |
3932 KB |
Output is correct |
5 |
Correct |
29 ms |
5084 KB |
Output is correct |
6 |
Correct |
46 ms |
6180 KB |
Output is correct |
7 |
Correct |
70 ms |
8136 KB |
Output is correct |
8 |
Correct |
94 ms |
9548 KB |
Output is correct |
9 |
Correct |
155 ms |
13932 KB |
Output is correct |
10 |
Correct |
141 ms |
13360 KB |
Output is correct |
11 |
Correct |
188 ms |
15796 KB |
Output is correct |
12 |
Correct |
196 ms |
17256 KB |
Output is correct |
13 |
Correct |
207 ms |
17416 KB |
Output is correct |
14 |
Correct |
190 ms |
17084 KB |
Output is correct |
15 |
Correct |
192 ms |
17084 KB |
Output is correct |
16 |
Correct |
201 ms |
17712 KB |
Output is correct |
17 |
Correct |
197 ms |
17712 KB |
Output is correct |
18 |
Correct |
198 ms |
17580 KB |
Output is correct |
19 |
Correct |
201 ms |
17792 KB |
Output is correct |
20 |
Correct |
198 ms |
17600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
452 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
344 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
15 ms |
3932 KB |
Output is correct |
20 |
Correct |
29 ms |
5084 KB |
Output is correct |
21 |
Correct |
46 ms |
6180 KB |
Output is correct |
22 |
Correct |
70 ms |
8136 KB |
Output is correct |
23 |
Correct |
94 ms |
9548 KB |
Output is correct |
24 |
Correct |
155 ms |
13932 KB |
Output is correct |
25 |
Correct |
141 ms |
13360 KB |
Output is correct |
26 |
Correct |
188 ms |
15796 KB |
Output is correct |
27 |
Correct |
196 ms |
17256 KB |
Output is correct |
28 |
Correct |
207 ms |
17416 KB |
Output is correct |
29 |
Correct |
190 ms |
17084 KB |
Output is correct |
30 |
Correct |
192 ms |
17084 KB |
Output is correct |
31 |
Correct |
201 ms |
17712 KB |
Output is correct |
32 |
Correct |
197 ms |
17712 KB |
Output is correct |
33 |
Correct |
198 ms |
17580 KB |
Output is correct |
34 |
Correct |
201 ms |
17792 KB |
Output is correct |
35 |
Correct |
198 ms |
17600 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
2 ms |
624 KB |
Output is correct |
38 |
Correct |
3 ms |
600 KB |
Output is correct |
39 |
Correct |
16 ms |
1884 KB |
Output is correct |
40 |
Correct |
34 ms |
4824 KB |
Output is correct |
41 |
Correct |
61 ms |
6348 KB |
Output is correct |
42 |
Correct |
78 ms |
7700 KB |
Output is correct |
43 |
Correct |
106 ms |
9400 KB |
Output is correct |
44 |
Correct |
191 ms |
15200 KB |
Output is correct |
45 |
Correct |
166 ms |
12840 KB |
Output is correct |
46 |
Correct |
196 ms |
13936 KB |
Output is correct |
47 |
Correct |
246 ms |
17208 KB |
Output is correct |
48 |
Correct |
264 ms |
17348 KB |
Output is correct |
49 |
Correct |
243 ms |
17920 KB |
Output is correct |
50 |
Correct |
260 ms |
17080 KB |
Output is correct |
51 |
Correct |
248 ms |
17752 KB |
Output is correct |
52 |
Correct |
247 ms |
17636 KB |
Output is correct |
53 |
Correct |
245 ms |
17576 KB |
Output is correct |
54 |
Correct |
254 ms |
17652 KB |
Output is correct |
55 |
Correct |
258 ms |
17728 KB |
Output is correct |