Joint query set maintenance interval handover and merging

Time:2021-12-30

Any two numbers are different and the length is\(n\)Sequence of\(a\), yes\(q\)A message, such as\(a_l, a_{l+1},\cdots,a_r\)The minimum value of is\(r\) .

Is there a contradiction?

Contradictions fall into two categories:

  1. If the minimum values of the two intervals are the same, but the two intervals do not intersect (each number is different)
  2. If the union of several larger intervals contains the intersection of some smaller intervals

The latter is equivalent to a data structure that can be maintained

  1. Merge interval
  2. Interval inclusion relation

Merge intervals can be violent and search sets to put each\([l,r]\)All the fathers in the chain\(r\)In fact, the complexity is quite low (and the magic of the collection is shared qwq)

The code is about this

using namespace std;

const int N = 1e6+500, Q = 25555, I = 0x3f3f3f3f;

inline void chkmin(int& a, int b){if (a > b) a = b;}
inline void chkmax(int& a, int b){if (a < b) a = b;}

struct Input
{
	int l, r, v;
	bool operator < (const Input& x)const{return v > x.v;}
}inp[Q], t[Q];
int n, q;

struct Magic
{
	int fa[N]; // dsu
	void init(){for (int i=0; i<=n+3; i++) fa[i] = i;}
	void clear(){init();}
	int get(int x){return fa[x] == x ? x : fa[x] = get(fa[x]);}
	void merge(int l, int r)
	{
		for (int u = l; u <= r; u++)
			fa[get(u)] = get(r+1); // dsu union
	}
	bool crs(int l, int r){return get(l) > r;}
	Magic(){init();}
}T;

bool check(int r) // -> is NOT true
{
	T.clear();
	for (int i=1; i<=r; i++) t[i] = inp[i];
	sort(t+1, t+1+r);
	int lmin, lmax, rmin, rmax;
	lmin = lmax = t[1].l; rmin = rmax = t[1].r;
	for (int i=2; i<=r; i++)
	{
		if (t[i].v == t[i-1].v) // Case 1
		{
			lmin = min(lmin, t[i].l); lmax = max(lmax, t[i].l);
			rmin = min(rmin, t[i].r); rmax = max(rmax, t[i].r);
			if (rmin < lmax) return true;
			continue;
		}                       // Case 2
		if (T.crs(lmax, rmin)) return true;
		T.merge(lmin, rmax);
		lmin = lmax = t[i].l; rmin = rmax = t[i].r;
	}
	return T.crs(lmax, rmin);
}