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 ...
随机推荐
- iOS开发之多线程技术—GCD篇
本篇将从四个方面对iOS开发中GCD的使用进行详尽的讲解: 一.什么是GCD 二.我们为什么要用GCD技术 三.在实际开发中如何使用GCD更好的实现我们的需求 一.Synchronous & ...
- hive的简单使用
一.一些说明 1.支持的操作 hive 默认不支持updata 和 delete操作 insert也是执行缓慢,主要用于数据的计算 hive 数据类型---字符串,大部分与java一致. 2.内外表的 ...
- vps搭建个人网盘不二之选—kodexplorer介绍,包含安装步骤
之前给大家介绍过seafile.h5ai等网盘系统,今天给大家介绍下kodexplorer网盘系统.Kodexplorer,也叫芒果云.可道云.kodcloud,总之名字改了不少.但其本身作为一个网盘 ...
- Fast R-CNN学习总结
Fast R-CNN是R-CNN的改良版,同时也吸取了SPP-net中的方法.在此做一下总结. 论文中讲到在训练阶段,训练一个深度目标检测网络(VGG16),训练速度要比R-CNN快9倍左右,比SPP ...
- charles 在mac下 抓取 https包
1. 打开charles --> help --> SSL proxying --> install charles root certificate 2. 在弹出的添加证书窗口中 ...
- Xftp安装和使用的视频录制方法
内容: 1.使用工具 2.操作步骤及方法 视频地址: http://v.youku.com/v_show/id_XMzEwNjg2MTg2NA==.html?spm=a2h3j.8428770.341 ...
- TDGA-需求分析
李青:绝对的技术控,团队中扮演“猪”的角色,勤干肯干,是整个团队的主心骨,课上紧跟老师的步伐,下课谨遵老师的指令,课堂效率高,他的编程格言“没有编不出来的程序,只有解决不了的bug”. 胡金辉:半两油 ...
- 在windows和unbuntu上安装octave
windows安装octave 安装wiki Octave ftp库 从上述的库中可以找到对应的版本的octave的exe安装程序,或者是zip等的压缩包,建议直接下载对应系统的exe执行文件.安装. ...
- BETA版本前冲刺准备
[团队概要] 团队项目名:小葵日记 团队名:日不落战队 队员及角色: 队员 角色 备注 安琪 前端工程师 队长 佳莹 前端工程师 智慧 后端工程师 章鹏 后端工程师 语恳 UI设计师 炜坤 前端工程师 ...
- 【week10】规格说明书练习-吉林市1日游
假设我们全班同学及教师去吉林省吉林市1日游,请为这次活动给出规格说明书. 版本:1.0 编订:于淼 团队:2016级计算机技术全体同学 日期:2016/11/19 1.引言 1.1 编写目的 1.2 ...