题目大意
有 个正方形,从左到右排成一行。起初,所有方格都涂成白色。
依次处理 个查询。 查询 给出了一个介于 和 (含)之间的整数 ,并执行以下操作:
从左侧翻转 th 正方形的颜色。具体来说,如果左起第 个方格涂成白色,则涂成黑色;如果涂成黑色,则涂成白色。
然后,求连续涂黑方格的个数。
数据范围:
思路
尝试:如果对于每次查询,都扫一遍所有方格,时间复杂度显然是级别的,总运算能达到,绝对飞了
思考:显然,处理次询问至少需要的时间代价,也就是说,在每一次查询当中,我们只能付出以下的代价,如何?
结论:通过一次查询,使 和 具有不同颜色的索引 的个数要么增加两个,要么减少两个,要么保持不变,这完全取决于 和 在翻转之前是否具有不同颜色
证明:
在某个时刻,假设 是使方格 和 具有不同颜色的索引 的个数。对于所有的,方格的涂色方式如下:
- 对于 ,方格 涂成白色
- 对于 , 放个涂成黑色
- 对于 ,方格 涂成白色
- 对于 ,方格 涂成黑色
- 对于 , 放个涂成白色
因此,有 个连续涂黑的单元格间隔。
注: 恒为偶数,因为方格 和 恒为白
因此,只需寻找到 ,使得方格 和 有不同的颜色。
最初,每个方格都涂成白色,因此对于所有的 而言,方格 和 的颜色是相同的。
而翻转单元格 的颜色后,可能会出现以下两种情况:
- 对于 ,如果单元格 和 原本具有相同的颜色,那么它们就会变得不同;如果它们原本不同,那么它们就会变得相同
- 对于 ,如果 和 单元格原本就是相同的颜色,那么它们就会变得不同;如果它们原本不同,那么它们就会变得相同
证毕!
此时我们做到了!
这个时候我们只需要维护一个数组,表示相邻方格是否异色即可
代码
// 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;
}