CF1764H Doremy's Paint 2 题解
题目链接:CF 或者 洛谷
高分题,感觉挺有意思的题,值得一提的是这个题的 和 版本却是两个基础题。
一开始以为跟这道差不多:P8512 [Ynoi Easy Round 2021] TEST_152 题解。后面重新读了一下发现一个有趣的点:
也就是是说操作的
先关注问什么,全局颜色数,又是这类较为经典的问题。我们重新审题会读到一个有意思的点:
重新读一下题目和样例一手玩下看看,很容易发现一个性质:同种颜色段总是连续的,而且有且只有一段,因为每次修改操作都会使得右边的所有颜色被覆盖为
说到全局颜色数,以及颜色会消失,其实有一道挺经典的题:P9247 [集训队互测 2018] 完美的队列题解。其实根据这题我们基本上就知道咋做了,考虑每个颜色的影响时间,当这个颜色被删去的时候,就会贡献
现在回到这个颜色影响上,考虑这种操作会对颜色消失时间产生什么影响:
首先明确一点,当有一个修改区间为
显然
接下来考虑下怎么求出这个最后的删除时间,显而易见的是,最开始没有操作的删除时间都是最后
最后,考虑操作产生的颜色贡献问题。树状数组和第一个推荐的题一样的用法,记录时刻前缀和,即轴为时间轴。初始的时候,显然有
参照代码
#include <bits/stdc++.h> // #pragma GCC optimize(2) // #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native") // #define isPbdsFile #ifdef isPbdsFile #include <bits/extc++.h> #else #include <ext/pb_ds/priority_queue.hpp> #include <ext/pb_ds/hash_policy.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/trie_policy.hpp> #include <ext/pb_ds/tag_and_trait.hpp> #include <ext/pb_ds/hash_policy.hpp> #include <ext/pb_ds/list_update_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/exception.hpp> #include <ext/rope> #endif using namespace std; using namespace __gnu_cxx; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef tuple<int, int, int> tii; typedef tuple<ll, ll, ll> tll; typedef unsigned int ui; typedef unsigned long long ull; typedef __int128 i128; #define hash1 unordered_map #define hash2 gp_hash_table #define hash3 cc_hash_table #define stdHeap std::priority_queue #define pbdsHeap __gnu_pbds::priority_queue #define sortArr(a, n) sort(a+1,a+n+1) #define all(v) v.begin(),v.end() #define yes cout<<"YES" #define no cout<<"NO" #define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); #define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout); #define forn(i, a, b) for(int i = a; i <= b; i++) #define forv(i, a, b) for(int i=a;i>=b;i--) #define ls(x) (x<<1) #define rs(x) (x<<1|1) #define endl '\n' //用于Miller-Rabin [[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; template <typename T> int disc(T* a, int n) { return unique(a + 1, a + n + 1) - (a + 1); } template <typename T> T lowBit(T x) { return x & -x; } template <typename T> T Rand(T l, T r) { static mt19937 Rand(time(nullptr)); uniform_int_distribution<T> dis(l, r); return dis(Rand); } template <typename T1, typename T2> T1 modt(T1 a, T2 b) { return (a % b + b) % b; } template <typename T1, typename T2, typename T3> T1 qPow(T1 a, T2 b, T3 c) { a %= c; T1 ans = 1; for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c; return modt(ans, c); } template <typename T> void read(T& x) { x = 0; T sign = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-')sign = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } x *= sign; } template <typename T, typename... U> void read(T& x, U&... y) { read(x); read(y...); } template <typename T> void write(T x) { if (typeid(x) == typeid(char))return; if (x < 0)x = -x, putchar('-'); if (x > 9)write(x / 10); putchar(x % 10 ^ 48); } template <typename C, typename T, typename... U> void write(C c, T x, U... y) { write(x), putchar(c); write(c, y...); } template <typename T11, typename T22, typename T33> struct T3 { T11 one; T22 tow; T33 three; bool operator<(const T3 other) const { if (one == other.one) { if (tow == other.tow)return three < other.three; return tow < other.tow; } return one < other.one; } T3() { one = tow = three = 0; } T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three) { } }; template <typename T1, typename T2> void uMax(T1& x, T2 y) { if (x < y)x = y; } template <typename T1, typename T2> void uMin(T1& x, T2 y) { if (x > y)x = y; } constexpr int N = 4e5 + 10; struct ODT { int l, r; mutable int tim; ODT(const int l, const int r, const int tim) : l(l), r(r), tim(tim) { } bool operator<(const ODT& other) const { return l < other.l; } }; set<ODT> node; typedef set<ODT>::iterator iter; int n, m, k; int bit[N]; //轴为时间轴的树状数组 inline void add(int x, const int val) { while (x <= 2 * m)bit[x] += val, x += lowBit(x); } //查询 inline int query(int x) { int ans = 0; while (x)ans += bit[x], x -= lowBit(x); return ans; } int ans[N]; inline iter split(const int pos) { auto it = node.lower_bound(ODT(pos, 0, 0)); if (it != node.end() and it->l == pos)return it; --it; if (it->r < pos)return node.end(); auto [l,r,tim] = *it; node.erase(it), node.insert(ODT(l, pos - 1, tim)); return node.insert(ODT(pos, r, tim)).first; } //覆盖 inline void assign(const int Time, const int l, const int r) { auto itr = split(r + 1), itl = split(l); int last = 0; //这个区间内最后删除的时间 for (auto it = itl; it != itr; ++it) { auto [L,R,tim] = *it; add(tim, -(R - L + 1)); //去掉每种颜色 uMax(last, tim); } node.erase(itl, itr), node.emplace(l, l, last), add(last, 1); //加入a_l位置贡献 if (l != r)node.emplace(l + 1, r, Time), add(Time, r - l); //存在[l+1,r]就如题解覆盖新的最后颜色删除时间 } int L[N], R[N]; //操作 inline void solve() { cin >> n >> m >> k; forn(i, 1, m)cin >> L[i] >> R[i]; forn(i, m+1, 2*m)L[i] = L[i - m], R[i] = R[i - m]; //断环为链,预处理操作 const int INF = 2 * m; //最后操作时间 node.emplace(1, n, INF), add(INF, n); //人为规定,操作结束以后全部颜色删除 forv(i, m+k-1, 1) { assign(i, L[i], R[i]); if (i <= m)ans[i] = query(INF) - query(i + k - 1); //最巧妙的一点,初始的时候每个位置颜色互不相同 } forn(i, 1, m)cout << ans[i] << ' '; } signed int main() { // MyFile Spider //------------------------------------------------------ // clock_t start = clock(); int test = 1; // read(test); // cin >> test; forn(i, 1, test)solve(); // while (cin >> n, n)solve(); // while (cin >> test)solve(); // clock_t end = clock(); // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统