**题意:**给出一串序列,求区间连续异或值的最大和最小。
**思路:**如果不是出在专题里,想不到可以用字典树做。先求前缀异或值,转为二进制,加入Trie树中,如果要求最大,就是尽可能走和当前位数字相反的,这样异或值才能保持最大,最小,就尽可能走和当前位数字相同的,走到尽头异或前缀值。

/** @Date    : 2016-11-10-16.34

* @Author : Lweleth (SoungEarlf@gmail.com)

* @Link : https://github.com/

* @Version :

*/

#include <stdio.h>

#include <iostream>

#include <string.h>

#include <algorithm>

#include <utility>

#include <vector>

#include <map>

#include <set>

#include <string>

#include <stack>

#include <queue>

#define LL long long

#define MMF(x) memset((x),0,sizeof(x))

#define MMI(x) memset((x), INF, sizeof(x))

using namespace std;



const int INF = 0x3f3f3f3f;

const int N = 5e4+20;



struct yuu

{

int cnt;

int val[35*N];//前缀和值

int nxp[35*N][2];



int init()

{

MMF(nxp[0]);

MMF(val);

cnt = 1;

}



int idx(char c)

{

return c - '0';

}



int insert(char *s, int v)//

{

int u = 0;

int x = strlen(s);

for(int i = 0; i < x; i++)

{

int c = idx(s[i]);

if(nxp[u][c] == 0)

{

MMF(nxp[cnt]);

nxp[u][c] = cnt;

cnt++;

}

u = nxp[u][c];

}

val[u] = v;

}



int find(char *s, int a, int ma)//ma 代表 是否找相反的

{

int u = 0;

int x = strlen(s);



for(int i = 0; i < x; i++)

{

int c = idx(s[i]);

int v = c ^ ma;

if(nxp[u][v] == 0)

{

v ^= 1;

}

u = nxp[u][v];

}

return val[u] ^ a;//尽头赋值

}

}tt;



void atoi_(char *s, int x)

{

for(int i = 31; i >= 0; i--)

{

s[i] = ((x >> i) & 1) + '0';

}

s[32] = '\0';

reverse(s, s + 32);

// for(int i = 0; i <= 32; i++)

// cout << s[i] ;

// cout << endl;

}



int a[N];

int sum[N];

char t[35];



int main()

{

int T;

int cnt = 0;

cin >> T;

while(T--)

{

int n;

scanf("%d", &n);

sum[0] = 0;

for(int i = 1; i <= n; i++)

{

scanf("%d", a + i);

sum[i] = sum[i-1] ^ a[i];

}

tt.init();

atoi_(t, 0);

tt.insert(t, 0);



int ma = -INF;

int mi = INF;

for(int i = 1; i <= n; i++)

{

atoi_(t, sum[i]);

ma = max(ma, tt.find(t, sum[i], 1));

mi = min(mi, tt.find(t, sum[i], 0));

tt.insert(t, sum[i]);

}

printf("Case %d: %d %d\n", ++cnt, ma, mi);

}

return 0;

}

LightOJ 1269 - Consecutive Sum Trie树的更多相关文章

  1. LightOJ 1269 Consecutive Sum (Trie树)

    Jan's LightOJ :: Problem 1269 - Consecutive Sum 题意是,求给定序列的中,子序列最大最小的抑或和. 做法就是用一棵Trie树,记录数的每一位是0还是1.查 ...

  2. 1269 - Consecutive Sum

    1269 - Consecutive Sum    PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB ...

  3. LightOJ 1129 - Consistency Checker Trie树模板

    题意:给出n条串判断是否存在一个串为另一个串的前缀. 思路:套Trie树的模板,先全部插入,再查找每个字串,如果查找字串完毕,但还存在下一个节点,说明存在前缀. /** @Date : 2016-11 ...

  4. hdu 4825 Xor Sum trie树

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Proble ...

  5. HDU4825 Xor Sum —— Trie树

    题目链接:https://vjudge.net/problem/HDU-4825 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Li ...

  6. Consecutive Sum LightOJ - 1269(区间异或和)

    Consecutive Sum 又来水一发blog... 本来是昨天补codechef的题,最后一道题是可持久化字典树,然后去黄学长博客看了看 觉得字典树写法有点不太一样,就想着用黄学长的板子写码几道 ...

  7. HDU 4825 Xor Sum (trie树处理异或)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

  8. CodeForces979D:Kuro and GCD and XOR and SUM(Trie树&指针&Xor)

    Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...

  9. HDU4825 Xor Sum(贪心+Trie树)

    Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeu ...

随机推荐

  1. [leetcode-744-Find Smallest Letter Greater Than Target]

    Given a list of sorted characters letters containing only lowercase letters, and given a target lett ...

  2. 衡量失业:失业率(Unemployment Rate)

    定义 劳动力 = 就业人数 + 失业人数 失业率 = (失业人数 / 劳动力) * % 劳动力参与率 = (劳动力 / 成年人口) * %

  3. allocator类

    一.动态数组 [new的局限性] new将内存分配和对象构造组合在一起,同样delete将对象析构和内存释放组合在一起 我们分配单个对象时,通常希望将内存分配和对象初始化组合在一起(我们知道对象应有什 ...

  4. python中装饰器的原理以及实现,

    python版本 3.6 1.python的装饰器说白了就是闭包函数的一种应用场景,在运用的时候我们遵循 #开放封闭原则:对修改封闭,对拓展开放 2.什么是装饰器 #装饰他人的器具,本身可以是任意可调 ...

  5. OSG学习:基本几何体绘制示例

    绘制并渲染几何体主要有如下3大步骤: 1.创建各种向量数据,如顶点.纹理坐标.颜色和法线等.需要注意的是,添加顶点数据时主要按照逆时针顺序添加, 以确保背面剔除的正确. 2.实例化一个几何体对象(os ...

  6. Sqoop使用笔记(转载)

    Sqoop是Apache顶级项目,主要用来在Hadoop和关系数据库中传递数据.通过sqoop,可以方便的将数据从关系数据库导入到HDFS,或将数据从HDFS导出到关系数据库. 关于Sqoop 官网S ...

  7. PHP连接Redis操作函数

    phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/ow ...

  8. Delphi中Sender对象的知识

    Sender是一个TObject类型的参数,它告诉Delphi哪个控件接收这个事件并调用相应的处理过程.你可以编写一个单一的事件处理句柄,通过Sender参数和IF…THEN…语句或者CASE语句配合 ...

  9. Windows 8.1 SecureBoot未正确配置的解决方法

    使用联想Y510P,安装win8.1后破解 ,屏幕右下角老是显示 SecureBoot未正确配置的解决方法,以下是解决方案 步骤1:在机器重启至“Lenovo字样的屏幕”时,不停敲击“F2”键或“Fn ...

  10. 【python】用 sqlacodegen 将存在的数据库表 转化成model.py

    Flask的sqlalchemy对数据库表的模型提供了很多易用的方法.为了使用这些内容,需要将数据库表按照Flask识别的格式创建成Model,但是一般我们都是在已经创建好的数据库环境中开发Pytho ...