【校内训练2019-04-10】神犇

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39972971/article/details/89180689

【思路要点】

  • a i = c n t x i c n t y i , b i = c n t y i c n t z i , c i = c n t z i c n t x i a_i=cntx_i-cnty_i,b_i=cnty_i-cntz_i,c_i=cntz_i-cntx_i s i s_i 表示 i i 处的前缀异或和。
  • 则答案 A n s i Ans_i M a x j < i , a i a j , b i b j , c i c j { s i s j } Max_{j<i,a_i\ne a_j,b_i\ne b_j,c_i\ne c_j}\{s_i\oplus s_j\}
  • 注意到 a , b , c a,b,c 三对数中若有两对相等,那么另一对也相等。
  • 考虑容斥,令 a l l all 表示所有 j j 的字典树, x x 表示所有 a i = a j a_i=a_j j j 的字典树, y y 表示所有 b i = b j b_i=b_j j j 的字典树, z z 表示所有 c i = c j c_i=c_j j j 的字典树, s p e spe 表示所有 a i = a j , b i = b j , c i = c j a_i=a_j,b_i=b_j,c_i=c_j j j 的字典树,在 a l l x y z + 2 e p s all-x-y-z+2eps 上询问即可。
  • 时间复杂度 O ( N L o g V ) O(NLogV)

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 6e5 + 5;
const int MAXP = 4e7 + 5;
typedef long long ll;
typedef long double ld;
typedef int long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
struct SegmentTree {
	struct Node {
		int lc, rc;
		int sum;
	} a[MAXP];
	int size, depth;
	void init(int x) {
		size = 0;
		depth = x;
	}
	int modify(int root, int pos, int d, int depth) {
		if (root == 0) root = ++size;
		a[root].sum += d;
		if (depth == -1) return root;
		int tmp = 1 << depth;
		if (tmp & pos) a[root].rc = modify(a[root].rc, pos, d, depth - 1);
		else a[root].lc = modify(a[root].lc, pos, d, depth - 1);
		return root;
	}
	int modify(int root, int pos, int d) {
		return modify(root, pos, d, depth);
	}
	int query(int ra, int rb, int rc, int rd, int re, int val, int depth) {
		if (depth == -1) return 0;
		int tmp = 1 << depth;
		if (val & tmp) {
			if (a[a[ra].lc].sum + a[a[rb].lc].sum + a[a[rc].lc].sum + a[a[rd].lc].sum + a[a[re].lc].sum) return query(a[ra].lc, a[rb].lc, a[rc].lc, a[rd].lc, a[re].lc, val, depth - 1) + tmp;
			else return query(a[ra].rc, a[rb].rc, a[rc].rc, a[rd].rc, a[re].rc, val, depth - 1);
		} else {
			if (a[a[ra].rc].sum + a[a[rb].rc].sum + a[a[rc].rc].sum + a[a[rd].rc].sum + a[a[re].rc].sum) return query(a[ra].rc, a[rb].rc, a[rc].rc, a[rd].rc, a[re].rc, val, depth - 1) + tmp;
			else return query(a[ra].lc, a[rb].lc, a[rc].lc, a[rd].lc, a[re].lc, val, depth - 1);
		}
	}
	int query(int ra, int rb, int rc, int rd, int re, int val) {
		if (a[ra].sum + a[rb].sum + a[rc].sum + a[rd].sum + a[re].sum) return query(ra, rb, rc, rd, re, val, depth);
		else return 0;
	}
} ST;
int n, opt, p[MAXN], a[MAXN];
int rootall, rootx[MAXN], rooty[MAXN], rootz[MAXN];
map <pair <int, pair <int, int>>, int> rootspe;
void addpoint(int x, int y, int z, int val) {
	rootall = ST.modify(rootall, val, 1);
	rootx[x] = ST.modify(rootx[x], val, -1);
	rooty[y] = ST.modify(rooty[y], val, -1);
	rootz[z] = ST.modify(rootz[z], val, -1);
	rootspe[make_pair(x, make_pair(y, z))] = ST.modify(rootspe[make_pair(x, make_pair(y, z))], val, 2);
}
int query(int x, int y, int z, int val) {
	return ST.query(rootall, rootx[x], rooty[y], rootz[z], rootspe[make_pair(x, make_pair(y, z))], val);
}
int main() {
	freopen("imbalance.in", "r", stdin);
	freopen("imbalance.out", "w", stdout);
	ST.init(30);
	read(n), read(opt);
	for (int i = 1; i <= n; i++)
		read(p[i]);
	for (int i = 1; i <= n; i++)
		read(a[i]);
	int lastans = 0, x = n, y = n, z = n, s = 0;
	addpoint(x, y, z, s);
	for (int i = 1; i <= n; i++) {
		if (opt) {
			p[i] = (p[i] ^ lastans) % 3;
			a[i] = a[i] ^ lastans;
		}
		s ^= a[i];
		if (p[i] == 0) x++, y--;
		if (p[i] == 1) y++, z--;
		if (p[i] == 2) z++, x--;
		printf("%d ", lastans = query(x, y, z, s));
		addpoint(x, y, z, s);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39972971/article/details/89180689
今日推荐