首页 > 其他分享 >CF1556G Gates to Another World

CF1556G Gates to Another World

时间:2023-08-06 20:28:01浏览次数:48  
标签:Gates lc int ll tim Another rc CF1556G find

*3300

这种 \(2 ^ n\) 和区间,看着就很想套上线段树,事实上是对的。

引理 1 :

在线段数内同一颗子树内的点可以互相到达。

这个是非常容易验证的,把边画出来就是在一条链上挂若干条横着的链。

然后我们考虑把区间挂上去,然后用时光倒流转化为加边。我们发现,我们可以用叶子节点来代表若干区间,然后在上面打上时间戳来看这个时候是否存在。

考虑到不同时间不同子树的联通性,我们要使用线段树合并暴力连边,然后递归到叶子节点的时候可以使用并查集来维护连通性。

整个时间复杂度有点复杂,反正能过。

// LUOGU_RID: 119091043
#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = l; i <= r; i ++)
#define per(i, r, l) for (int i = r; i >= l; i --)
#define pb push_back
#define ll long long
using namespace std;

const int _ = 5e4 + 5, __ = _ * 100;

int n, m, qn;
ll lim;
bool ans[_];
int tot = 1, lc[__], rc[__], tim[__], anc[__];
struct query { ll a, b; int id; } q[_];

vector <int> ele[_], e[__];

int find (int x) { 
	return x == anc[x] ? x : anc[x] = find(anc[x]);
}
bool leaf (int x) { return !lc[x] && !rc[x]; }
void pushdown (int x) {
	if (!lc[x]) lc[x] = ++ tot;
	if (!rc[x]) rc[x] = ++ tot;
	if (tim[x]) tim[lc[x]] = tim[rc[x]] = tim[x], tim[x] = 0;
}
int findnode (int x, ll l, ll r, ll p) {
	if (leaf(x)) return x;
	ll mid = (l + r) >> 1ll;
	if (p <= mid) return findnode(lc[x], l, mid, p);
	else return findnode(rc[x], mid + 1, r, p);
}
void cover (int x, ll l, ll r, ll ql, ll qr, int k) {
	if (ql <= l && r <= qr) return tim[x] = k, void();
	ll mid = (l + r) >> 1ll; pushdown(x);
	if (ql <= mid) cover(lc[x], l, mid, ql, qr, k);
	if (qr > mid) cover(rc[x], mid + 1, r, ql, qr, k);
}
void assign (int x, ll l, ll r) {
	if (leaf(x)) return ele[tim[x]].pb(x), void();
	ll mid = (l + r) >> 1ll; pushdown(x); 
	assign(lc[x], l, mid), assign(rc[x], mid + 1, r);
}
void merge (int x, int y) {
	if (leaf(x) && leaf(y)) return ((tim[x] < tim[y]) ? e[x].pb(y) : e[y].pb(x)), void();
	if (leaf(x)) return merge(x, lc[y]), merge(x, rc[y]), void();
	if (leaf(y)) return merge(y, lc[x]), merge(y, rc[x]), void();
	return merge(lc[x], lc[y]), merge(rc[x], rc[y]), void();
}

signed main () {
	cin >> n >> m, lim = (1ll << n) - 1;
	rep(i, 1, m) {
		string x;
		cin >> x;
		scanf("%lld %lld", & q[i].a, & q[i].b);
		if (x[0] == 'a') q[i].id = ++ qn;
	}
	tim[1] = m + 1;
	per(i, m, 1) if (!q[i].id) cover(1, 0, lim, q[i].a, q[i].b, i);
	assign(1, 0, lim);
	rep(x, 1, tot) if (! leaf(x)) merge(lc[x], rc[x]);
	rep(i, 1, tot) anc[i] = i;
	for (int x : ele[m + 1]) 
		for (int y : e[x]) if (find(x) != find(y)) anc[find(x)] = find(y);
	per(i, m, 1) {
		if (! q[i].id) {
			for (int x : ele[i]) 
				for (int y : e[x]) if (find(x) != find(y)) anc[find(x)] = find(y);
		} else {
			int x = findnode(1, 0, lim, q[i].a), y = findnode(1, 0, lim, q[i].b);
			if (find(x) == find(y)) ans[q[i].id] = 1;
	 	}
	}
	rep(i, 1, qn) printf("%lld\n", ans[i]);
	return 0;
}

标签:Gates,lc,int,ll,tim,Another,rc,CF1556G,find
From: https://www.cnblogs.com/Custlo/p/17609908.html

相关文章

  • Atcoder ABC259H Yet Another Path Counting
    首先可以想到有组合数的方法:令起点为\((x1,y1)\),终点为\((x2,y2)\),则路径方案数就为\(\binom{x2+y2-x1-y1}{x2-x1}\),这样设有\(k\)个相同颜色的点,时间复杂度就为\(O(k^2)\)。再考虑到还有\(\text{DP}\)方法:令\(f_{x,y}\)为走到\((x,y)\)的方案数,不限制......
  • Do you already have another mysqld server running on port: 8008 ?
    实现"Doyoualreadyhaveanothermysqldserverrunningonport:8008?"的步骤概述在解决问题之前,我们先了解一下整个问题的流程。下面是解决问题的步骤:步骤操作1检查是否已经有一个mysqld服务运行在8008端口2如果有,关闭该服务3如果没有,继续其他操作......
  • YetAnotherGridTask
    [ABC311F]YetAnotherGridTask考虑找规律。我们先将必定要填黑的格子填完。对于以下的矩形....#........#................处理后....#.....##.#..##.##.##.#####一个必要的观察是:对于从右上角到左下角的次对角线,对角线上必定存在一个分界点,使得其左边全为白,......
  • java中tomcat 加载动态库XXX.dll报错“java.lang.UnsatisfiedLinkError: already load
    错误:在Tomcat项目和supermapiserverwar包中使用了相同的supermapjavaiobject【四个jar包】,实际的访问过程如下:这时候在访问Tomcat的时候,就会出现一个错误:anexceptioncaughtatEnvironment.loadLibrary(),programwillcontinuerunning.java.lang.UnsatisfiedL......
  • 题解 Yet Another Minimization Problem
    YetAnotherMinimizationProblem神仙题。第一眼看上去就是DP。定义\(f_{i,j}\)表示当前点\(i\),分\(j\)段的最小费用。\(f_{i,j}=\min(f_{i,j},f_{k,j-1}+val_{k+1,i})\)然后发现复杂度\(O(n^2k)\),直接T飞,需要优化。我们发现\(j\)那一维可以滚掉,也就是只考虑第......
  • AtCoder Grand Contest 058 D Yet Another ABC String
    洛谷传送门AtCoder传送门OrzH6_6Q,感谢他给我们带来了这道容斥好题。这个东西看起来很不好搞。可以尝试容斥。但是我们要容斥啥?钦定ABC不出现,其他任意?感觉还是很难算。观察不合法子串,发现它们很有特点。如果我们钦定\(\texttt{A}\)为\(0\),\(\texttt{B}\)为\(1\),\(\te......
  • mongodb Aggregates Accumulators使用初步
    importcom.mongodb.BasicDBObject;importcom.mongodb.client.MongoCollection;importcom.mongodb.client.MongoCursor;importcom.mongodb.client.model.Accumulators;importcom.mongodb.client.model.Aggregates;importcom.mongodb.client.model.Filters;importorg.bso......
  • VMware:Package vim is not available, but is referred to by another package.
    出错语句在ubuntu中输入sudoapt-getinstallvim安装vim时出现如下错误语句Readingpackagelists...DoneBuildingdependencytreeReadingstateinformation...DonePackagevimisnotavailable,butisreferredtobyanotherpackage.Thismaymeanthatt......
  • YetAnotherRGBSequence
    [ABC266G]YetAnotherRGBSequence为了方便将\(r,g,b\)替换为\(a,b,c\)。考虑可以将\(a-=k,b-=k\),就变为\(a-k\)个\(a\),\(b-k\)个\(b\),\(c\)个\(c\),\(k\)个\(ab\),(这里我们已经将\(a,b\)减去\(k\),下文的\(a,b\)均指代减去后的结果)然后求排列总数,使得不构成新......
  • Yet Another Minimization Problem(CF1637D)
    \(\text{Des}\)Youaregiventwoarrays$a$and$b$,bothoflength$n$.Youcanperformthefollowingoperationanynumberoftimes(possiblyzero):selectanindex$i$($1\leqi\leqn$)andswap$a_i$and$b_i$.Let'sdefi......