**题意:**给出一串序列,求区间连续异或值的最大和最小。
**思路:**如果不是出在专题里,想不到可以用字典树做。先求前缀异或值,转为二进制,加入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. instanceof 运算符简介

    文章摘自: http://www.ibm.com/developerworks/cn/web/1306_jiangjj_jsinstanceof/ https://developer.mozilla. ...

  2. scrapy(2)——scrapy爬取新浪微博(单机版)

    Sina爬虫教程   Scrapy环境搭建   环境:window10 + python2.7(包含scrapy)+ mongoDB 1.1 安装集成了python2.7的anaconda   ana ...

  3. IT启示录

    引用电影<夏洛特烦恼>中夏洛的一句话:"一直以来,我根本就不知道自己想要什么".可以说在写这篇博客之前我仍然没有考虑清楚之后的道路,即使早已明确了走游戏开发的道理,却不 ...

  4. iOS-addSubView时给UIView添加效果

    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; ...

  5. C# Winform防止闪频和再次运行

    其实想实现只允许运行一个实例很简单,就是从program的入口函数入手.有两种情况: 第一种,用户运行第二个的时候给一个提示: using System; using System.Collectio ...

  6. 【Linux】- mv命令

    Linux mv命令用来为文件或目录改名.或将文件或目录移入其它位置. 语法 mv [options] source dest mv [options] source... directory 参数说 ...

  7. Bookmark Sentry – 检查重复、删除死链书签 Chrome扩展

    Bookmark Sentry 的用处,就是 处理重复的收藏夹的死链 . 重复链收藏.具体,请百度. Bookmark Sentry 下载 :  https://files.cnblogs.com/f ...

  8. MVP开发模式的理解

    1.MVP是什么 如果从层次关系来讲,MVP属于Presentation层的设计模式.对于一个UI模块来说,它的所有功能被分割为三个部分,分别通过Model.View和Presenter来承载.Mod ...

  9. xpath获取块元素下<br>下的信息

    再爬虫取字段的时候遇到一种类似下面的结构: <p> <br> "通用名称:xxxxxx" </p> 用xpath取的方式://p//text() ...

  10. Redis 学习之主从复制

    该文使用centos6.5 64位    redis3.2.8 主从复制 Redis的复制功能是支持多个数据库之间的数据同步.一类是主数据库(master)一类是从数据库(slave),主数据库可以进 ...