Codeforces243A The Brand New Function
2 seconds
256 megabytes
standard input
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".
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.
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.
3
1 2 0
4
10
1 2 3 4 5 6 1 2 9 10
11
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的更多相关文章
- Codeforces G. The Brand New Function(枚举)
题目描述: The Brand New Function time limit per test 2 seconds memory limit per test 256 megabytes input ...
- [Javascript] The "this" keyword
The very first thing to understand when we're talking about this-keyword is really understand what's ...
- Codeforces Round #150 (Div. 2)
A. Dividing Orange 模拟. B. Undoubtedly Lucky Numbers 暴力枚举\(x.y\). C. The Brand New Function 固定左端点,右端点 ...
- 我所理解的 PHP Trait
Trait 是从 PHP 5.4 加入的一种细粒度代码复用的语法.以下是官方手册对 Trait 的描述: Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制.Trait 为了减少单继承 ...
- 自己写的highcharts级联(点击事件)
$.fn.extend({ Zhu: function (option) { var id = $(this).attr("id"); $('#' + id).highcharts ...
- 用js采集网页数据并插入数据库最快的方法
今天教大家一个快速采集网站数据的方法,因为太晚了,直接上例子,这里以采集易车网的产品数据为例. 思路:利用js获取网页数据并生成sql命令,执行sql命令把采集的数据插入数据库. 1.用谷歌浏览器或者 ...
- TypeScript - 类型声明、枚举、函数、接口
目录 可定义的类型 类型声明 枚举 函数 接口 可定义的类型 以下所写的并不代表typescript的数据类型,而是在使用过程中可以用作定义的类型 number : 数值类型: string ...
- Javascript——概述 && 继承 && 复用 && 私有成员 && 构造函数
原文链接:A re-introduction to JavaScript (JS tutorial) Why a re-introduction? Because JavaScript is noto ...
- mpvue + 微信小程序 picker 实现自定义多级联动 超简洁
微信小程序官网只提供了省市区的三级联动,实际开发中更多的是自定义的多级联动: 依照微信小程序官网提供的自定义多级联动,需要使用到picker 的多列选择器,即设置 mode = multiSelect ...
随机推荐
- JAVA学习笔记--匿名内部类
匿名内部类,即没有名字的内部类. 我们在编写JAVA程序时,往往要创建很多类,类是可以被重复使用的.但有时,我们创建了一个类,却只需要使用该类一次,那么单独为其编写一个类就显得有些麻烦,这时可以使用匿 ...
- python request 获取cookies value值的方法
import requests res = requests.get(url) cookies = requests.utils.dict_from_cookiejar(res.cookies) pr ...
- 深度系统(deepin)与win10双系统切换设置
之前在win10下安装了深度系统,我不知道其他人在双系统切换的时候是否需要更改BIOS参数,我根据我的实际情况给出双系统切换设置的解决方案. 1.开机后进入选项System setup 2.按照下图选 ...
- Scrum立会报告+燃尽图(十一月二十一日总第二十九次):β阶段第二周分配任务
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...
- 读《it小小鸟》有感
我一直认为大学就是一个自由的舒适的学习环境,没有人可以干扰你限制你,以至于我到了大学之后只剩下了颓废的生活.每天上课玩手机,下课玩电脑,吃饭叫外卖,从不去锻炼,周末就熬夜通宵,状态越来越差,导致我逐渐 ...
- Spring的初始化:org.springframework.web.context.ContextLoaderListener
在web.xml中配置 <listener> <listener-class>org.springframework.web.context.ContextLoaderL ...
- 3dContactPointAnnotationTool开发日志(三四)
今天就是让背景图可以变大变小,变透明度,然后将3d的点投影到图片上,输出2d接触点信息: 可以看到输出了正确的接触点信息: 然后还把空物体的包围盒大小设置为边长为0.1的的正方体,点击选中 ...
- Splash广告界面
在软件开始启动时都是会使用一个splashActivity实现联网判断和相关资源的加载,在一款网络软件上开始时的缓存加载和网络判断可以为用户节省不必要的流量开销. 使用handler延时启动下一个ac ...
- QMetaEnum利用Qt元数据实现枚举(enum)类型值及字符串转换
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QMetaEnum利用Qt元数据实现枚举(enum)类型值及字符串转换 本文地址:ht ...
- Java Servlet简介
一.了解Servlet的概念 Servlet定义 Servlet是基于Java技术的Web组件,由容器管理并产生动态的内容.Servlet引擎作为WEB服务器的扩展提供支持Servlet的功能.Se ...