题解:AT_abc411_c [ABC411C] Black Intervals

· · 题解

题目大意

有 N 个正方形,从左到右排成一行。起初,所有方格都涂成白色。

依次处理 Q 个查询。 查询i 给出了一个介于 1 和 N (含)之间的整数 A_i ,并执行以下操作:

从左侧翻转 A_i th 正方形的颜色。具体来说,如果左起第 A_i 个方格涂成白色,则涂成黑色;如果涂成黑色,则涂成白色。
然后,求连续涂黑方格的个数。

数据范围:

思路

尝试:如果对于每次查询,都扫一遍所有方格,时间复杂度显然是\Theta(QN)级别的,总运算能达到10^{10},绝对T飞了

思考:显然,处理Q次询问至少需要\Theta{(Q)}的时间代价,也就是说,在每一次查询当中,我们只能付出\Theta{(log N)}以下的代价,如何?

结论:通过一次查询,使 i 和 (i+1) 具有不同颜色的索引 i 的个数要么增加两个,要么减少两个,要么保持不变,这完全取决于 i 和 (i+1) 在翻转之前是否具有不同颜色

证明:

在某个时刻,假设 k 是使方格 i 和 (i+1) 具有不同颜色的索引 i 的个数。对于所有的i,方格的涂色方式如下:

因此,有 \frac{k}{2} 个连续涂黑的单元格间隔。

注:k 恒为偶数,因为方格 0 和 (N+1) 恒为白

因此,只需寻找到 i ,使得方格 i 和 (i+1) 有不同的颜色。

最初,每个方格都涂成白色,因此对于所有的 0\leq i\leq N 而言,方格 i 和 (i+1) 的颜色是相同的。

而翻转单元格 X 的颜色后,可能会出现以下两种情况:

证毕!

此时我们做到了\Theta(1)!

这个时候我们只需要维护一个数组,表示相邻方格是否异色即可

代码

// Problem: C - sq Intervals
// Contest: AtCoder - UNIQUE VISION Programming Contest 2025 Summer (AtCoder Beginner Contest 411)
// URL: https://atcoder.jp/contests/abc411/tasks/abc411_c
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)

// AC!
#include <bits/stdc++.h>
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using real = long double;

#define FOR(i, a, b) for (i64 i = (i64)(a); i < (i64)(b); ++i)
#define FORD(i, a, b) for (i64 i = (i64)(a); i <= (i64)(b); ++i)
#define rep(i, a, b) for (i64 i = (i64)(a); i > (i64)(b); i--)
#define repd(i, a, b) for (i64 i = (i64)(a); i >= (i64)(b); i--)
#define FastIO std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0);
#define endl "\n"
#define De std::cout << "De" << endl
#define de(x) std::cout << x << endl
#define pll std::pair<i64, i64>
#define pii std::pair<int, int>
#define pdd std::pair<double, double>
#define ld long double
#define pb(x) push_back((x))
#define ALL(a) (((a).begin()), ((a).end()))
#define mp(a, b) make_pair((a), (b))
#define sqrt(x) sqrtl((x))

bool __stb;
int __stt = clock();
int tests = 1;

void solve() {
    int N, Q;
    std::cin >> N >> Q;
    std::vector<bool> sq(N + 2, false);
    //维护相邻状态的数组
    sq[0] = sq[N + 1] = false;
    int cnt = 0;
    while (Q--) {
        int a;
        std::cin >> a;
        bool L = sq[a - 1];
        bool R = sq[a + 1];
        if (!sq[a]) {
            if (!L and !R) {
                ++cnt;
            } else if (L and R) {
                --cnt;
            }
            sq[a] = true;
        } else {
            if (!L and !R) {
                --cnt;
            } else if (L and R) {
                ++cnt;
            }
            sq[a] = false;
        }
        std::cout << cnt << endl;
    }
    return;
}
bool __enb;

int main() {
    FastIO;
#ifdef LOCAL
    std::cerr << "Memory: " << (&__stb - &__enb) / 1024.0 / 1024.0 << " MB" << endl;
//  freopen("input.in", "r", stdin);
//  cin >> tests;
//  freopen("output.out","w",stdout);
#endif
    while (tests--) {
        solve();
    }
#ifdef LOCAL
    int __edt = clock();
    std::cerr << "Time: " << (__edt - __stt) << "ms" << endl;
#endif
    return 0;
}