zoukankan      html  css  js  c++  java
  • Codeforces 1187 F

    F - Expected Square Beauty

    思路:
    https://codeforces.com/blog/entry/68111

    代码:

    #pragma GCC optimize(2)
    #pragma GCC optimize(3)
    #pragma GCC optimize(4)
    #include<bits/stdc++.h>
    using namespace std;
    #define y1 y11
    #define fi first
    #define se second
    #define pi acos(-1.0)
    #define LL long long
    //#define mp make_pair
    #define pb push_back
    #define ls rt<<1, l, m
    #define rs rt<<1|1, m+1, r
    #define ULL unsigned LL
    #define pll pair<LL, LL>
    #define pli pair<LL, int>
    #define pii pair<int, int>
    #define piii pair<pii, int>
    #define pdd pair<double, double>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define debug(x) cerr << #x << " = " << x << "
    ";
    #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    //head
    
    const int MOD = 1e9 + 7;
    const int N = 2e5 + 5;
    int l[N], r[N], n;
    LL p[N], q[N], sum[N];
    LL q_pow(LL n, LL k) {
        LL res = 1;
        while(k) {
            if(k&1) res = (res * n) % MOD;
            n = (n * n) % MOD;
            k >>= 1;
        }
        return res;
    }
    int main() {
        scanf("%d", &n);
        for (int i = 1; i <= n; ++i) scanf("%d", &l[i]);
        for (int i = 1; i <= n; ++i) scanf("%d", &r[i]);
        for (int i = 1; i <= n; ++i) {
            int L = max(l[i-1], l[i]), R = min(r[i-1], r[i]);
            if(L > R) p[i] = 1, q[i] = 0;
            else q[i] = (R-L+1)*q_pow((r[i]-l[i]+1)*1LL*(r[i-1]-l[i-1]+1)%MOD, MOD-2)%MOD, p[i] = (1-q[i]+MOD)%MOD;
            sum[i] = (sum[i-1] + p[i]) % MOD;
        }
        LL ans = sum[n];
        for (int i = 1; i <= n ;++i) {
            LL tot = sum[n];
            tot = (tot - p[i])%MOD;
            if(i-1 >= 1) tot = (tot - p[i-1]) % MOD;
            if(i+1 <= n) tot = (tot - p[i+1]) % MOD;
            tot = (tot + MOD) % MOD;
            ans = (ans + p[i]*tot%MOD) % MOD;
        }
        for (int i = 1; i < n; ++i) {
            LL tot = ((1-q[i]-q[i+1])%MOD+MOD)%MOD;
            if(i-1 >= 1) {
                int L = max(l[i-1], max(l[i], l[i+1])), R= min(r[i-1], min(r[i], r[i+1]));
                if(L <= R)tot = (tot + (R-L+1)*q_pow((r[i]-l[i]+1)*1LL*(r[i-1]-l[i-1]+1)%MOD*(r[i+1]-l[i+1]+1)%MOD, MOD-2)%MOD)%MOD;
            }
            ans = (ans + 2*tot) % MOD;
        }
        printf("%lld
    ", ans%MOD);
        return 0;
    }
  • 相关阅读:
    BZOJ 3506 机械排序臂 splay
    BZOJ 2843 LCT
    BZOJ 3669 魔法森林
    BZOJ 2049 LCT
    BZOJ 3223 文艺平衡树 splay
    BZOJ 1433 假期的宿舍 二分图匹配
    BZOJ 1051 受欢迎的牛 强连通块
    BZOJ 1503 郁闷的出纳员 treap
    BZOJ 1096 ZJOI2007 仓库设计 斜率优化dp
    BZOJ 1396: 识别子串( 后缀数组 + 线段树 )
  • 原文地址:https://www.cnblogs.com/widsom/p/11131833.html
Copyright © 2011-2022 走看看