Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) F. High Cry(思维 统计)
1 second
512 megabytes
standard input
standard 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 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.
5
3 2 1 6 5
8
4
3 3 3 3
0
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.
【题意】给你一个序列,问有多少个区间满足区间所有数的或值大于区间的所有数。
【分析】或值大于区间的所有数,即大于这个区间内的最大值。那我们可以枚举每个数作为最大值来算他的贡献。那么对于每个数,我们就要找到一个极限的区间使得这个数在此区间内是最大值。那么如何知道某个数在这个区间内或值大于他呢?考虑到或的性质,如果这个数 x 第 i 位上为0,而,离他最近的某个数 y 第 i 位上为1,那么区间内包含这个数x就必须包含这个数y,那么对于每一个非1 位,取一个离他最近的,左边右边都是。以上就是两个预处理,第一个预处理求每个数找到左边第一个大于等于它的数的位置的后一位置,和右边第一个大于它的数的前一位置。第二个预处理求满足或条件的最小区间,然后算贡献即可。
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 2e5+;;
const int M = ;
const int mod = ;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
typedef pair<ll,int>P;
int n,m,k;
int a[N],l[N],r[N],L[N],R[N],st[N],pos[];
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
l[i]=;r[i]=n;
L[i]=;R[i]=n+;
}
for(int i=;i<=n;i++){
while(st[]>&&a[st[st[]]]<a[i])--st[];
if(st[])l[i]=st[st[]]+;
st[++st[]]=i;
}
st[]=;
for(int i=n;i>;i--){
while(st[]>&&a[st[st[]]]<=a[i])--st[];
if(st[])r[i]=st[st[]]-;
st[++st[]]=i;
}
for(int i=;i<=n;i++){
for(int j=;j<=;j++){
if((a[i]&(<<j))==){
L[i]=max(L[i],pos[j]);
}
else {
pos[j]=i;
}
}
}
for(int i=;i<=;i++)pos[i]=n+;
for(int i=n;i>;i--){
for(int j=;j<=;j++){
if((a[i]&(<<j))==){
R[i]=min(R[i],pos[j]);
}
else {
pos[j]=i;
}
}
}
ll ans=;
for(int i=;i<=n;i++){
if(l[i]<=L[i])ans+=1LL*(L[i]-l[i]+)*(r[i]-i+);
if(r[i]>=R[i])ans+=1LL*(r[i]-R[i]+)*(i-l[i]+);
if(l[i]<=L[i]&&r[i]>=R[i])ans-=1LL*(L[i]-l[i]+)*(r[i]-R[i]+);
}
printf("%lld\n",ans);
return ;
}
/*
5
3 2 1 6 5
*/
Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) F. High Cry(思维 统计)的更多相关文章
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) D. Sorting the Coins
http://codeforces.com/contest/876/problem/D 题意: 最开始有一串全部由"O"组成的字符串,现在给出n个数字,指的是每次把位置n上的&qu ...
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) C. Classroom Watch
http://codeforces.com/contest/876/problem/C 题意: 现在有一个数n,它是由一个数x加上x每一位的数字得到的,现在给出n,要求找出符合条件的每一个x. 思路: ...
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) B. Divisiblity of Differences
http://codeforces.com/contest/876/problem/B 题意: 给出n个数,要求从里面选出k个数使得这k个数中任意两个的差能够被m整除,若不能则输出no. 思路: 差能 ...
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) A. Trip For Meal
http://codeforces.com/contest/876/problem/A 题意: 一个人一天要吃n次蜂蜜,他有3个朋友,他第一次总是在一个固定的朋友家吃蜂蜜,如果说没有吃到n次,那么他就 ...
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)
A. Trip For Meal 题目链接:http://codeforces.com/contest/876/problem/A 题目意思:现在三个点1,2,3,1-2的路程是a,1-3的路程是b, ...
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) E. National Property(2-sat)
E. National Property time limit per test 1 second memory limit per test 512 megabytes input standard ...
- ACM-ICPC (10/16) Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)
A. Trip For Meal Winnie-the-Pooh likes honey very much! That is why he decided to visit his friends. ...
- Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) D. Labyrinth
http://codeforces.com/contest/1064/problem/D 向上/向下加0,向左/右加1, step = 0,1,…… 求的是最少的步数,所以使用bfs. step=k ...
- Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) D. Labyrinth(重识搜索)
https://codeforces.com/contest/1064/problem/D 题意 给你一个有障碍的图,限制你向左向右走的次数,问你可以到达格子的个数 思路 可以定义状态为vi[x][y ...
随机推荐
- JS回调函数的应用,原来这么简单!
JS的回调函数很简单,看代码: 在a.js中 var myback = null; function load(obj){ myback = obj; } function save(){ // 后台 ...
- TED_Topic4:How I fell in love with quasars, blazars and our incredible universe
By Jedidah Isler # Background about our speaker Jedidah Isler studies blazars(耀变天体) — supermassive h ...
- POJ 3783 Balls --扔鸡蛋问题 经典DP
题目链接 这个问题是谷歌面试题的加强版,面试题问的是100层楼2个鸡蛋最坏扔多少次:传送门. 下面我们来研究下这个题,B个鸡蛋M层楼扔多少次. 题意:给定B (B <= 50) 个一样的球,从 ...
- 16、DecimalFormat类
DecimalFormat类概述 在一些金融或者银行的业务里面,会出现这样千分位格式的数字,¥123,456.00,表示人民币壹拾贰万叁仟肆佰伍拾陆元整,java.text包下提供了一个Decimal ...
- MS Office CVE-2015-1641 恶意 Exploit 样本分析
MS Office CVE-2015-1641 恶意 Exploit 样本分析 在对最近的一个恶意 MS Office 文档样本进行分析时,我们发现了一些有趣的特性.这个文档利用 CVE-2015-1 ...
- jumpserver安装教程
centos7系统一步一步安装jumpserver 参照官方文档,查找了百度所有的文档,基本上都是按照官方的文档操作的 官方文档点我-> 安装jumpserver需注意: 1:网络环境要好,有的 ...
- GO里的“指针”
指针 *T即为类型T的指针 &t即为获取变量t的地址 *p即为获取指针变量所指向的内容 var p *int 指针的*在左边 类型在右边.这里的 *int就是一个指针类型. 跟int str ...
- 深入理解HashMap(及hash函数的真正巧妙之处)
原文地址:http://www.iteye.com/topic/539465 Hashmap是一种非常常用的.应用广泛的数据类型,最近研究到相关的内容,就正好复习一下.网上关于hashmap的文章很多 ...
- SQL2000数据库修改sa密码
开始——程序——Microsoft SQL Server——企业管理器 2 展开数据库Microsoft SQL Server—— SQL Server组——安全性——登录——双击sa 3 在常规内有 ...
- Python爬虫之HDU提交数据
前一篇http://www.cnblogs.com/liyinggang/p/6094338.html 使用了爬虫爬取hdu 的代码,今天实现了将数据向hdu 提交的功能,接下来就是需要将两个功能合并 ...