CodeForces - 1190D Tokitsukaze and Strange Rectangle ( 离散化 + 树状数组 )

本文介绍了一种在平面上通过划定特殊矩形区域来获取不同点集组合的算法。该算法利用离散化处理和特定的数据结构实现高效计算,适用于处理大规模数据集。通过按点的y坐标降序扫描,并统计每条扫描线对应的点集数量,最终得到所有可能的不同点集总数。
Describe:

There are n points on the plane, the i-th of which is at (xi,yi). Tokitsukaze wants to draw a strange rectangular area and pick all the points in the area.
The strange area is enclosed by three lines, x=l, y=a and x=r, as its left side, its bottom side and its right side respectively, where l, r and a can be any real numbers satisfying that l<r. The upper side of the area is boundless, which you can regard as a line parallel to the x-axis at infinity. The following figure shows a strange rectangular area.
A point (xi,yi) is in the strange rectangular area if and only if l<xia. For example, in the above figure, p1 is in the area while p2 is not.
在这里插入图片描述
Tokitsukaze wants to know how many different non-empty sets she can obtain by picking all the points in a strange rectangular area, where we think two sets are different if there exists at least one point in one set of them but not in the other.
Input
The first line contains a single integer n (1≤n≤2×105) — the number of points on the plane.
The i-th of the next n lines contains two integers xi, yi (1≤xi,yi≤109) — the coordinates of the i-th point.
All points are distinct.

Output
Print a single integer — the number of different non-empty sets of points she can obtain.

sample input:

3
1 1
1 2
1 3

sample output:

3
题意:

平面上有n个点,你可以画一个左边界为l, 右边界为r, 下边界为a的无上界的区域(l, r, a 都可以为实数),把一些点框在里面,求有多少种不同的点集可以被框出来。(比如图片中可以框出三种点集:{p1} , {p2}, {p1, p2}

题解:

离散化,按y从大到小扫,如果处于y这条线的点只有一个,那么统计到目前为止,左边有多少间隔,右边有多少间隔,乘起来就是这个点的贡献。
如果处于y这条线的点有多个,先把这条线上所有点的x坐标对应间隔都加入当前的间隔中,然后再把这条线上的点从左到右扫一遍,一个点的贡献为:右边的间隔数 * (当前点左边的间隔数 - 前一个点左边的间隔数)。

题目链接
ac代码:

#include<bits/stdc++.h>
#define ll long long
#define lowbit(x) (x & -x)
using namespace std;
const int maxn = 2e5 + 50;
ll cx[maxn], cy[maxn];
int nx, ny;
ll l[maxn], r[maxn];
int n;
ll a[maxn];
void add(int i, int x)
{
    while(i < maxn){
        a[i] += x;
        i += lowbit(i);
    }
}
ll query(int i)
{
    ll ans = 0;
    while(i){
        ans += a[i];
        i -= lowbit(i);
    }return ans;
}
struct node{
    ll x, y;
    bool operator < (const node & a){
        if(y != a.y) return y > a.y;
        return x < a.x;
    }
}e[maxn];
int main()
{
    memset(l,0x3f,sizeof l);
    memset(r,0,sizeof r);
    nx = ny = 0;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
        scanf("%I64d%I64d",&e[i].x, &e[i].y);
        cx[++nx] = e[i].x;
        cy[++ny] = e[i].y;
    }
    cx[++nx] = 0x3f3f3f3f;
    sort(cx+1, cx+1+nx);
    nx = unique(cx+1, cx+1+nx) - cx - 1;
    sort(cy+1, cy+1+ny);
    ny = unique(cy+1, cy+1+ny) - cy - 1;
    ll ans = 0;
    sort(e+1, e+1+n);
    add(nx, 1);
    int pre = -1;
    for(int i = 1; i <= n; ++i){
        int posx = lower_bound(cx+1, cx+nx+1, e[i].x) - cx;
        if(query(posx) - query(posx-1) == 0) add(posx, 1);
        if(e[i].y != e[i+1].y){
            if(pre == -1){
                int posx = lower_bound(cx+1, cx+nx+1, e[i].x) - cx;
                ll L = query(posx);
                ll R = query(nx) - L;
                ans += L*R;
            }
            else{
                ll res = 0;
                ll A = query(nx);
                for(int j = pre; j <= i; ++j){
                    int posx = lower_bound(cx+1, cx+nx+1, e[j].x) - cx;
                    ll L = query(posx) - query(res);
                    ans += L*(A - query(posx));
                    res = posx;
                }
                pre = -1;
            }
        }
        else if(pre == -1) pre = i;
    }
    cout<<ans<<endl;
}


### 解题思路 #### 问题描述 Codeforces 1678C - Tokitsukaze and Strange Inequality 是一道关于排列组合与前缀和的应用问题。给定一个长度为 \( n \) 的排列数组 \( p \),需要统计满足条件 \( a < b < c < d \) 并且 \( p_a < p_c \) 同时 \( p_b > p_d \) 的四元组数量。 --- #### 核心思想 由于数据规模较小 (\( n \leq 5000 \)),可以直接通过枚举的方式解决问题。为了降低时间复杂度,引入 **前缀和** 技术来加速计算过程[^3]。 具体来说: - 枚举变量 \( a \) 和 \( c \),固定它们之后,目标是快速找到符合条件的 \( b \) 和 \( d \)- 使用预处理好的前缀和数组 `num` 来高效查询某个范围内满足特定关系的数量。 - 定义辅助数组 `sum` 表示对于固定的区间范围内的某些约束条件下的累积计数结果。 --- #### 实现细节 ##### 步骤一:构建前缀和数组 `num` 定义二维数组 `num[i][j]`,其中 `num[i][j]` 表示在序列的前 \( i \) 项中,有多少个元素大于 \( j \)。 该数组可以通过如下方式初始化: ```python n = len(p) max_val = max(p) # 初始化 num 数组 num = [[0] * (max_val + 2) for _ in range(n + 1)] for i in range(1, n + 1): for j in range(max_val + 1, -1, -1): # 反向遍历以保持正确性 if p[i - 1] > j: num[i][j] = num[i - 1][j] + 1 else: num[i][j] = num[i - 1][j] ``` 上述代码的时间复杂度为 \( O(n \cdot m) \),其中 \( m \) 是数组中的最大值。 --- ##### 步骤二:定义并填充辅助数组 `sum` 定义另一个二维数组 `sum[i][j]`,它表示当 \( a=i \), \( c=j \) 时,在区间 \([a+1, c-1]\) 中满足 \( p[b] > p[d] \) 的总贡献次数。 利用动态规划的思想逐步更新此数组: ```python sum_ = [[0] * (n + 1) for _ in range(n + 1)] bucket = [0] * (max_val + 1) for l in range(n - 1, 0, -1): bucket[p[l]] += 1 for r in range(l + 2, n + 1): sum_[l][r] = sum_[l][r - 1] + (num[r - 1][p[r - 1]] - num[l][p[r - 1]]) ``` 这里的关键在于如何有效累加当前区间的合法贡献,并借助之前已经计算的结果减少重复运算。 --- ##### 步骤三:枚举所有可能的 \( a \) 和 \( c \) 最后一步是对所有的 \( a \) 和 \( c \) 进行双重循环,并将对应位置上的 `sum[a][c]` 加入最终答案中: ```python result = 0 for a in range(1, n - 2): for c in range(a + 2, n): result += sum_[a][c] print(result) ``` 整个算法的核心部分即完成以上三个阶段的操作即可实现高效的解决方案。 --- ### 总结 本题主要考察的是对多重嵌套结构的有效简化以及合理运用前缀和技巧的能力。通过巧妙设计的数据结构能够显著提升程序运行效率至可接受水平。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值