[Nowcoder 2018ACM多校第四场E] Skyline

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013578420/article/details/81282843

题目大意:
给你n个点 ( x i , y i ) , 每个点有一个出现的概率 a i b i , 求(x,y)满足至少有一个i, 使得 0 < x x i , 0 < y y i 的期望个数。 ( n 10 5 , n 10 6 , 1 x i , y i 10 9 , 1 a i b i 10 9 )

题目思路:
单独考虑每个点的贡献, 某个点产生的期望是 1 - 所有在它右上方的点均没有出现的概率。 可以离散化后, 用线段树+扫描线解决。 线段树维护一段区间的后缀乘积的和,线段树维护y坐标, 将点按x坐标排序, 扫描线根据x坐标一段一段的算贡献。

PS:
有几个要注意的细节, 一个是离散化时记得把边界加进去, 一个是注意 a i == b i 的特殊点。

Code:

#include <map>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

#define ll long long
#define db double
#define fi first
#define se second
#define mp(x, y) make_pair(x, y)
#define ls (x << 1)
#define rs ((x << 1) | 1)
#define mid ((l + r) >> 1)

using namespace std;

const int N = (int)1e5 + 10;
const int mo = (int)1e9 + 7;

ll pw(ll x, ll k){
    ll ret = 1;
    while (k){
        if (k & 1) ret = ret * x % mo;
        x = x * x % mo;
        k >>= 1;
    }
    return ret;
}

ll ine(ll x){
    return pw(x, mo - 2);
}

int n, m, c[N], x[N], y[N], a[N], b[N], id[N];

ll sum[N << 2], pro[N << 2], val[N << 2]; int sz[N << 2];
void update(int x, int l, int r){
    sum[x] = (sum[rs] + sum[ls] * pro[rs] % mo + (c[mid + 1] - c[mid] - 1) * pro[rs] % mo) % mo;
    pro[x] = pro[ls] * pro[rs] % mo;
}
void build(int x, int l, int r){
    if (l == r) {sum[x] = 1; pro[x] = 1; val[x] = 1; sz[x] = 0; return;}
    build(ls, l, mid);
    build(rs, mid + 1, r);
    update(x, l, r);
}
void modf(int x, int l, int r, int pos, int _a, int _b){
    if (l == r){
        val[x] = val[x] * _a % mo * ine(_b) % mo;
        if (sz[x] == 0) sum[x] = pro[x] = val[x];

        return ;
    }
    if (pos <= mid) modf(ls, l, mid, pos, _a, _b);
    else modf(rs, mid + 1, r, pos, _a, _b);
    update(x, l, r);
}
void s_modf(int x, int l, int r, int pos, int v){
    if (l == r){
        sz[x] += v;
        if (sz[x]) sum[x] = pro[x] = 0;
        else sum[x] = pro[x] = val[x];
        return ;
    }
    if (pos <= mid) s_modf(ls, l, mid, pos, v);
    else s_modf(rs, mid + 1, r, pos, v);
    update(x, l, r);
}

bool cmp(int i, int j){
    return x[i] < x[j];
}

int main(){

    int T; scanf("%d", &T);

    while (T --){
        scanf("%d", &n); m = 0;
        for (int i = 1; i <= n; i ++){
            scanf("%d %d %d %d", x + i, y + i, a + i, b + i);
            a[i] = b[i] - a[i]; c[++ m] = y[i];
        }

        c[++ m] = 1;
        sort(c + 1, c + m + 1); 
        m = unique(c + 1, c + m + 1) - c - 1;

        build(1, 1, m);
        for (int i = 1; i <= n; i ++){
            if (a[i] != 0) modf(1, 1, m, lower_bound(c + 1, c + m + 1, y[i]) - c, a[i], b[i]);
            else s_modf(1, 1, m, lower_bound(c + 1, c + m + 1, y[i]) - c, 1);
        }

        ll ans = 0;
        for (int i = 1; i <= n; i ++) id[i] = i;
        sort(id + 1, id + n + 1, cmp);

        for (int j = 1; j <= n; j ++){
            int i = id[j];

            ans = (ans + (c[m] - sum[1]) * (x[id[j]] - x[id[j - 1]]) % mo) % mo;

            if (a[i] != 0) modf(1, 1, m, lower_bound(c + 1, c + m + 1, y[i]) - c, b[i], a[i]);
            else s_modf(1, 1, m, lower_bound(c + 1, c + m + 1, y[i]) - c, -1);
        }

        if (ans < 0) ans += mo;
        printf("%lld\n", ans);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/u013578420/article/details/81282843
今日推荐