题目链接

problem

对于一个长度为n的非负整数序列\(b_1,b_2,...,b_n\),定义这个序列的能量为:\(f(b)=\max\limits_{i=0,1,...,n}(b_1 \otimes b
_2 \otimes...\otimes b_i)+(b_{i+1} \otimes b_{i+2} \otimes...\otimes b_n)\),给定一个长度为n的非

负整数序列\(a_1,a_2,...,a_n\),请计算a的每个前缀的能量值。

solution

先对a求一边前缀异或和。然后对于前i个元素,从j位置分开的贡献就是\(S_i\otimes S_j+S_j\)

从高到低按位处理。如果\(S_i\)的第k位为1,那么无论\(S_j\)的第k位为0还是为1,造成的贡献都是\(2^k\)。如果\(S_i\)的第k位为0,那么如果在满足前面位置的限制的情况下,\(S_j\)的第k位可以为1,那么造成的贡献就是\(2\times 2^k\)。

那么问题来了,当\(S_i\)的第k位为0时,如何判断前面是否有某个位置在满足前面限制的情况下当前位置为1呢?

用\(f_i\)表示i的超集出现的最靠前的位置。然后上面的判断就很好做了:只要看一下满足所有限制的最小位置是不是在i之前就可以了。

因为是超集,所以\(f_i\)的预处理可以用\(FMT\)优化。复杂度\(\Theta(2^kk)\)

总复杂度就是\(\Theta(nk+2^kk)\)

code

/*
* @Author: wxyww
* @Date: 2019-12-17 22:04:49
* @Last Modified time: 2019-12-17 22:18:24
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;
const int N = 300100;
ll read() {
ll x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1; c = getchar();
}
while(c >= '0' && c <= '9') {
x = x * 10 + c - '0'; c = getchar();
}
return x * f;
}
int mi[N],mx,f[N * 10],a[N];
int main() {
int n = read(); memset(f,0x3f,sizeof(f)); for(int i = 1;i <= n;++i) {
a[i] = read() ^ a[i - 1];f[a[i]] = min(i,f[a[i]]);
mx = max(mx,a[i]);
} mi[0] = 1;
for(int i = 1;i <= 30;++i) mi[i] = mi[i - 1] << 1; for(int i = 0;i <= 20;++i) {
for(int j = 0;j <= mx;++j) {
if(!(j >> i & 1)) f[j] = min(f[j],f[j | (1 << i)]);
}
} for(int i = 1;i <= n;++i) {
int now = 0;
ll ans = 0;
for(int k = 20;k >= 0;--k) {
if((a[i] >> k) & 1) ans += mi[k];
else if(f[now | (1 << k)] <= i) {
ans += 2ll * mi[k],now |= (1 << k);
}
}
printf("%lld\n",ans);
}
return 0;
}

bzoj5092 分割序列的更多相关文章

  1. BZOJ5092 分割序列(贪心)

    设si为该序列的异或前缀和,则显然相当于求Σmax{sj+sj^si} (i=1~n,j=0~i).从高位到低位考虑,如果该位si为1,无论sj怎么填都是一样的:如果该位si为0,则sj该位应尽量为1 ...

  2. bzoj 5092 [Lydsy1711月赛]分割序列 贪心高维前缀和

    [Lydsy1711月赛]分割序列 Time Limit: 5 Sec  Memory Limit: 256 MBSubmit: 213  Solved: 97[Submit][Status][Dis ...

  3. bzoj 5092: [Lydsy1711月赛]分割序列

    5092: [Lydsy1711月赛]分割序列 Time Limit: 5 Sec  Memory Limit: 256 MBSubmit: 219  Solved: 100[Submit][Stat ...

  4. BZOJ5092:[Lydsy1711月赛]分割序列(贪心,高维前缀和)

    Description 对于一个长度为n的非负整数序列b_1,b_2,...,b_n,定义这个序列的能量为:f(b)=max{i=0,1,...,n}((b_1 xor b_2 xor...xor b ...

  5. 【BZOJ5092】分割序列(高维前缀和)

    题意:对于一个长度为n的非负整数序列b_1,b_2,...,b_n, 定义这个序列的能量为:f(b)=max{i=0,1,...,n}((b_1 xor b_2 xor...xor b_i)+(b_{ ...

  6. BZOJ.5092.[Lydsy1711月赛]分割序列(高维前缀和)

    题目链接 \(Description\) \(Solution\) 首先处理\(a_i\)的前缀异或和\(s_i\).那么在对于序列\(a_1,...,a_n\),在\(i\)位置处分开的价值为:\( ...

  7. BZOJ:5092 [Lydsy1711月赛]分割序列(贪心&高维前缀和)

    Description 对于一个长度为n的非负整数序列b_1,b_2,...,b_n,定义这个序列的能量为:f(b)=max{i=0,1,...,n}((b_1 xor b _2 xor...xor ...

  8. bzoj 5092 [Lydsy1711月赛]分割序列——高维前缀和

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5092 套路地弄一个前缀异或和,就变成 f[ i ]=max_{j=0}^{i} { s[ j ...

  9. bzoj 5092 分割序列 —— 高维前缀和

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5092 首先,处理出异或前缀和 s[i],i 位置的答案就是 s[j] + s[j]^s[i] ...

随机推荐

  1. 嵌入Canvas App到Dynamics 365 Customer Engagement(Model-Driven App)中,创造更多可能!

    我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...

  2. InnoDB On-Disk Structures(三)--Tablespaces (转载)

    转载.节选于 https://dev.mysql.com/doc/refman/8.0/en/innodb-tablespace.html This section covers topics rel ...

  3. Mac中 pip3 install mysqlclient 报错

    主要错误提示如下: ld: library not found for -lssl clang: error: linker command failed with exit code 1 (use ...

  4. 20182320《Program Design and Data Structures》Learning Summary Week9

    20182320<Program Design and Data Structures>Learning Summary Week9 1.Summary of Textbook's Con ...

  5. Java基础 - volatile

    volatile的作用:对与volatile修饰的变量, 1,保证该变量对所有线程的可见性. 2,禁止指令重排序. Java内存模型(JMM) 原子性 i = 2; 把i加载到工作内存副本i,副本i= ...

  6. Linux目录详解,软件应该安装到哪个目录

    原文地址:https://www.w3h5.com/post/336.html 我们应该知道 Windows 有一个默认的安装目录专门用来安装软件.Linux 的软件安装目录也应该是有讲究的,遵循这一 ...

  7. 【使用篇二】SpringBoot定时任务Scheduled(14)

    在日常项目运行中,我们总会有需求在某一时间段周期性的执行某个动作.比如每天在某个时间段导出报表,或者每隔多久统计一次现在在线的用户量.在springboot中可以有很多方案去帮我们完成定时器的工作,有 ...

  8. jquery选择器之模糊匹配

    模糊匹配主要分为前导模糊匹配,后导模糊匹配和全文模糊匹配. 前导模糊匹配[^=] 例子:选择name前缀为aa的所有div的jQuery对象. $("div[name^='aa']" ...

  9. WPF customize DelegateCommand

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin ...

  10. java基础(3):变量、运算符

    1. 变量 1.1 变量概述 前面我们已经学习了常量,接下来我们要学习变量.在Java中变量的应用比常量的应用要多很多.所以变量也是尤为重要的知识点! 什么是变量?变量是一个内存中的小盒子(小容器), ...