Codeforces 876F High Cry【逆向思维】
来源:互联网 发布:神经网络算法预测股市 编辑:程序博客网 时间:2024/06/05 18:36
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.
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.
Print the only integer, the number of ways to choose two different mountains.
53 2 1 6 5
8
43 3 3 3
0
In the first test case all the ways are pairs of mountains with the numbers (numbering from one):
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【i】,表示以i作为中间点,【(L【i】~i),i】之内的区间都是不合法的,同理,对于R【i】,表示以i作为中间点,【i,(i,R【i】)】之间的区间都是不合法的。
那么Ans=n*(n+1)/2-(i-L【i】)*(R【i】-i)【1<=i<=n】
③我们以求L【i】为例,我们设定pos【j】=x,表示二进制下,第j位最后出现的位子在x处 (即a【x】&(1<<j)>0)。那么L【i】=max(L【i】,pos【j】)这里需要保证a【i】&(1<<j)==0;
求R【i】反过去就行了。同理。
Ac代码:
#include<stdio.h>#include<string.h>#include<algorithm>#include<map>using namespace std;int L[250000];int R[250000];int pos[250000];int a[250000];int main(){ int n; while(~scanf("%d",&n)) { memset(L,0,sizeof(L)); memset(R,0,sizeof(R)); memset(pos,0,sizeof(pos)); map<int,int>last; for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=n;i++) { L[i]=last[a[i]]; for(int j=0;j<=31;j++) { if((a[i]&(1<<j))==0) { L[i]=max(L[i],pos[j]); } } for(int j=0;j<=31;j++) { if((a[i]&(1<<j))>0)pos[j]=i; } last[a[i]]=i; } for(int j=0;j<=31;j++)pos[j]=n+1; for(int i=n;i>=1;i--) { R[i]=n+1; for(int j=0;j<=31;j++) { if((a[i]&(1<<j))==0) { R[i]=min(R[i],pos[j]); } } for(int j=0;j<=31;j++) { if((a[i]&(1<<j))>0)pos[j]=i; } } __int64 output=1ll*n*(n+1)/2; for(int i=1;i<=n;i++) { __int64 temp1=i-L[i]; __int64 temp2=R[i]-i; output-=temp1*temp2; } printf("%I64d\n",output); }}
- Codeforces 876F High Cry【逆向思维】
- Codeforces 876F High Cry【逆向思维】
- codeforces 876F High Cry (思维,位运算)
- [杂题 位运算] Codeforces #876F. High Cry
- Codeforces #441 Div2 F. High Cry
- Codeforces Round #441 (Div. 2)F. High Cry 按位考虑 + 思维
- Codeforces 875D High Cry
- Codeforces Round #441 (Div. 2)F. High Cry |区间问题
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) F. High Cry
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) F. High Cry
- Codeforces 875D High Cry rmq+分治
- Codeforces 875D High Cry [枚举+二进制]
- Codeforces 875D High Cry st表+分治
- Codeforces 611F 思维
- codeforces 217E 逆向思维
- codeforces 250/C 逆向思维
- Codeforces 828 D. High Load 思维+构造
- Codeforces Round #441 (Div. 1, by Moscow Team Olympiad) D. High Cry
- 使用json-server模拟REST API
- (M)DFS:98. Validate Binary Search Tree
- Android开发:5分钟解析Activity&Fragment生命周期
- Attention机制以及强化RNNs
- Spring Boot + Activiti
- Codeforces 876F High Cry【逆向思维】
- Android题目笔记(二)
- 最新用友企业管理软件ERP T6 V7.0免狗破解
- 微信开发内置浏览器JS自动关闭当前页面回到微信对话窗口
- 谈谈MySQL水平扩展--分库分表
- 自定义UIAlertController
- MySQL 5.7以上 root用户默认密码问题
- 低功耗蓝牙BLE外围模式(peripheral)-使用BLE作为服务端
- 【1543】冒泡排序