#include <iostream>
#include <algorithm>
using namespace std;
typedef pair < int, int > PII;
typedef pair < int, PII > Type;
const int NMAX = 2e5 + 1, QMAX = 2e5 + 1;
int N, Q, A[NMAX];
Type Queries[QMAX];
int B[(NMAX + QMAX)], V[(NMAX + QMAX)];
static inline void Read ()
{
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> Q;
for(int i = 1; i <= N; ++i)
cin >> A[i], B[++B[0]] = A[i];
for(int i = 1; i <= Q; ++i)
{
cin >> Queries[i].first >> Queries[i].second.first;
if(Queries[i].first == 2)
cin >> Queries[i].second.second, B[++B[0]] = Queries[i].second.second;
else
B[++B[0]] = Queries[i].second.first;
}
return;
}
static inline int Find (int Val)
{
return (int)(lower_bound(V + 1, V + V[0] + 1, Val) - V);
}
static inline void Replace ()
{
for(int i = 1; i <= N; ++i)
A[i] = Find(A[i]);
for(int i = 1; i <= Q; ++i)
if(Queries[i].first == 1)
Queries[i].second.first = Find(Queries[i].second.first);
else
Queries[i].second.second = Find(Queries[i].second.second);
return;
}
static inline void Normalize ()
{
sort(B + 1, B + B[0] + 1);
V[++V[0]] = B[1];
for(int i = 2; i <= B[0]; ++i)
if(B[i] != B[i - 1])
V[++V[0]] = B[i];
Replace();
return;
}
class SegmentTree
{
static const int NMAX = (4e5 + 1);
long long A[(NMAX << 2)], Lazy[(NMAX << 2)];
private:
inline void Go (int Node, int a, int b)
{
if(a == b)
return;
if(Lazy[Node] == 0)
return;
int Mid = ((a + b) >> 1);
A[(Node << 1)] += 1LL * Lazy[Node] * (Mid - a + 1), Lazy[(Node << 1)] += 1LL * Lazy[Node];
A[((Node << 1) + 1)] += 1LL * Lazy[Node] * (b - Mid), Lazy[((Node << 1) + 1)] += 1LL * Lazy[Node];
Lazy[Node] = 0;
return;
}
public:
inline void Update (int Node, int a, int b, int ua, int ub, int Val)
{
if(ua <= a && b <= ub)
{
Lazy[Node] += 1LL * Val;
A[Node] += 1LL * Val * (b - a + 1);
return;
}
Go(Node, a, b);
int Mid = ((a + b) >> 1);
if(ua <= Mid)
Update((Node << 1), a, Mid, ua, ub, Val);
if(ub > Mid)
Update(((Node << 1) + 1), Mid + 1, b, ua, ub, Val);
A[Node] = A[(Node << 1)] + A[((Node << 1) + 1)];
return;
}
inline long long Query (int Node, int a, int b, int qa, int qb)
{
if(qa <= a && b <= qb)
return A[Node];
Go(Node, a, b);
int Mid = ((a + b) >> 1);
long long p_Left = 0, p_Right = 0;
if(qa <= Mid)
p_Left = Query((Node << 1), a, Mid, qa, qb);
if(qb > Mid)
p_Right = Query(((Node << 1) + 1), Mid + 1, b, qa, qb);
return (p_Left + p_Right);
}
} AINT;
static inline void Initialize ()
{
for(int i = 1; i <= N; ++i)
if(A[i] > A[i - 1])
AINT.Update(1, 1, V[0], A[i - 1] + 1, A[i], 1);
return;
}
static inline void Solve ()
{
Initialize();
for(int i = 1; i <= Q; ++i)
if(Queries[i].first == 1)
cout << AINT.Query(1, 1, V[0], Queries[i].second.first, Queries[i].second.first) << '\n';
else
{
int pos = Queries[i].second.first, X = Queries[i].second.second;
if(A[pos] > A[pos - 1])
AINT.Update(1, 1, V[0], A[pos - 1] + 1, A[pos], -1);
if(pos < N && A[pos] < A[pos + 1])
AINT.Update(1, 1, V[0], A[pos] + 1, A[pos + 1], -1);
A[pos] = X;
if(A[pos] > A[pos - 1])
AINT.Update(1, 1, V[0], A[pos - 1] + 1, A[pos], 1);
if(pos < N && A[pos] < A[pos + 1])
AINT.Update(1, 1, V[0], A[pos] + 1, A[pos + 1], 1);
}
return;
}
int main()
{
Read();
Normalize();
Solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
512 KB |
Output is correct |
13 |
Correct |
4 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
768 KB |
Output is correct |
4 |
Correct |
19 ms |
2040 KB |
Output is correct |
5 |
Correct |
40 ms |
3576 KB |
Output is correct |
6 |
Correct |
66 ms |
6392 KB |
Output is correct |
7 |
Correct |
109 ms |
11256 KB |
Output is correct |
8 |
Correct |
144 ms |
12152 KB |
Output is correct |
9 |
Correct |
254 ms |
22904 KB |
Output is correct |
10 |
Correct |
235 ms |
21624 KB |
Output is correct |
11 |
Correct |
302 ms |
23672 KB |
Output is correct |
12 |
Correct |
338 ms |
24184 KB |
Output is correct |
13 |
Correct |
342 ms |
24152 KB |
Output is correct |
14 |
Correct |
336 ms |
24056 KB |
Output is correct |
15 |
Correct |
331 ms |
24056 KB |
Output is correct |
16 |
Correct |
359 ms |
24284 KB |
Output is correct |
17 |
Correct |
349 ms |
24184 KB |
Output is correct |
18 |
Correct |
348 ms |
24440 KB |
Output is correct |
19 |
Correct |
341 ms |
24184 KB |
Output is correct |
20 |
Correct |
357 ms |
24440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
512 KB |
Output is correct |
13 |
Correct |
4 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
768 KB |
Output is correct |
18 |
Correct |
3 ms |
768 KB |
Output is correct |
19 |
Correct |
19 ms |
2040 KB |
Output is correct |
20 |
Correct |
40 ms |
3576 KB |
Output is correct |
21 |
Correct |
66 ms |
6392 KB |
Output is correct |
22 |
Correct |
109 ms |
11256 KB |
Output is correct |
23 |
Correct |
144 ms |
12152 KB |
Output is correct |
24 |
Correct |
254 ms |
22904 KB |
Output is correct |
25 |
Correct |
235 ms |
21624 KB |
Output is correct |
26 |
Correct |
302 ms |
23672 KB |
Output is correct |
27 |
Correct |
338 ms |
24184 KB |
Output is correct |
28 |
Correct |
342 ms |
24152 KB |
Output is correct |
29 |
Correct |
336 ms |
24056 KB |
Output is correct |
30 |
Correct |
331 ms |
24056 KB |
Output is correct |
31 |
Correct |
359 ms |
24284 KB |
Output is correct |
32 |
Correct |
349 ms |
24184 KB |
Output is correct |
33 |
Correct |
348 ms |
24440 KB |
Output is correct |
34 |
Correct |
341 ms |
24184 KB |
Output is correct |
35 |
Correct |
357 ms |
24440 KB |
Output is correct |
36 |
Correct |
2 ms |
384 KB |
Output is correct |
37 |
Correct |
3 ms |
512 KB |
Output is correct |
38 |
Correct |
4 ms |
768 KB |
Output is correct |
39 |
Correct |
25 ms |
1920 KB |
Output is correct |
40 |
Correct |
55 ms |
3448 KB |
Output is correct |
41 |
Correct |
100 ms |
6136 KB |
Output is correct |
42 |
Correct |
145 ms |
10872 KB |
Output is correct |
43 |
Correct |
207 ms |
11784 KB |
Output is correct |
44 |
Correct |
421 ms |
22392 KB |
Output is correct |
45 |
Correct |
326 ms |
21368 KB |
Output is correct |
46 |
Correct |
370 ms |
22008 KB |
Output is correct |
47 |
Correct |
506 ms |
23416 KB |
Output is correct |
48 |
Correct |
522 ms |
23544 KB |
Output is correct |
49 |
Correct |
519 ms |
23532 KB |
Output is correct |
50 |
Correct |
503 ms |
23508 KB |
Output is correct |
51 |
Correct |
518 ms |
23672 KB |
Output is correct |
52 |
Correct |
524 ms |
23824 KB |
Output is correct |
53 |
Correct |
527 ms |
28536 KB |
Output is correct |
54 |
Correct |
529 ms |
28536 KB |
Output is correct |
55 |
Correct |
529 ms |
28664 KB |
Output is correct |