Codeforces243A The Brand New Function

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[100010], pre[100010][21], ans, use[100010], maxn;
int st[100010][21], s[100010], tot, T;
bool flag = false;

void init()
{
    for (int j = 1; j <= 20; j++)
        for (int i = 1; i + (1 << j) - 1 <= n; i++)
            st[i][j] = st[i][j - 1] | st[i + (1 << (j - 1))][j - 1];
}

void col(int l, int r)
{
    int k = (int)((log(r - l + 1)) / log(2.0));
    s[++tot] = st[l][k] | st[r - (1 << k) + 1][k];
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &a[i]);
        if (a[i] == 0)
            flag = true;
        st[i][0] = a[i];
    }
    init();
    for (int i = 1; i <= n; i++)
    {
        memcpy(pre[i], pre[i - 1], sizeof(pre[i - 1]));
        int x = a[i], cnt = 1;
        while (x)
        {
            if (x % 2 == 1)
                pre[i][cnt] = i;
            x /= 2;
            cnt++;
        }
        cnt--;
        maxn = max(maxn, cnt);
    }
    for (int i = 1; i <= n; i++)
    {
        T++;
        for (int j = 1; 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 + 1, s + tot + 1);
    ans = unique(s + 1, s + 1 + tot) - s - 1;
    printf("%d
", ans);

    return 0;
}
原文地址:https://www.cnblogs.com/zbtrs/p/7856772.html