CF1980F2 - Field Division (hard version)
首先,阅读这篇简单版的题解。
困难版的题目就是第二问将判断0/1改成了判断增加多少。
可以想到进行前缀预处理。
喷泉只有在被移除时才会不再是角,因为在移除操作后,前缀的最左侧角不可能变得更左。
为了计算面积的变化,我们需要做一个重要的观察:如果喷泉 不是角,那么它要么无法成为角,要么只有在移除排序顺序中位于它之前的最后一个角之后才会成为角。这个被移除的角是排序顺序中位于喷泉 之前的最左侧角,而排序顺序中的下一个角会严格更高。
因此,我们需要做以下操作:对于每个角,计算不考虑它直到下一个角的面积,这可以通过我们在计算面积时所做的预计算来帮助实现。
每个喷泉只会被处理一次,因此时间复杂度为 。
代码在简单版的基础上改改就出来了。
代码
cf记录:Ciallo~(∠・ω< )⌒★。
// Ciallo~(∠・ω< )⌒★
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
#define be begin
#define ed end
#define fi first
#define se second
#define ls(p) (p << 1)
#define rs(p) (p << 1 | 1)
#define file(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
#ifdef LOCAL
void debug_out() { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cout << H << " ";
debug_out(T...);
}
#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-9;
const int N = 1e5 + 6;
inline void solve(){
int n, m, k;
cin >> n >> m >> k;
vector<pii> a(k);
map<pii, int> cia;
for (int i = 0; i < k; i++){
cin >> a[i].fi >> a[i].se;
cia[a[i]] = i;
}
cia[{0, 0}] = k++;
a.emplace_back(0, 0);
sort(a.be(), a.ed(), [&](pii a, pii b){
if (a.fi != b.fi) return a.fi > b.fi;
return a.se < b.se;
});
vi res(k);
vi tot(k + 1), cur(k + 1, m + 1), las(k + 1, n);
for (int i = 1; i <= k; i++){
pii x = a[i - 1];
tot[i] = tot[i - 1];
cur[i] = cur[i - 1];
las[i] = las[i - 1];
if (cur [i] > x.se){
res[cia[x]] = 1;
tot[i] += (cur[i] - 1) * (las[i] - x.fi);
cur[i] = x.se;
las[i] = x.fi;
}
}
cout << tot[k] << "\n";
for (int i = 1; i <= k; i++){
pii x = a[i - 1];
if (res[cia[x]] == 0)
continue;
int tt = tot[i - 1];
int cr = cur[i - 1];
int ls = las[i - 1];
for (int j = i + 1; j <= k; j++){
pii xx = a[j - 1];
if (cr > xx.se){
tt += (cr - 1) * (ls - xx.fi);
cr = xx.se;
ls = xx.fi;
}
if (res[cia[xx]] == 1){
res[cia[x]] = tt - tot[j];
break;
}
}
}
res.pop_back();
for (int x : res) cout << x << ' ';
cout << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T; cin >> T;
while (T--) solve();
return 0;
}