F - Make them monochrome

F - Make them monochrome

Problem Statement

There is an integer sequence $A=(A_1,A_2,\dots,A_N)$ of length $N$.
Process the following $Q$ queries.

Type $1$ :

1 $i$ $x$

Change $A_i$ to $x$.

Type $2$ :

2 $l$ $r$

Suppose that each $-1$ contained in $A_l,A_{l+1},\dots,A_r$ can be replaced with an arbitrary positive integer. Print Yes if $A_l,A_{l+1},\dots,A_r$ can all be made equal, and No otherwise.

Constraints

  • All input values are integers.
  • $1 \le N,Q \le 3 \times 10^5$
  • $-1 \le A_i \le 10^6$
  • $A_i \neq 0$
  • Queries of type $1$ satisfy the following constraints:
    • $1 \le i \le N$
    • $-1 \le x \le 10^6$
    • $x \neq 0$
  • Queries of type $2$ satisfy the following constraints:
    • $1 \le l \le r \le N$

Input

The input is given from Standard Input in the following format, where ${\rm Query}_i$ denotes the $i$-th query:

$N$ $Q$
$A_1$ $A_2$ $\dots$ $A_N$
${\rm Query}_1$
${\rm Query}_2$
$\vdots$
${\rm Query}_Q$

Output

Each time a query of type $2$ is given, print the answer to that query in one line.


Sample Input 1

8 9
5 -1 3 -1 2 3 4 5
2 3 6
1 5 -1
2 3 6
1 7 5
2 7 8
2 1 8
1 3 5
1 6 -1
2 1 8

Sample Output 1

No
Yes
Yes
No
Yes

This input contains $9$ queries.
Below, we explain the first $3$ queries.

  • Initially, $A=(5,-1,3,-1,2,3,4,5)$.
  • The $1$-st query is of type $2$, and the subarray in question is $(3,-1,2,3)$. These cannot all be made equal by the operation in the problem statement, so print No.
  • The $2$-nd query is of type $1$, and changes $A_5$ to $-1$. As a result, $A=(5,-1,3,-1,-1,3,4,5)$.
  • The $3$-rd query is of type $2$, and the subarray in question is $(3,-1,-1,3)$. These can all be made equal by the operation in the problem statement, so print Yes.

 

解题思路

  先通过分类讨论来确定某段区间能否全部变成同一个正整数。我们把区间内不是 $-1$ 的数称为确定的值。替换只能作用于 $-1$,所以确定的值始终保持不变。如果区间内至少有 $2$ 种确定的值,那么无论怎样替换 $-1$,区间中最终都至少存在 $2$ 种不同的正整数。如果区间内恰好有 $1$ 种确定的值,只需把所有的 $-1$ 都替换成这个值,整段区间就会变成同一个正整数(若区间内本来就没有 $-1$ 则它已经全部相等)。如果区间内全部都是 $-1$,则统一替换成同一个正整数即可。综上,当且仅当区间内至多有 $1$ 种确定的值时有解。

  然后是如何快速判断某段区间内有多少种确定的值。实际上我们并不关心具体的种数,只需判断它是否不超过 $1$。如果区间内恰好有 $1$ 种确定的值,那么这些值的最小值必然等于最大值。而只要有 $2$ 种及以上,最小值就一定严格小于最大值。据此,我们可以用线段树动态维护区间内所有确定的值的最小值与最大值。这样只要查询得到的最小值等于最大值,就说明区间内恰好有 $1$ 种确定的值,一定可以通过替换使整段区间变成同一个正整数。

  还有整个区间都是 $-1$ 的边界情况需要处理,此时区间内没有任何确定的值,我们约定每个 $-1$ 对应的最小值为 $+\infty$,最大值为 $-\infty$。于是,如果查询得到的最小值大于最大值,就意味着整个区间都是 $-1$,同样可以通过替换变成同一个正整数。综上,只要查询得到的最小值不小于最大值就有解。

  AC 代码如下,时间复杂度为 $O(n \log{n})$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 3e5 + 5, INF = 0x3f3f3f3f;

int a[N];
struct Node {
    int l, r, mn, mx;
}tr[N * 4];

Node pushup(Node l, Node r) {
    return {l.l, r.r, min(l.mn, r.mn), max(l.mx, r.mx)};
}

void build(int u, int l, int r) {
    tr[u] = {l, r};
    if (l == r) {
        if (a[l] == -1) tr[u].mn = INF, tr[u].mx = -INF;
        else tr[u].mn = tr[u].mx = a[l];
    }
    else {
        int mid = l + r >> 1;
        build(u << 1, l, mid);
        build(u << 1 | 1, mid + 1, r);
        tr[u] = pushup(tr[u << 1], tr[u << 1 | 1]);
    }
}

void modify(int u, int x, int c) {
    if (tr[u].l == tr[u].r) {
        if (c == -1) tr[u].mn = INF, tr[u].mx = -INF;
        else tr[u].mn = tr[u].mx = c;
    }
    else {
        if (x <= tr[u].l + tr[u].r >> 1) modify(u << 1, x, c);
        else modify(u << 1 | 1, x, c);
        tr[u] = pushup(tr[u << 1], tr[u << 1 | 1]);
    }
}

Node query(int u, int l, int r) {
    if (tr[u].l >= l && tr[u].r <= r) return tr[u];
    int mid = tr[u].l + tr[u].r >> 1;
    if (r <= mid) return query(u << 1, l, r);
    if (l >= mid + 1) return query(u << 1 | 1, l, r);
    return pushup(query(u << 1, l, r), query(u << 1 | 1, l, r));
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    build(1, 1, n);
    while (m--) {
        int op, l, r;
        cin >> op >> l >> r;
        if (op == 1) {
            modify(1, l, r);
        }
        else {
            Node t = query(1, l, r);
            cout << (t.mn >= t.mx ? "Yes" : "No") << '\n';
        }
    }
    
    return 0;
}

 

日记

  强行水了篇 11 月份的博客 ( ´・ω・`),我真的好想再像以前一样在算竞中投入大量精力并享受,可是完全没有时间 (´;ω;`)。今年就这样过去了,唉。

 

参考资料

  Editorial - AtCoder Beginner Contest 484:https://atcoder.jp/contests/abc484/editorial/29006

posted @ 2026-11-22 17:45  onlyblues  阅读(4)  评论(0)    收藏  举报
Web Analytics