LightOJ 1269 Consecutive Sum (Trie树)
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树)的更多相关文章
- LightOJ 1269 - Consecutive Sum Trie树
题意:给出一串序列,求区间连续异或值的最大和最小. 思路:如果不是出在专题里,想不到可以用字典树做.先求前缀异或值,转为二进制,加入Trie树中,如果要求最大,就是尽可能走和当前位数字相反的,这样异或 ...
- 1269 - Consecutive Sum
1269 - Consecutive Sum PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB ...
- LightOJ 1129 - Consistency Checker Trie树模板
题意:给出n条串判断是否存在一个串为另一个串的前缀. 思路:套Trie树的模板,先全部插入,再查找每个字串,如果查找字串完毕,但还存在下一个节点,说明存在前缀. /** @Date : 2016-11 ...
- hdu 4825 Xor Sum trie树
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Proble ...
- HDU4825 Xor Sum —— Trie树
题目链接:https://vjudge.net/problem/HDU-4825 Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- Consecutive Sum LightOJ - 1269(区间异或和)
Consecutive Sum 又来水一发blog... 本来是昨天补codechef的题,最后一道题是可持久化字典树,然后去黄学长博客看了看 觉得字典树写法有点不太一样,就想着用黄学长的板子写码几道 ...
- HDU 4825 Xor Sum (trie树处理异或)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- 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 ...
- HDU4825 Xor Sum(贪心+Trie树)
Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeu ...
随机推荐
- jnhs[未解决]无法使用选定的hibernate配置文件建立数据库连接.请验证hibernate.cfg.xml中的数据库连接详情信息
工程可以正常的使用读写数据库,当然model和model.hbm.xml文件是自己写的. 解决中
- django中的聚合索引
Django(元信息)元类建索引 ORM查询(sql优化)优化 自定义聚合函数 Django的元类建索引————索引:索引的一个主要目的就是加快检索表中数据,索引是经过某种算法优化过的,因而查找次数要 ...
- Hdu 1156
题目链接 Brownie Points II Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- ecshop二次开发之视频上传
1.前台展示效果: 2.后台展示效果: 3.代码实现: 后台实现过程: 1.在languages/zh_cn/admin/goods.PHP中插入 $_LANG['tab_video'] = '视频上 ...
- 理解nodejs的module
module 在 Node.js 模块系统中,每个文件都视为独立的模块,node在运行某个模块儿时会生成一个module对象 Module { id: '.', exports: 2, parent: ...
- php_imagick是怎么实现复古效果的呢?
php_imagick程序示例 1.创建一个缩略图并显示出来 <?phpheader('Content-type: image/jpeg');$image = new Imagick('imag ...
- HTML-DOM常用对象的用法(select/option/form/table)
HTML DOM 常用对象: 它对常用HTML元素操作的简化. Select对象 它代表页面上的一个select元素,常用属性有: select.value ——当前选中项的value ,没有valu ...
- 设置脚本sh
- PAT天梯赛L1-002 打印漏斗
题目链接:点击打开链接 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个"*",要求按下列格式打印 ***** *** * *** ***** 所谓"沙漏形 ...
- 分布式Jmeter
遇到的问题 1.压力不够大 2.单台瓶颈 3.网络瓶颈 分布式系统是由一组通过网络进行通信.为了完成共同的任务而协调工作的计算机节点组成的系统.分布式系统的出现是为了用廉价的.普通的机器完成单个计算机 ...