【Luogu3733】【HAOI2017】八纵八横(线段树分治,线性基)

12 篇文章 0 订阅
9 篇文章 0 订阅

Description

https://www.luogu.org/problemnew/show/P3733


Solution

如果只有插入,我们可以搞出一棵生成树,记录每个点到根的异或和 d i s [ u ] dis[u] dis[u],对于边 ( u , v ) (u,v) (u,v),将 d i s [ u ]   x o r   d i s [ v ]   x o r   w dis[u]\ xor\ dis[v]\ xor\ w dis[u] xor dis[v] xor w插入线性基,查询最大异或和即可。
如果有删除、修改操作,线段树分治即可。


Code

/************************************************
 * Au: Hany01
 * Date: Sep 26th, 2018
 * Prob: luogu3733 HAOI2017 八纵八横
 * Email: hany01dxx@gmail.com & hany01@foxmail.com
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define Rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define X first
#define Y second
#define PB(a) push_back(a)
#define MP(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define ALL(a) a.begin(), a.end()
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
	static int _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 505, maxb = 1001, maxq = 1001;

int n, m, q, es, id[maxq], ids;

typedef bitset<maxb> BS;

inline BS getBS() {
	static char str[maxb];
	static int len;
	static BS a; a.reset();
	scanf("%s", str), len = strlen(str);
	Rep(i, len) a[i] = str[len - i - 1] ^ 48;
	return a;
}

inline void printBS(BS a) {
	static int pos; pos = 0;
	Fordown(i, maxb - 1, 0) if (a[i]) { pos = i; break; }
	Fordown(i, pos, 0) putchar(a[i] ^ 48);
	putchar('\n');
}

struct DSU {
	int fa[maxn];
	inline void init() { For(i, 1, n) fa[i] = i; }
	int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
	bool merge(int x, int y) { return ((x = find(x)) ^ (y = find(y))) ? fa[x] = y, 1 : 0; }
}dsu;

struct LinearBasis {
	BS a[maxb];
	inline int insert(BS p) {
		Fordown(i, maxb - 1, 0) if (p[i]) {
			if (!a[i][i]) return a[i] = p, 1;
			else p ^= a[i];
			//else if (!(p ^= a[i]).count()) return 0;
		}
		return 0;
	}
	inline BS getmax() {
		static BS ans; ans.reset();
		Fordown(i, maxb - 1, 0) if (!ans[i]) ans ^= a[i];
		return ans;
	}
}xt[35];

BS dis[maxn], nbs, w[maxn << 1];

struct Edge {
	int st, ed, u, v;
	BS w;
}E[maxn * 3];

int beg[maxn], nex[maxn << 1], v[maxn << 1], e = 1;

inline void add(int x, int y, BS z) { v[++ e] = y, w[e] = z, nex[e] = beg[x], beg[x] = e; }

inline void Input() {
	static int x, y;
	static BS z;
	static char ty[11];
	n = read(), m = read(), q = read(), dsu.init();
	For(i, 1, m) {
		x = read(), y = read(), z = getBS();
		if (dsu.merge(x, y)) add(x, y, z), add(y, x, z);
		E[++ es] = (Edge){0, q, x, y, z};
	}
	For(i, 1, q)
		if (scanf("%s", ty), ty[0] == 'A') x = read(), y = read(), E[id[++ ids] = ++ es] = (Edge){i, q, x, y, getBS()};
		else if (ty[1] == 'a') E[id[read()]].ed = i - 1;
		else
			x = read(), z = getBS(), E[id[x]].ed = i - 1,
			E[++ es] = (Edge){i, q, E[id[x]].u, E[id[x]].v, z}, id[x] = es;
}

void getDis(int u, int pa) {
	for (register int i = beg[u]; i; i = nex[i])
		if (v[i] != pa) dis[v[i]] = dis[u] ^ w[i], getDis(v[i], u);
}

vector<BS> vct[maxq << 2];

#define mid ((l + r) >> 1)
#define lc (t << 1)
#define rc (lc | 1)
void update(int t, int l, int r, int x, int y) {
	if (x <= l && r <= y) { vct[t].PB(nbs); return; }
	if (x <= mid) update(lc, l, mid, x, y);
	if (y >  mid) update(rc, mid + 1, r, x, y);
}

void query(int t, int l, int r, int dep) {
	Rep(i, SZ(vct[t])) xt[dep].insert(vct[t][i]);
	if (l == r) printBS(xt[dep].getmax());
	else
		xt[dep + 1] = xt[dep], query(lc, l, mid, dep + 1),
		xt[dep + 1] = xt[dep], query(rc, mid + 1, r, dep + 1);
}

int main()
{
#ifdef hany01
	freopen("luogu3733.in", "r", stdin);
	freopen("luogu3733.out", "w", stdout);
#endif

	Input(), getDis(1, 0);
	For(i, 1, es)
		nbs = dis[E[i].u] ^ dis[E[i].v] ^ E[i].w, update(1, 0, q, E[i].st, E[i].ed);
	query(1, 0, q, 0);

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值