zoukankan      html  css  js  c++  java
  • CodeForces 875 D High Cry

    High Cry

    题解:

    把思路转换成总-非法方案数。

    对于第i个点来说 找到L[i], R[i] 然后 对于所有的在[ L[i], R[i] ]  的值都 < a[i],

    然后对于第i个点来说 在 [L[i], i]这段区间中找到最大的x使得 a[x] | a[x+1] | ... | a[i] > a[i]

    同样在[i, R[i]]这段区间中找到最小的y使得 a[i] | a[i + 1] | a[i + 2] | ... | a[ x] > a[i]

    那么对于[x+1, y - 1]这段区间都是非法的区间。

    然后总答案 - 非法方案数。

    这样处理完之后,我们会发现样例二过不去 Orz,,,

    因为有相同数的时候,我们这个区间没有考虑过。

    所以我们定义在L[i] 和 i 之间的数是 <= a[i]的

    i 到 R[i]的数是 < a[i]的

    这样就可以使得每个区间只访问到一次。

    然后继续做上诉操作。

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
    #define LL long long
    #define ULL unsigned LL
    #define fi first
    #define se second
    #define pb push_back
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define lch(x) tr[x].son[0]
    #define rch(x) tr[x].son[1]
    #define max3(a,b,c) max(a,max(b,c))
    #define min3(a,b,c) min(a,min(b,c))
    typedef pair<int,int> pll;
    const int inf = 0x3f3f3f3f;
    const int _inf = 0xc0c0c0c0;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const LL _INF = 0xc0c0c0c0c0c0c0c0;
    const LL mod =  (int)1e9+7;
    const int N = 2e5 + 100;
    int a[N];
    int l[N], r[N];
    int main(){
        int n;
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
        a[0] = a[n+1] = inf;
        for(int i = 1; i <= n; ++i){
            l[i] = i - 1;
            while((a[l[i]] | a[i]) == a[i]) l[i] = l[l[i]];
        }
        for(int i = n; i >= 1; --i){
            r[i] = i + 1;
            while((a[r[i]]|a[i]) == a[i] && a[i] != a[r[i]]) r[i] = r[r[i]];
        }
        LL ans = n * (n + 1ll) / 2;
        for(int i = 1; i <= n; ++i){
            ans -= (1ll*i-l[i]) * (r[i]-i);
        }
        cout << ans << endl;
        return 0;
    }
    View Code
  • 相关阅读:
    Laravel -- Excel 导入(import) (v2.1.0)
    mysql触发器
    支付宝和微信支付的坑
    php 中的引用(&)与foreach结合后的一个注意点
    oatu2.0认证原理(转)
    python3:time类
    python3练习:针对某一日期增加或减少时间、天数
    python:datetime类常用内容
    python3练习:计算日期差
    python3练习:涉及序列
  • 原文地址:https://www.cnblogs.com/MingSD/p/10877060.html
Copyright © 2011-2022 走看看