首页 > 其他分享 >Codeforces Round #815 (Div. 2) D2 Xor-Subsequence (hard version)

Codeforces Round #815 (Div. 2) D2 Xor-Subsequence (hard version)

时间:2022-08-20 01:00:28浏览次数:83  
标签:Xor int hard tr Codeforces oplus mx dp define

原题链接

\(A>B\),总是有二进制下从高到低的前\(k\)位相等,第\(k+1\)位\(A\)是\(1\),\(B\)是\(0\)
本题中\(A=a_i\oplus j\),\(B=a_j\oplus i\),这里有一个很奇妙的性质(手玩或者打表也可以发现):

\(a_i\oplus j\)和\(a_j\oplus i\)的前\(k\)位相等,等价于\(a_i\oplus i\)和\(a_j\oplus j\)的前\(k\)位相等

然后这题就可以做了,只要对\(a_i\oplus i\)在之前建立的字典树中从高位枚举,出现分支再进行讨论即可(\(a_i\)和\(i\)的当前位我们知道了,所以\(a_i\oplus j>a_j\oplus i\)成立只要按\(a_j\)和\(j\)的当前位分类讨论即可)

总结:位运算,异或性质,字典树上dp

参考博客

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;

#define fr first
#define se second
#define et0 exit(0);
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i ++)
#define rrep(i, a, b) for(int i = (int)(a); i >= (int)(b); i --)
#define IO ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;

const int INF = 0X3f3f3f3f, N = 3e5 + 10, MOD = 1e9 + 7;
const double eps = 1e-7, pi = acos(-1);

int idx;
int a[N];
int tr[N * 40][2], dp[N * 40][2];

void insert(int x, int y) {
	int p = 0;
	rrep (i, 30, 0) {
		int u = x >> i & 1, v = y >> i & 1;
		if (!tr[p][u ^ v]) tr[p][u ^ v] = ++idx;
		p = tr[p][u ^ v];
	}
}

int query(int x, int y) {
	int p = 0, q = 0, mx = 0;
	rrep (i, 30, 0) {
		int u = x >> i & 1, v = y >> i & 1;
		q = tr[p][!(u ^ v)], p = tr[p][u ^ v];
		if (q) mx = max(mx, dp[q][v]);
		if (!p) break;
	}
	return mx + 1;
}

void update(int x, int y, int mx) {
	int p = 0;
	rrep (i, 30, 0) {
		int u = x >> i & 1, v = y >> i & 1;
		p = tr[p][u ^ v];
		dp[p][u] = max(dp[p][u], mx);
	}
}

void work() {
	int n;
	cin >> n;
	rep (i, 0, n - 1) cin >> a[i];
	
	int res = 0;
	rep (i, 0, n - 1) {
		int mx = query(a[i], i);
		res = max(res, mx);
		insert(a[i], i);
		update(a[i], i, mx);
	}
	
	cout << res << endl;
	
	rep (i, 0, idx) tr[i][0] = tr[i][1] = dp[i][0] = dp[i][1] = 0;
	idx = 0;
}

signed main() {
	IO
	
	int test = 1;
	cin >> test;

	while (test--) {
		work();
	}

	return 0;
}

标签:Xor,int,hard,tr,Codeforces,oplus,mx,dp,define
From: https://www.cnblogs.com/xhy666/p/16607009.html

相关文章

  • CodeForces-1601B Frog Traveler
    FrogTravelerdp+bfs感觉又像搜索从后往前进行状态转移,像\(bfs\)一样保证当前搜索的是消耗次数最少的状态转移因为是一个连续的区间,因此考虑当前能上升到的最大距......
  • CodeForces-1671E Preorder
    Preorder树型dp+思维\(dp[i]\)表示以\(i\)为根的子树通过变换有多少种不同的先序遍历状态转移方程:当左右子树不同,两个子树交换位置之后,没有重复的出现:\(dp[x]......
  • Codeforces #815 Div.2
    A—BurenkaPlayswithFractions思路:数论O(1)见题解题解:#include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;typedeflonglongL......
  • CF1196D2 RGB Substring (hard version)
    https://www.luogu.com.cn/problem/CF1196D2前缀和黄色题思路:看当前输入要被修改的这个字符串的第i位,是否与'R','G','B'三个中的一个相等,不相等的另外两个则增加一次修......
  • Educational Codeforces Round 117 (Rated for Div. 2) CF1612
    https://codeforces.com/contest/1612VP过了A~E,感觉海星。F,G这几天补。主要是luogu有翻译拯救了英语不好的我。A一眼\(x+y\equiv0\pmod{2}\),否则无解。那么显......
  • Educational Codeforces Round 33 (Rated for Div. 2) 虚拟赛体验
    前言就只做出了\(A,B,C,D\)是不是很弱?A.ChessForThreeA,B,C三人下棋,A和B先下,每次下完棋之后由现在观战的人(例如第一局就由C)代替下输的人。每次输入一个数表示谁赢......
  • D2. Xor-Subsequence (hard version)
    D2.Xor-Subsequence(hardversion)昨天cf的E题,挺好的一个DP优化问题。暴力的DP就是设dp[i]表示以i结尾的最长长度。转移时枚举之前的所有j,复杂度O(n^2)。考虑怎么优......
  • 一张图看懂 OrchardCore 中的模块加载及依赖管理
    先上图   Manifest.cs  Module与FeatureModule特性 如果模块中只有一个功能【Feature】那么可以直接用Module替代,也就是///<summary>///......
  • Codeforces Round #815 (Div. 2) 全解
    目录ABCD1D2EAad和cb,查看是不是相等或者倍数关系,特判0Bsort()cout<<a[n]+a[n-1]-a[1]-a[2]C查看所有的四方格一个四方格有2个0,ans=1的个数一个四方格有1个0,ans=1......
  • Codeforces Round #815 (Div. 2) (补题中)
    战绩:  打到一半被叫走,回来后断断续续打完的。。。A.BurenkaPlayswithFractions刚开始感觉被trick绕进去了,思路有点乱,就先去切B了。实际上如果要a/b=c/d,我们只......