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
| #include <iostream> #include <algorithm> #define maxn 200010 #define maxm 200010 #define lowbit(i) ((i) & (-i)) using namespace std;
int n, m, a[maxn];
struct Query { int opt, x, y; } Q[maxm];
int b[maxn * 2], cnt; void init_hash() { for (int i = 1; i <= n; ++i) b[i] = a[i]; for (int i = 1; i <= m; ++i) if (Q[i].opt == 1) b[i + n] = Q[i].x; else b[i + n] = Q[i].y; sort(b + 1, b + n + m + 1); cnt = unique(b + 1, b + n + m + 1) - b - 1; for (int i = 1; i <= n; ++i) a[i] = lower_bound(b, b + cnt + 1, a[i]) - b; for (int i = 1; i <= m; ++i) if (Q[i].opt == 1) Q[i].x = lower_bound(b + 1, b + cnt + 1, Q[i].x) - b; else Q[i].y = lower_bound(b + 1, b + cnt + 1, Q[i].y) - b; }
int Bit[maxn * 2][2]; void add(int i, int v, int o) { while (i <= cnt) Bit[i][o] += v, i += lowbit(i); }
void update(int l, int r, int v, int o) { add(l, v, o); add(r + 1, -v, o); }
int get_sum(int i, int o) { int s = 0; while (i) s += Bit[i][o], i -= lowbit(i); return s; }
inline void update(int i, int v) { if (i > 1) update(1, min(a[i - 1], a[i]), -1, 0); if (i < n) update(1, min(a[i], a[i + 1]), -1, 0); add(a[i], -1, 1); a[i] = v; if (i > 1) update(1, min(a[i - 1], a[i]), 1, 0); if (i < n) update(1, min(a[i], a[i + 1]), 1, 0); add(a[i], 1, 1); }
inline void solve_1(int x) { cout << n - get_sum(x - 1, 1) - get_sum(x, 0) << "\n"; }
inline void solve_2(int x, int y) { update(x, y); }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= m; ++i) { cin >> Q[i].opt; if (Q[i].opt == 1) cin >> Q[i].x; else cin >> Q[i].x >> Q[i].y; } init_hash(); for (int i = 1; i <= n; ++i) { add(a[i], 1, 1); if (i > 1) update(1, min(a[i - 1], a[i]), 1, 0); } for (int i = 1; i <= m; ++i) if (Q[i].opt == 1) solve_1(Q[i].x); else solve_2(Q[i].x, Q[i].y); return 0; }
|