Jan's LightOJ :: Problem 1269 - Consecutive Sum

  题意是,求给定序列的中,子序列最大最小的抑或和。

  做法就是用一棵Trie树,记录数的每一位是0还是1。查询的时候,如果求最大值,就尽量让高位是1,相反就尽量让高位是0。

代码如下,1y:

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
const int M = ; struct Node {
int c[];
void init() { c[] = c[] = -;}
} ; struct Trie {
Node node[M * N];
int rt, curid;
void init() { rt = curid = ; node[curid++].init();}
void insert(int x, int dg = ) {
int p = rt;
for (int i = dg - , idx; i >= ; i--) {
idx = (x & << i) != ;
if (node[p].c[idx] == -) node[node[p].c[idx] = curid++].init();
p = node[p].c[idx];
}
}
int min(int x, int dg = ) {
int p = rt, ret = ;
for (int i = dg - , idx; i >= ; i--) {
idx = (x & << i) != ;
if (~node[p].c[idx]) p = node[p].c[idx], ret <<= , ret |= idx;
else p = node[p].c[!idx], ret <<= , ret |= !idx;
}
return ret;
}
int max(int x, int dg = ) {
int p = rt, ret = ;
for (int i = dg - , idx; i >= ; i--) {
idx = (x & << i) == ;
if (~node[p].c[idx]) p = node[p].c[idx], ret <<= , ret |= idx;
else p = node[p].c[!idx], ret <<= , ret |= !idx;
}
return ret;
}
} trie; int main() {
int T, n, x;
scanf("%d", &T);
for (int cas = ; cas <= T; cas++) {
scanf("%d", &n);
int ans1 = 0x80000000, ans2 = 0x7fffffff, sum = ;
trie.init();
trie.insert();
for (int i = ; i < n; i++) {
scanf("%d", &x);
sum ^= x;
//cout << sum << endl;
ans1 = max(ans1, trie.max(sum) ^ sum);
ans2 = min(ans2, trie.min(sum) ^ sum);
trie.insert(sum);
}
printf("Case %d: %d %d\n", cas, ans1, ans2);
}
return ;
}

  其实做这题的时候想到一种比较麻烦的情况,那就是数里面有负数。这样的话需要对最高位特判,从而求得最大最小值。不过没有这样的数据,所以就不这么搞了。

——written by Lyon

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

  1. LightOJ 1269 - Consecutive Sum Trie树

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

  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. Could not parse mapping document from resource com/hs/model/StudentModel.hbm.xml

    网上出现这个问题的 lei.hbm.xml配置写错的,文件头应该改为如下,并不是这个问题 <?xml version="1.0"?> <!DOCTYPE hibe ...

  2. python基础--数据类型的常用方法1

    1.数字类型 整型 用途:存qq号,手机号,不带字母的身份证号... 进制转换: 二进制转十进制:10 -->  1*(2**1) + 0*(2**0) 2 八进制转十进制:  235  --& ...

  3. 洛谷P1029 最大公约数和最小公倍数问题 [2017年6月计划 数论02]

    P1029 最大公约数和最小公倍数问题 题目描述 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数 条件: 1 ...

  4. PHP通过sql生成CSV文件并下载,PHP实现文件下载

    /** * PHP通过sql生成CSV文件并下载 * @param string $sql 查询sql,结果为二维数组 * @param array $title 数据,CSV文件标题 * @para ...

  5. 利用InformationSchema与阿里云交易和账单管理API实现MaxCompute费用对账分摊统计

    利用MaxCompute InformationSchema与阿里云交易和账单管理API 实现MaxCompute费用对账分摊统计 一.需求场景分析 很多的企业用户选择MaxCompute按量付费模式 ...

  6. listview显示固定条数

    看了很多网上其他大神的,感觉还是在listview的adapter中的getCount中下手比较好点 毕竟计算高度等等,那会让辅助的布局会一团糟,例如下面的搜索历史只显示四条,布局中有横向listvi ...

  7. javascript函数式编程和链式优化

    1.函数式编程理解 函数式编程可以理解为,以函数作为主要载体的编程方式,用函数去拆解.抽象一般的表达式 与命令式相比,这样做的好处在哪?主要有以下几点: (1)语义更加清晰 (2)可复用性更高 (3) ...

  8. Hdu 4920矩阵乘法(内存访问的讲究)

    题目链接 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K ( ...

  9. LintCode刷题笔记--Longest Increasing Subsequence

    标签: 动态规划 描述: Given a sequence of integers, find the longest increasing subsequence (LIS). You code s ...

  10. 50倍时空算力提升,阿里云RDS PostgreSQL GPU版本上线

    2019年3月19日,阿里云RDS PostgreSQL数据库GPU规格版本正式上线,开启了RDS异构计算并行加速之路.该版本在RDS(关系型数据库服务)的云基础设施层面首次完成了与阿里云异构计算产品 ...