zoukankan      html  css  js  c++  java
  • Codeforces Round #441(Div.2) F High Cry

    F. High Cry
    time limit per test: 1 second
    memory limit per test: 512 megabytes

    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.

    Output

    Print the only integer, the number of ways to choose two different mountains.

    统计或和>=最大值的区间数;

    考虑计算每个数最为最大值时的贡献,因此需要计算满足“该数作为最大值”的区间和满足“或和>=该数”的区间;

    第一类区间可以单调栈维护,注意两端点只有一端考虑边界数与选定数相等;

    第二类区间,考虑按位计算,注意到对于选定数的任意 0 位,只要区间内任意数该位为 1 即为合法区间;

    所以按位拆分后可以转成类似最小全 0 子矩形的问题,用悬线法解决;

    然后两类区间容斥一下就行了;

    AC GET☆DAZE

     

    ↓代码

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<string>
     6 #include<vector>
     7 #include<cmath>
     8 #include<queue>
     9 #include<ctime>
    10 #include<map>
    11 #include<set>
    12 #define N 200039
    13 #define ll long long
    14 #define inf 0x3f3f3f3f
    15 using namespace std;
    16 ll num[N],L[N],R[N],l[39][N],r[39][N];
    17 ll sta[N],h,ans;
    18 int main()
    19 {
    20     ll n,stp,a,b,c;
    21     scanf("%lld",&n);
    22     for(a=1;a<=n;a++)
    23     {
    24         scanf("%lld",&num[a]);
    25     }
    26     for(a=1,h=0,sta[0]=0;a<=n;a++)
    27     {
    28         while(num[a]>=num[sta[h]] && h)
    29         {
    30             h--;
    31         }
    32         L[a]=sta[h],sta[++h]=a;
    33     }
    34     for(a=n,h=0,sta[h]=n+1;a>=1;a--)
    35     {
    36         while(num[a]>num[sta[h]] && h)
    37         {
    38             h--;
    39         }
    40         R[a]=sta[h],sta[++h]=a;
    41     }
    42     for(a=0;a<30;a++)
    43     {
    44         l[a][1]=stp=1;
    45         for(b=2;b<=n;b++)
    46         {
    47             if(((1<<a)&num[b-1])>=1 && ((1<<a)&num[b])==0)
    48             {
    49                 stp=b;
    50             }
    51             if(((1<<a)&num[b])==0)
    52             {
    53                 l[a][b]=stp;
    54             }
    55             else
    56             {
    57                 l[a][b]=1;
    58             }
    59         }
    60         r[a][n]=stp=n;
    61         for(b=n-1;b>=1;b--)
    62         {
    63             if(((1<<a)&num[b+1])>=1 && ((1<<a)&num[b])==0)
    64             {
    65                 stp=b;
    66             }
    67             if(((1<<a)&num[b])==0)
    68             {
    69                 r[a][b]=stp;
    70             }
    71             else
    72             {
    73                 r[a][b]=n;
    74             }
    75         }
    76         for(b=1;b<=n && a;b++)
    77         {
    78             l[a][b]=max(l[a][b],l[a-1][b]);
    79             r[a][b]=min(r[a][b],r[a-1][b]);
    80         }
    81     }
    82     for(a=1;a<=n;a++)
    83     {
    84         if(l[29][a]-L[a]>1)
    85         {
    86             ans+=(R[a]-a)*(l[29][a]-L[a]-1);
    87         }
    88         if(R[a]-r[29][a]>1)
    89         {
    90             ans+=(a-L[a])*(R[a]-r[29][a]-1);
    91         }
    92         if(l[29][a]-L[a]>1 && R[a]-r[29][a]>1)
    93         {
    94             ans-=(l[29][a]-L[a]-1)*(R[a]-r[29][a]-1);
    95         }
    96     }
    97     printf("%lld",ans);
    98     return 0;
    99 }
    High Cry
  • 相关阅读:
    P3193 [HNOI2008]GT考试(KMP+矩阵乘法加速dp)
    P2606 [ZJOI2010]排列计数
    洛谷P2657 [SCOI2009]windy数
    P2602 [ZJOI2010]数字计数(递推)
    洛谷 P1073 最优贸易
    [一本通学习笔记] 字典树与 0-1 Trie
    [一本通学习笔记] KMP算法
    [一本通学习笔记] 字符串哈希
    [一本通学习笔记] 广度优先搜索和优化
    [一本通学习笔记] 深度优先搜索与剪枝
  • 原文地址:https://www.cnblogs.com/Sinogi/p/7700539.html
Copyright © 2011-2022 走看看