Codeforces 876F High Cry【逆向思维】

来源:互联网 发布:神经网络算法预测股市 编辑:程序博客网 时间:2024/06/05 18:36

F. High Cry
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Disclaimer: there are lots of untranslateable puns in the Russian version of the statement, so there is one more reason for you to learn Russian :)

Rick and Morty like to go to the ridge High Cry for crying loudly — there is an extraordinary echo. Recently they discovered an interesting acoustic characteristic of this ridge: if Rick and Morty begin crying simultaneously from different mountains, their cry would be heard between these mountains up to the height equal the bitwise OR of mountains they've climbed and all the mountains between them.

Bitwise OR is a binary operation which is determined the following way. Consider representation of numbers x and y in binary numeric system (probably with leading zeroes) x = xk... x1x0 and y = yk... y1y0. Then z = x | y is defined following way: z = zk... z1z0, where zi = 1, if xi = 1 or yi = 1, and zi = 0 otherwise. In the other words, digit of bitwise OR of two numbers equals zero if and only if digits at corresponding positions is both numbers equals zero. For example bitwise OR of numbers 10 = 10102 and 9 = 10012 equals 11 = 10112. In programming languages C/C++/Java/Python this operation is defined as «|», and in Pascal as «or».

Help Rick and Morty calculate the number of ways they can select two mountains in such a way that if they start crying from these mountains their cry will be heard above these mountains and all mountains between them. More formally you should find number of pairs land r (1 ≤ l < r ≤ n) such that bitwise OR of heights of all mountains between l and r (inclusive) is larger than the height of any mountain at this interval.

Input

The first line contains integer n (1 ≤ n ≤ 200 000), the number of mountains in the ridge.

Second line contains n integers ai (0 ≤ ai ≤ 109), the heights of mountains in order they are located in the ridge.

Output

Print the only integer, the number of ways to choose two different mountains.

Examples
input
53 2 1 6 5
output
8
input
43 3 3 3
output
0
Note

In the first test case all the ways are pairs of mountains with the numbers (numbering from one):

(1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)

In the second test case there are no such pairs because for any pair of mountains the height of cry from them is 3, and this height is equal to the height of any mountain.


题目大意:

让我们计算有多少个区间【L,R】,使得其或的和严格大于区间内所有的数。


思路:


①我们考虑正着做找可行区间发现很不容易,那么我们不妨反向考虑,找不合法的。如果我们把不合法的区间数量求出来了,再减一下就行。


②我们设定L【i】,表示以i作为中间点,【(L【i】~i),i】之内的区间都是不合法的,同理,对于R【i】,表示以i作为中间点,【i,(i,R【i】)】之间的区间都是不合法的。

那么Ans=n*(n+1)/2-(i-L【i】)*(R【i】-i)【1<=i<=n】


③我们以求L【i】为例,我们设定pos【j】=x,表示二进制下,第j位最后出现的位子在x处 (即a【x】&(1<<j)>0)。那么L【i】=max(L【i】,pos【j】)这里需要保证a【i】&(1<<j)==0;

求R【i】反过去就行了。同理。


Ac代码:

#include<stdio.h>#include<string.h>#include<algorithm>#include<map>using namespace std;int L[250000];int R[250000];int pos[250000];int a[250000];int main(){    int n;    while(~scanf("%d",&n))    {        memset(L,0,sizeof(L));        memset(R,0,sizeof(R));        memset(pos,0,sizeof(pos));        map<int,int>last;        for(int i=1;i<=n;i++)scanf("%d",&a[i]);        for(int i=1;i<=n;i++)        {            L[i]=last[a[i]];            for(int j=0;j<=31;j++)            {                if((a[i]&(1<<j))==0)                {                    L[i]=max(L[i],pos[j]);                }            }            for(int j=0;j<=31;j++)            {                if((a[i]&(1<<j))>0)pos[j]=i;            }            last[a[i]]=i;        }        for(int j=0;j<=31;j++)pos[j]=n+1;        for(int i=n;i>=1;i--)        {            R[i]=n+1;            for(int j=0;j<=31;j++)            {                if((a[i]&(1<<j))==0)                {                    R[i]=min(R[i],pos[j]);                }            }            for(int j=0;j<=31;j++)            {                if((a[i]&(1<<j))>0)pos[j]=i;            }        }        __int64 output=1ll*n*(n+1)/2;        for(int i=1;i<=n;i++)        {            __int64 temp1=i-L[i];            __int64 temp2=R[i]-i;            output-=temp1*temp2;        }        printf("%I64d\n",output);    }}





原创粉丝点击