您的位置:首页 > 其它

Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) F. High Cry

2017-11-30 23:38 495 查看
F. High Cry

time limit per test1 second

memory limit per test512 megabytes

inputstandard input

outputstandard 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 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

题意:给n个数,问有多少个区间符合区间内所有数的或,大于区间的最大值;

做法1:第一眼看到,经典的n*log*log区间查找问题,以每个数为起点最多有log种前缀或,所以枚举这log个节点,再从每段种找出区间最大值小于前缀或的个数。

做法2:a了之后发现别人都比我快了10倍,然后学到了一种玄学做法,对于每个点,找出以他为最大值,且区间最大值小于区间或的最左端点和最右端点,然后减去就好了,注意两个数相同可能会覆盖。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5+100;
int a
,b
,c
;
int main(){
int n;
scanf<
4000
/span>("%d",&n);
for(int i = 1;i <= n;i ++){
scanf("%d",&a[i]);
}
a[0] = a[n+1] = ~0;
for(int i = 1;i <= n;i ++){
b[i] = i-1;
while((a[i]|a[b[i]]) == a[i]) b[i] = b[b[i]];
}
for(int i= n;i >= 1;i --){
c[i] = i+1;
while((a[i]|a[c[i]])== a[i]&&a[i] > a[c[i]]) c[i] = c[c[i]];
}
ll ans = 1LL*n*(n-1)/2;
for(int i = 1;i <= n;i ++){
ans -= 1LL*(i-b[i])*(c[i]-i);
}
cout << ans +n<<endl;
return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5+100;
int st[20]
,mx[20]
;
int num
;
int n;
inline int clz(int x){return __builtin_clz(x);}
inline int clz(ll x){return __builtin_clzll(x);}
inline int lg2(int x){return !x ? -1 : 31-clz(x);}
inline int lg2(ll x){return !x ? -1 : 63-clz(x);}
void init(){
int nxt = 1;
int cns = -1;
for(int i = 1;i < N;i ++){

if(i == nxt){
nxt <<= 1;
cns ++;
}
num[i] = cns;
}
cin >> n;
for(int i = 1;i <= n;i ++){
scanf("%d",&st[0][i]);
mx[0][i] = st[0][i];
}
for(int i = 1;i < 20;i ++){
for(int j= 1;j <= n;j ++){
if(j + (1<<(i-1)) <= n)
st[i][j] = st[i-1][j]|st[i-1][j+(1<<(i-1))];
else st[i][j] = st[i-1][j];
if(j + (1<<(i-1)) <= n){
mx[i][j] = max(mx[i-1][j],mx[i-1][j+(1<<(i-1))]);
}
else mx[i][j] = mx[i-1][j];
}
}
}
int check(int l,int r){
//int lg = lg2(r-l+1);
int lg = num[r-l+1];
return st[lg][l]|st[lg][r-(1<<lg)+1];
}

int check2(int l,int r){
//int lg = lg2(r-l+1);
int lg = num[r-l+1];
return max(mx[lg][l],mx[lg][r-(1<<lg)+1]);
}
int main(){
init();
ll ans = 0;
for(int i = 1;i <= n;i ++){
int tmp = i;
while(1){
int l = i,r = n+1;
int cmp = check(i,tmp);
while(r-l>1){
int mid = l+r>>1;
if(check(i,mid) > cmp) r = mid;
else l = mid;
}
tmp= r;
//cout<<"!!" << i << ' '<< r << endl;
if(tmp > n) break;
cmp = check(i,tmp);
// if(check2(i,r) == cmp) continue;
l = tmp-1,r = n+1;
while(r - l > 1){
int mid = r+l>>1;
if(check2(i,mid) < cmp && check(i,mid) == cmp) l = mid;
else r = mid;
}
//cout << i << ' '<<tmp << ' '<< r << endl;
ans += r - tmp;
}
}
cout << ans << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: