Skip to content

Instantly share code, notes, and snippets.

@alculquicondor
Created October 26, 2012 22:43
Show Gist options
  • Save alculquicondor/3961992 to your computer and use it in GitHub Desktop.
Save alculquicondor/3961992 to your computer and use it in GitHub Desktop.
Union Range
int C[MAXN], next[MAXN];
inline int findSet(int i) {
return (C[i] == i) ? i : (C[i] = findSet(C[i]));
}
inline int findNext(int i) {
return (next[i] == i) ? i : (next[i] = findNext(next[i]));
}
inline void unionSet(int i, int j) {
C[findSet(i)] = findSet(j);
}
inline void unionRange(int i, int j) {
if(j<i)
return;
int g = findNext(i), last = findNext(j);
next[i] = last;
while(g<j) {
next[g] = last;
unionSet(g, g+1);
g = findNext(g+1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment