版本比较
题解:P4737 [CERC2017] Buffalo Barricades
正文
增加 14 行删除 14 行
90 行未改动919293949595969798991001011021031041051041051061071081091091101111121137 行未改动1211221231241251261271281251261271281291301311321331311321331341351361361371381391391401411421431441441451461471485 行未改动90 行未改动#define fs first#define sc second#define mp make_pair#define pii pair <int, int>using namespace std; using namespace std;const int N = 1e6+5;int n, m, fa[N], outer[N], sum[N], ans[N];set <pii> s;int find(int x) { return fa[x] = fa[x] == x? x : find(fa[x]); }void unite(int u, int v){ int fu = find(u), fv = find(v); if (fu != fv){ fa[fv] = fu; sum[fu] += sum[fv]; // 记得将贡献加上 } sum[fu] += sum[fv]; // 记得将贡献加上 }}struct node{ int x, y, t;} a[2*N]; // 记得开两倍,因为贡献点和检查点都在这里面 } a[2*N]; // 记得开两倍,因为贡献点和检查点都在这里面bool cmp(node x, node y){ if (x.y != y.y) return x.y > y.y; if (x.x != y.x) return x.x > y.x; return x.t > y.t;7 行未改动 sort(a+1, a+n+m+1, cmp); for (int i = 1, x, t; i <= n+m; i++){ x = a[i].x, t = a[i].t; pii pr(x, t); if (!t){ // 贡献点 auto it = s.lower_bound(pr); // upper也行 if (it != s.end()) sum[it->sc]++; // 贡献加1 } else { // 检查点 if (!t){ // 贡献点 auto it = s.lower_bound(pr); // upper也行 if (it != s.end()) sum[it->sc]++; // 贡献加1 } else { // 检查点 s.insert(pr); auto it = s.find(pr); while (it != s.begin() && (--it)->sc > t){ // 不是第一个 且关系成立 s.erase(it); // 删掉再找一个新的 it = s.find(pr); // 注意这里要重新找,因为你把it删掉后再++或--是错的 while (it != s.begin() && (--it)->sc > t){ // 不是第一个 且关系成立 s.erase(it); // 删掉再找一个新的 it = s.find(pr); // 注意这里要重新找,因为你把it删掉后再++或--是错的 } it = ++s.find(pr); if (it != s.end()) outer[t] = it->sc; // 处理包含关系 if (it != s.end()) outer[t] = it->sc; // 处理包含关系 } } for (int i = m; i >= 1; i--){ // 细节倒序枚举 for (int i = m; i >= 1; i--){ // 细节倒序枚举 ans[i] = sum[find(i)]; if (outer[i]) unite(i, outer[i]); } for (int i = 1; i <= m; i++) cout << ans[i] << '\n';
return 0;}```
5 行未改动