Codeforces 1187 F - Expected Square Beauty

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 << "\n";
#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\n", ans%MOD);
    return 0;
}

 

posted @ 2019-07-04 13:17  Wisdom+.+  阅读(262)  评论(0编辑  收藏  举报