cf875D High Cry 单调栈+倍增

28 篇文章 0 订阅
24 篇文章 0 订阅

Description


给你一个长度为 n 的数列 ai​,求满足区间或 > 区间最大值的区间个数。

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.

Solution


很喜欢Rick&Morty,然鹅只能网盘见了╮(╯▽╰)╭

考虑正反两次单调栈求出每个数作为最大值的区间。一开始我想的是枚举一个端点二分另一个端点,区间和用线段树
然鹅两个log跑不过cf的强力数据。于是我们直接倍增上就行了,又短又快。。。
要注意相等相邻数字的处理,以及正反倍增的预处理

非常套路的题

Code


#include <stdio.h>
#include <string.h>
#define rep(i,st,ed) for (register int i=st;i<=ed;++i)
#define drp(i,st,ed) for (register int i=st;i>=ed;--i)

typedef long long LL;
const int N=400005;

int stack[N],a[N];
int L[N],R[N];
LL wjp[N][21],lzh[N][21];

int read() {
	int x=0,v=1; char ch=getchar();
	for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
	for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
	return x*v;
}

int main(void) {
	// freopen("data.in","r",stdin);
	// freopen("myp.out","w",stdout);
	int n=read();
	rep(i,1,n) lzh[i][0]=wjp[i][0]=a[i]=read();
	rep(j,1,18) {
		rep(i,1,n) wjp[i][j]=wjp[i][j-1]|wjp[i+(1<<(j-1))][j-1];
		drp(i,n,1) {
			lzh[i][j]=lzh[i][j-1];
			if (i>=(1<<(j-1))) lzh[i][j]|=lzh[i-(1<<(j-1))][j-1];
		}
	}
	for (register int i=1,top=0;i<=n;++i) {
		for (;top&&a[stack[top]]<=a[i];) top--;
		L[i]=stack[top]+1; stack[++top]=i;
	} stack[0]=n+1;
	for (register int i=n,top=0;i>=1;--i) {
		for (;top&&a[stack[top]]<a[i];) top--;
		R[i]=stack[top]-1; stack[++top]=i;
	}
	LL ans=0;
	rep(i,1,n) if (i-L[i]<=R[i]-i) {
		for (register int j=L[i];j<=i;++j) {
			LL ly=a[j]; int now=j;
			drp(k,18,0) {
				if ((now+(1<<k)<=R[i])&&((ly|wjp[now+1][k])<=a[i])) {
					ly|=wjp[now+1][k];
					now+=(1<<k);
				}
			}
			if (now>=i&&now<=R[i]) ans+=now-i+1;
		}
	} else {
		for (register int j=i;j<=R[i];++j) {
			LL ly=a[j]; int now=j;
			drp(k,18,0) {
				if ((now-(1<<k)>=L[i])&&((ly|lzh[now-1][k])<=a[i])) {
					ly|=lzh[now-1][k];
					now-=(1<<k);
				}
			}
			if (now<=i&&now>=L[i]) ans+=i-now+1;
		}
	}
	printf("%lld\n", 1LL*n*(n+1LL)/2LL-ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值