题意:区间长度为n,m个查询。每次查询给出区间与一个数值0或者1,代表区间内的1的个数。找出不矛盾的最后一个询问。
思路:首先用到区间压缩,排序后去重即可。使用带权dsu,如果是同一个root,那么xor运算看是否符合输入。如果不是同一个root,直接合并。这里合并区间的时候权重更新有点抽象,xx合并到yy,使用的是x->xx的权重和y->yy的权重以及设定的op。相较于路径权重是长度时,稍微难理解一点。
class DisjointSet{
public:
DisjointSet(int sz) : sz_(sz){
fa_.resize(sz_);
iota(fa_.begin(), fa_.end(), 0);
dist_.resize(sz_);
}
int findSet(int x){
updateDist(x);
return fa_[x];
}
bool unionSet(int x, int y, int value){
int xx = findSet(x);
int yy = findSet(y);
if (xx == yy){ return false; }
fa_[xx] = yy;
dist_[xx] = dist_[x] ^ dist_[y] ^ value;
return true;
}
int getDist(int x){ updateDist(x); return dist_[x]; }
private:
vector<int> fa_;
vector<int> dist_;
int sz_;
inline void updateDist(int x){
if (fa_[x] == x) { return; }
int parent = fa_[x];
fa_[x] = findSet(parent);
dist_[x] ^= dist_[parent];
return;
}
};
void solve(){
int n, m;
cin >> n >> m;
set<int> sett;
vector<pair<int, int>> a(m);
vector<int> op(m);
vector<int> b(2 * m, 0);
for (int i = 0; i < m; ++i){
cin >> a[i].first >> a[i].second;
string s;
cin >> s;
op[i] = static_cast<int>(s[0] == 'o');
b[i * 2] = a[i].first;
b[i * 2 + 1] = a[i].second;
}
sort(b.begin(), b.end());
b.erase(unique(b.begin(), b.end()), b.end());
DisjointSet dsu(b.size() + 1);
int index = 0;
for (int i = 0; i < m; ++i){
const auto& [x, y] = a[i];
int l = lower_bound(b.begin(), b.end(), x - 1) - b.begin();
int r = lower_bound(b.begin(), b.end(), y) - b.begin();
if (dsu.unionSet(l, r, op[i])){
continue;
}
else if ((dsu.getDist(l) ^ dsu.getDist(r)) != op[i]){
cout << i << '\n';
return;
}
}
cout << m << endl;
}
标签:P5937,begin,dist,int,CEOI1999,xx,fa,Game,return
From: https://www.cnblogs.com/yxcblogs/p/18098367