Codeforces Round #441 (Div. 1, by Moscow Team Olympiad) D. High Cry

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
Input
5
3 2 1 6 5
Output
8
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.


题意:给你一个数列,然后问有多少对(l,r)满足下标在l和r之间的所有数的或大于下标在(l,r)间的所有值。


分析:很显然任何一个满足答案的区间中都有一个最大值,如果有多个那么我们就只算最左边的那个,然后现在我们只需要让整个区间的或和大于这个最大值就可以了,如果想让最大值或上某个不大于它的值变得更大,显然需要这个数存在某个二进制位为1而最大值的这一位为0,那么我们就可以考虑每个下标的数作为最大值对答案的贡献,做两边单调栈就可以求出来每个位置作为最大值所能控制的区间,然后只要枚举它的所有二进制0位,然后查找这一位为1的所有位置中左右距离它最近的两个位置就能算出来这个位置的数作为最大值对答案的贡献了。


#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
typedef long long ll;
int n,a[N],lm[N],rm[N],q[N];
vector<int> tree[35];
ll ans;
int main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n;i++) 
	{
		scanf("%d",&a[i]);
		for(int j = 0;(1<<j) <= a[i];j++)
		 if(a[i] & (1<<j)) tree[j].push_back(i);
	}
	int tail = 0;
	for(int i = 1;i <= n;i++)
	{
		while(tail && a[q[tail-1]] < a[i]) tail--;
		lm[i] = tail ? q[tail-1] + 1 : 1;
		q[tail++] = i;
	}
	tail = 0;
	for(int i = n;i;i--)
	{
		while(tail && a[q[tail-1]] <= a[i]) tail--;
		rm[i] = tail ? q[tail-1] - 1 : n;
		q[tail++] = i;
	}
	for(int i = 1;i <= n;i++)
	{
		if(lm[i] == rm[i]) continue;
		int tl = lm[i]-1,tr = rm[i]+1;
		for(int j = 0;(1<<j) <= a[i];j++)
		 if(!((1<<j) & a[i]))
		 {
			 if(tree[j].size() == 0) continue;
			 auto it = lower_bound(tree[j].begin(),tree[j].end(),i);
			 if(it != tree[j].begin()) 
			 {
				 it--;
				 tl = max(tl,*it);
			 } 
			 it = upper_bound(tree[j].begin(),tree[j].end(),i);
			 if(it != tree[j].end()) tr = min(tr,*it);
 		 }
 		ans += 1ll*(i-lm[i]+1)*(rm[i]-i+1ll) - 1ll*(i-tl)*(tr-i);
	}
	cout<<ans<<endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值