Codeforces 875D High Cry st表+分治

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








传送门


题意:给出n个数,询问有几个区间[L,R],使得L到R内所有数or起来的值>[L,R]内的最大值。


可以发现找出一个最大值之后,将它转化成二进制,

那么它的某一个0位置,假如某一个数字这一位是1,那么这个数字加入肯定就不合法了。

Pre[i][j]表示第i个数字二进制第j位,在1~(i-1)内的最靠后的二进制第j位为0的位置;

Next[i][j]表示第i个数字二进制第j位,在(i+1)~n内的最靠前的二进制第j位为0的位置。

那么对于一个位置x,区间L肯定L<=max{Pre[i][j]}

同理R>=min{Next[i][j]}

那么可以直接计算出区间的个数了。

每次递归处理[L,R],找出最大值的位置x(用st表),然后统计答案后,

分治解决[L,x-1],[x+1,R]即可。


区间判断错了导致没A很难受= =

方法没什么问题,代码不怎么想调了= =直接放上来吧。。。


UPDATE:已经发现代码的问题并且AC,

计算答案的时候要注意,只要一个端点在不合法范围内就是合法区间,

假设不合法的范围是L~l,r~R,最大数位置在m,用[L,R]表示区间L~R的长度

那么应该是:[L,l]*[m+1,R]+[r,R]*[L,m-1]-[L,l]*[r,R]

后面的减去是为了去除公共计算的部分,画个图就很好理解了。


代码已经更新。





#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll ans;
int n,f[200005][20],a[200005][31];
int x[200005],pre[200005][31],Next[200005][31];
void zh(int i,int x){
    int len=0;
    while (x){
        a[i][++len]=x&1;
        x>>=1;
    }
    while (len<30) a[i][++len]=0;
}
int MAX(int L,int R){
    int k=(double)log(R-L+1)/(double)log(2);
    if (x[f[L][k]]>x[f[R-(1<<k)+1][k]]) return f[L][k];
        else return f[R-(1<<k)+1][k];
}
void solve(ll L,ll R){
    if (L>R) return;
    int m=MAX(L,R);
    ll l=L-1,r=R+1;
    for (int i=1;i<=30;i++)
        if (!(x[m]&(1<<(30-i))))
            l=max(l,(ll)pre[m][31-i]),r=min(r,(ll)Next[m][31-i]);
    ans+=(l-L+1)*(R-m+1)+(R-r+1)*(m-L+1)-(l-L+1)*(R-r+1);   //计算答案***
    solve(L,m-1);
    solve(m+1,R);
}
int main(){
    scanf("%d",&n);
    for (int i=1;i<=n;i++){
        scanf("%d",&x[i]);
        f[i][0]=i;
        zh(i,x[i]);
    }
    int last[35];
    memset(last,0,sizeof(last));
    for (int i=1;i<=n;i++)
        for (int j=1;j<=30;j++){
            pre[i][j]=last[j];
            if (a[i][j]==1) last[j]=i;
        }
    for (int i=1;i<=30;i++) last[i]=n+1;
    for (int i=n;i;i--)
        for (int j=1;j<=30;j++){
            Next[i][j]=last[j];
            if (a[i][j]==1) last[j]=i;
        }
    for (int j=1;j<=30;j++)
        for (int i=1;i<=n;i++)
            if (i+(1<<j)-1>n) break;
                else{
                    if (x[f[i][j-1]]>x[f[i+(1<<(j-1))][j-1]])
                        f[i][j]=f[i][j-1];
                         else f[i][j]=f[i+(1<<(j-1))][j-1];
                }
    ans=0LL;
    solve(1,n);
    cout<<ans<<endl;
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值