版本比较
题解:P4737 [CERC2017] Buffalo Barricades
正文
增加 14 行删除 14 行
90 行未改动9192939495969798991001011021031041051061071081091101111121137 行未改动1211221231241251261271281291301311321331341351361371381391401411421431441451461471485 行未改动90 行未改动#define fs first#define sc second#define mp make_pair#define pii pair <int, int>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]; // 记得将贡献加上}}struct node{int x, y, t;} 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 { // 检查点s.insert(pr);auto it = s.find(pr);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; // 处理包含关系}}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 行未改动90 行未改动9192939495969798991001011021031041051061071081091101111121137 行未改动1211221231241251261271281291301311321331341351361371381391401411421431441451461471485 行未改动90 行未改动#define fs first#define sc second#define mp make_pair#define pii pair <int, int>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]; // 记得将贡献加上}}struct node{int x, y, t;} 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 { // 检查点s.insert(pr);auto it = s.find(pr);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; // 处理包含关系}}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 行未改动