A. The Brand New Function
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarpus has a sequence, consisting of n non-negative integers: a1, a2, ..., an.

Let's define function f(l, r) (l, r are integer, 1 ≤ l ≤ r ≤ n) for sequence a as an operation of bitwise OR of all the sequence elements with indexes from l to r. Formally: f(l, r) = al | al + 1 | ...  | ar.

Polycarpus took a piece of paper and wrote out the values of function f(l, r) for all l, r (l, r are integer, 1 ≤ l ≤ r ≤ n). Now he wants to know, how many distinct values he's got in the end.

Help Polycarpus, count the number of distinct values of function f(l, r) for the given sequence a.

Expression x | y means applying the operation of bitwise OR to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is marked as "|", in Pascal — as "or".

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of elements of sequence a. The second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 106) — the elements of sequence a.

Output

Print a single integer — the number of distinct values of function f(l, r) for the given sequence a.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64dspecifier.

Examples
input
3
1 2 0
output
4
input
10
1 2 3 4 5 6 1 2 9 10
output
11
Note

In the first test case Polycarpus will have 6 numbers written on the paper: f(1, 1) = 1, f(1, 2) = 3, f(1, 3) = 3, f(2, 2) = 2, f(2, 3) = 2, f(3, 3) = 0. There are exactly 4 distinct numbers among them: 0, 1, 2, 3.

大致题意:给定一个长度为n的区间,问有多少个子区间或起来的值不相同.

分析:非常好的一道题.为了实现O(1)查询,可以先用一个ST表处理出所有区间或起来的值.然后就可以O(n^2)的做了.

有关二进制的题一个常见的优化就是分位处理.在枚举区间的时候,先固定右端点,向左延伸确定左端点,考虑二进制下的第j位,记pre[i][j]表示在第i个数前二进制下第j位为1的最右边的数的位置.这个可以在读入的时候预处理出来.根据处理出来的这个pre数组,就能够不用一维一维地枚举左端点,而是可以“跳”.将所有跳到的点l标记一下,那么[l,i]的或值就要被考虑,放到数组里.最后排序去重就可以了.

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int n, a[], pre[][], ans, use[], maxn;
int st[][], s[], tot, T;
bool flag = false; void init()
{
for (int j = ; j <= ; j++)
for (int i = ; i + ( << j) - <= n; i++)
st[i][j] = st[i][j - ] | st[i + ( << (j - ))][j - ];
} void col(int l, int r)
{
int k = (int)((log(r - l + )) / log(2.0));
s[++tot] = st[l][k] | st[r - ( << k) + ][k];
} int main()
{
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
if (a[i] == )
flag = true;
st[i][] = a[i];
}
init();
for (int i = ; i <= n; i++)
{
memcpy(pre[i], pre[i - ], sizeof(pre[i - ]));
int x = a[i], cnt = ;
while (x)
{
if (x % == )
pre[i][cnt] = i;
x /= ;
cnt++;
}
cnt--;
maxn = max(maxn, cnt);
}
for (int i = ; i <= n; i++)
{
T++;
for (int j = ; j <= maxn; j++)
{
if (pre[i][j])
{
if (use[pre[i][j]] != T) //时间戳
{
use[pre[i][j]] = T;
col(pre[i][j], i);
}
}
}
s[++tot] = a[i];
}
sort(s + , s + tot + );
ans = unique(s + , s + + tot) - s - ;
printf("%d\n", ans); return ;
}

Codeforces243A The Brand New Function的更多相关文章

  1. Codeforces G. The Brand New Function(枚举)

    题目描述: The Brand New Function time limit per test 2 seconds memory limit per test 256 megabytes input ...

  2. [Javascript] The "this" keyword

    The very first thing to understand when we're talking about this-keyword is really understand what's ...

  3. Codeforces Round #150 (Div. 2)

    A. Dividing Orange 模拟. B. Undoubtedly Lucky Numbers 暴力枚举\(x.y\). C. The Brand New Function 固定左端点,右端点 ...

  4. 我所理解的 PHP Trait

    Trait 是从 PHP 5.4 加入的一种细粒度代码复用的语法.以下是官方手册对 Trait 的描述: Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制.Trait 为了减少单继承 ...

  5. 自己写的highcharts级联(点击事件)

    $.fn.extend({ Zhu: function (option) { var id = $(this).attr("id"); $('#' + id).highcharts ...

  6. 用js采集网页数据并插入数据库最快的方法

    今天教大家一个快速采集网站数据的方法,因为太晚了,直接上例子,这里以采集易车网的产品数据为例. 思路:利用js获取网页数据并生成sql命令,执行sql命令把采集的数据插入数据库. 1.用谷歌浏览器或者 ...

  7. TypeScript - 类型声明、枚举、函数、接口

    目录  可定义的类型  类型声明  枚举  函数  接口 可定义的类型 以下所写的并不代表typescript的数据类型,而是在使用过程中可以用作定义的类型 number : 数值类型: string ...

  8. Javascript——概述 && 继承 && 复用 && 私有成员 && 构造函数

    原文链接:A re-introduction to JavaScript (JS tutorial) Why a re-introduction? Because JavaScript is noto ...

  9. mpvue + 微信小程序 picker 实现自定义多级联动 超简洁

    微信小程序官网只提供了省市区的三级联动,实际开发中更多的是自定义的多级联动: 依照微信小程序官网提供的自定义多级联动,需要使用到picker 的多列选择器,即设置 mode = multiSelect ...

随机推荐

  1. sqli-labs学习笔记 DAY3

    DAY 3 sqli-labs lesson 6 同lesson 5,只是把单引号改为双引号 sqli-labs lesson 7 同lesson 5,只是把单引号后面加两个空格,使用Burpsuit ...

  2. 关于css文字的扩展

    1.不换行: .title{ white-space:nowrap; text-overflow:ellipsis; } 2.超出变三点 .title{ white-space:nowrap; tex ...

  3. 20181120-4 Beta阶段第2周/共2周 Scrum立会报告+燃尽图 01

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2018fall/homework/2409 版本控制地址   https://git.coding.net/lg ...

  4. c# printDialog不显示问题

    1.遇到问题:同样的代码,一个可以运行成功,另一个失败.百思不得其解情况下,监视下看每一个参数的属性是否一样,但参数太多,需要时间. 主要问题一般归结为:两个项目的属性编译设置不同,果然,一个x86正 ...

  5. Team Work总结 && OPP课程总结

    团队作业总结 工作总结 本次大作业我在团队内的工作是:根据框架构建实现建筑类的功能,包括防御塔.水晶.泉水等建筑.根据架构框架以及结合各建筑的特点,利用继承和多态很快速的解决了一些基本的问题.然而在实 ...

  6. Alpha冲刺——第九天

    Alpha第九天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...

  7. KMP的原理和代码实现(详细注释|参考多个博客总结|可作为模板)

    KMP算法解决的问题是字符匹配,是由Knuth–Morris–Pratt共同开发出来的,这个算法把字符匹配的时间复杂度缩小到O(m+n),而空间复杂度也只有O(m),n是target的长度,m是pat ...

  8. 周总结<2>

    本打算在这周日写周总结的,但由于事情太多,还要组织团日活动,所以没时间写.不过上周主要是一些书本上的学习,但是在周日的时候完成了一款小游戏还是比较有成就感的,但是主要是因为html的考试才去做的. 代 ...

  9. J2EE面试常见试题

    一.基础问答 1.下面哪些类可以被继承? java.lang.Thread (T) java.lang.Number (T) java.lang.Double (F) java.lang.Math  ...

  10. 整理sql server数据类型

    我们在平常开发过程中,在设计数据的时候,经常碰到数据类型选择的问题,为了更快,更合适地选择正确的数据类型,所以在这里做个总结. 分类 sql server 数据类型 c# 数据类型 描述 应用场景 字 ...