zoukankan      html  css  js  c++  java
  • 最大异或和

    最大异或和

    解题思路

    这题是一道版题,学过可持久化字典树的应该都会

    Code

    #include<algorithm>
    #include<cstdio>
    using namespace std;
    const int N = 6e5 + 5;
    int n,m,s[N],rt[N],ch[30 * N][2],sum[60 * N],tot = 0;
    
    void update(int u,int v,int x)
    {
    	for (int i = 30; i >= 0; i--)
    	{
    		int c = (x >> i) & 1;
    		sum[u] = sum[v] + 1;
    		ch[u][c ^ 1] = ch[v][c ^ 1];
    		ch[u][c] = ++ tot;
    		u = ch[u][c],v = ch[v][c];
    	}
    	sum[u] = sum[v] + 1;
    }
    int query(int u,int v,int x)
    {
    	int res = 0;
    	for (int i = 30; i >= 0; i--)
    	{
    		int c = (x >> i) & 1,k = sum[ch[u][c ^ 1]] - sum[ch[v][c ^ 1]];
    		if (k) res |= 1 << i,u = ch[u][c ^ 1],v = ch[v][c ^ 1];
    		else u = ch[u][c],v = ch[v][c];
    	}
    	return res;
    }
    int main()
    {
    	scanf("%d%d",&n,&m);
    	for (int i = 1; i <= n; i++)
    	{
    		int q;
    		scanf("%d",&q);
    		s[i] = s[i - 1] ^ q;
    	}
    	for (int i = 1; i <= n; i++) rt[i] = ++tot,update(rt[i],rt[i - 1],s[i]);
    	for (int i = 1; i <= m; i++)
    	{
    		char ch1[3];
    		int q,p,c;
    		scanf("%s",ch1);
    		if (ch1[0] == 'A') 
    			scanf("%d",&q),n++,s[n] = s[n - 1] ^ q,
    			rt[n] = ++tot,update(rt[n],rt[n - 1],s[n]);
    		if (ch1[0] == 'Q')
    		{
    			scanf("%d%d%d",&q,&p,&c);
    			q--,p--;
    			if (q == p && q == 0) printf("%d
    ",s[n] ^ c);
    			else printf("%d
    ",query(rt[p],rt[max(0,q - 1)],s[n] ^ c));
    		}
    	}
    }
    
  • 相关阅读:
    【CF 547E】 Mike and Friends
    [LNR#2 D1T3] 不等关系
    [THUPC2019] 不等式
    [THUPC2019]过河卒二
    [THUPC2018] 绿绿和串串
    【IOI2018】组合动作
    229. Majority Element II My Submissions Question
    169. Majority Element My Submissions Question
    ### Caffe
    ### OpenCV安装(Linux)
  • 原文地址:https://www.cnblogs.com/nibabadeboke/p/13466163.html
Copyright © 2011-2022 走看看