F. High Cry
Problem Statement
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 l and 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
Example 1
Input
5
3 2 1 6 5
Output
8
Example 2
Input
4
3 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.
题意
给一个一共有n个元素的数组,问你一共有几个区间满足条件,满足条件的意思是这个区间所有数的or值大于这个区间的最大值。
思路
我们可以将这题转化为所有的区间减去不满足的区间,首先,我们可以求出每个数向左和向右统领的范围分别为l[i]和r[i],那么不满足的区间就是(r[i]-i)*(i-l[i])的个数,我们定义统领的范围l[i]就是这个范围内所有的数与i or 起来的值都等于i,不超过i。所以我们在算l[i]的时候就从左向右扫,算r[i]的时候就从右往左扫,这样就可以在 O(32∗n) 的时间复杂度过掉这道题了,完全不需要考虑卡时间。
Code
#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline void readInt(int &x) {
x=0;int f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
x*=f;
}
inline void readLong(ll &x) {
x=0;int f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
x*=f;
}
/*================Header Template==============*/
int l[200010],r[200010],n,a[200010],p[33];
map<int,int> pos;
ll ans;
int main() {
readInt(n);
for(int i=1;i<=n;i++)
readInt(a[i]);
ans=(ll)n*(ll)(n+1)/2;
for(int i=0;i<32;i++)
p[i]=0;
for(int i=1;i<=n;i++) {
l[i]=pos[a[i]];
for(int j=0;j<32;j++)
if(!(a[i]&(1<<j)))
l[i]=max(l[i],p[j]);
for(int j=0;j<32;j++)
if((a[i]&(1<<j)))
p[j]=i;
pos[a[i]]=i;
}
for(int i=0;i<32;i++)
p[i]=n+1;
for(int i=n;i>0;i--) {
r[i]=n+1;
for(int j=0;j<32;j++)
if(!(a[i]&(1<<j)))
r[i]=min(r[i],p[j]);
for(int j=0;j<32;j++)
if((a[i]&(1<<j)))
p[j]=i;
ans-=(ll)(i-l[i])*(ll)(r[i]-i);
}
printf("%lld\n",ans);
return 0;
}