1. 程式人生 > >CF876 F 思維 枚舉

CF876 F 思維 枚舉

sizeof ems eof 我們 eth date push_back http pla

給你n個數,問有幾個區間滿足,區間內任意兩數或操作都大於區間內的任意數。

首先可以知道,兩數或操作的結果必定不會小於兩者間的最大值,也就是說對於一個區間中,不合法的狀態只有兩值或相等。那麽我們可以考慮枚舉每個數,向左向右找到第一個或不相等的,那麽該數對所有不合法區間的貢獻就能找到了,所以與其找合法的區間不如容斥找不合法的區間。

具體從左往右枚舉每個數,同時記錄該數某二進制位為0時,左側數中該位出現1的離i的最近位置,得到左邊界。右邊界類似。

然後就是要註意重復的數,重復的數出現直接就使區間不合法,左右兩側收縮邊界時只要有一側考慮重復數即可。

/** @Date    : 2017-10-16 23:43:44
  * @FileName: F.cpp
  * @Platform: Windows
  * @Author  : Lweleth ([email protected])
  * @Link    : https://github.com/
  * @Version : $Id$
  */
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 2e5+20;
const double eps = 1e-8;

int a[N];
LL l[N];
LL r[N];
LL t[N];
map<int, int>q;
int main()
{
	LL n;
	cin >> n;
	for(int i = 1; i <= n; i++)
		scanf("%d", a + i);
	LL ans = 0;
	for(int i = 1; i <= n; i++)
	{
		l[i] = q[a[i]];//標記重復數位置,重復數必定使區間不合法
		for(int j = 0; j < 31; j++)
		{
			if((a[i] & (1LL << j)))
				t[j] = i;
			else l[i] = max(l[i], t[j]);
		}
		q[a[i]] = i;
	}
	for(int i = 0; i < 31; i++)
		t[i] = n + 1;
	for(int i = n; i >= 0; i--)
	{
		r[i] = n + 1;
		for(int j = 0; j < 31; j++)
		{
			if((a[i] & (1LL << j)))
				t[j] = i;
			else r[i] = min(r[i], t[j]);
		}
	}
	for(int i = 1; i <= n; i++)
	{
		//cout << l[i] <<"~"<< i << "~"<< r[i] << endl;
		ans -= (i - l[i]) * (r[i] - i);
	}
	ans += n * (n + 1LL) / 2LL;
	printf("%lld\n", ans);
    return 0;
}

CF876 F 思維 枚舉